Skip to content

Instantly share code, notes, and snippets.

@ajaykumar97
Created July 29, 2019 17:59
Show Gist options
  • Save ajaykumar97/354668aa27567060282a8cf48348ca13 to your computer and use it in GitHub Desktop.
Save ajaykumar97/354668aa27567060282a8cf48348ca13 to your computer and use it in GitHub Desktop.
This is an example function to pick document from the device, attach it with the email and open in the email application(gmail etc.)
/*
Call pickDocAndSendToMail function on button click
*/
//...other imports
import DocumentPicker from 'react-native-document-picker';
import Mailer from 'react-native-mail';
import RNFS from 'react-native-fs';
pickDocAndSendToMail = async () => {
try {
const res = await DocumentPicker.pick({
type: [DocumentPicker.types.pdf],
});
console.log('document picker response is: ', res);
let path = res.uri;
if (Platform.OS !== 'ios') {
try {
let hasPermission = await PermissionsAndroid.check(PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE);
if (!hasPermission) {
const granted = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE
);
hasPermission = granted !== PermissionsAndroid.RESULTS.GRANTED;
}
if (!hasPermission) {
console.log('permission denied');
return;
}
} catch (error) {
console.warn(error);
}
path = `${RNFS.ExternalStorageDirectoryPath}/project_overview_${Number(new Date())}.pdf`;
try {
await RNFS.copyFile(res.uri, path);
} catch (error) {
console.log('file copy error: ', error);
return;
}
}
Mailer.mail({
subject: 'need help',
recipients: [],
body: '<b>A Bold Body</b>',
isHTML: true,
attachment: {
path, // The absolute path of the file from which to read data.
type: 'pdf', // Mime Type: jpg, png, doc, ppt, html, pdf, csv
name: 'project_overview.pdf', // Optional: Custom filename for attachment
}
}, (error, event) => {
if (error) {
console.log('mailer error: ', error);
}
});
} catch (err) {
if (DocumentPicker.isCancel(err)) {
// User cancelled the picker, exit any dialogs or menus and move on
} else {
throw err;
}
}
};
//...other code
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment