react-native-mail: Can't attach the file in email for the android mobile.
I’m using the below code. It’s working fine in the IOS mobile. But Not working in the Android mobile. An error shows the “permission denied for the attachment”. After I’m change the path path:‘file://’+DocumentDirectoryPath + ‘/Log.txt’, That time an error shows the “can’t attach the empty file” `sendMail = (issueType) => {
Mailer.mail({
subject: 'need help',
recipients: ['support@example.com'],
ccRecipients: ['supportCC@example.com'],
bccRecipients: ['supportBCC@example.com'],
body: '<b>A Bold Body</b>',
isHTML: true,
attachment: {
path: DocumentDirectoryPath + '/Log.txt', // The absolute path of the file from which to read data.
type: 'text', // Mime Type: jpg, png, doc, ppt, html, pdf, csv"Fetchh_Log.txt"
name: 'Log.txt', // Optional: Custom filename for attachment
}
}, (error, event) => {
Alert.alert(
error,
event,
[
{text: 'Ok', onPress: () => console.log('OK: Email Error Response')},
{text: 'Cancel', onPress: () => console.log('CANCEL: Email Error Response')}
],
{ cancelable: true }
)
});
}`
Any mistake in the above code? or any changes for android mobile?
About this issue
- Original URL
- State: open
- Created 6 years ago
- Reactions: 5
- Comments: 18
I have a workaround. You need to use
react-native-fs. The solution is to copy the file from the private app cache to the external storage which is readable by other apps.In case anyone is still looking for a solution, that’s what finally worked in my case: https://github.com/marcinolek/react-native-mail/commit/384d3629d2fe23bc1ded25447cad3532f515030b
Inspired by PR 127 & https://stackoverflow.com/questions/18249007/how-to-use-support-fileprovider-for-sharing-content-to-other-apps
For anyone still struggling with this issue, it is no longer recommended to share data such as an attachment using the file:// scheme. The solution is to implement a FileProvider. It allows you to securely pass file defined through an intent.
Useful links
https://inthecheesefactory.com/blog/how-to-share-access-to-file-with-fileprovider-on-android-nougat/en https://stackoverflow.com/questions/18249007/how-to-use-support-fileprovider-for-sharing-content-to-other-apps
Hope this helps
You must save the image to device’s picture directory.
import RNFS from “react-native-fs”;
var destPath = RNFS.PicturesDirectoryPath + ‘/yourpicture.jpg’; RNFS.moveFile(data.uri, destPath) .then((success) => { console.log(‘file moved!’); }) .catch((err) => { console.log("Error: " + err.message); });
//then in your Mailer.mail attachment //the correct path should be /storage/emulated/0/Pictures/yourpicture.jpg
path: destPath
+1
It’s working fine in the iOS mobile. But not working in the Android Mobile.