Created
October 19, 2018 13:40
-
-
Save m-tymchyk/ea71bfdf9b60af0205a16165d3cc4d3f to your computer and use it in GitHub Desktop.
React Native / How to send email
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// send-email.js | |
// We can use react-native Linking to send email | |
import qs from 'qs'; | |
import { Linking } from 'react-native'; | |
export async function sendEmail(to, subject, body, options = {}) { | |
const { cc, bcc } = options; | |
let url = `mailto:${to}`; | |
// Create email link query | |
const query = qs.stringify({ | |
subject: subject, | |
body: body, | |
cc: cc, | |
bcc: bcc | |
}); | |
if (query.length) { | |
url += `?${query}`; | |
} | |
// check if we can use this link | |
const canOpen = await Linking.canOpenURL(url); | |
if (!canOpen) { | |
throw new Error('Provided URL can not be handled'); | |
} | |
return Linking.openURL(url); | |
} | |
// example.js | |
import { sendEmail } from './send-email'; | |
sendEmail( | |
'[email protected]', | |
'Greeting!', | |
'I think you are fucked up how many letters you get.' | |
).then(() => { | |
console.log('Our email successful provided to device mail '); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Any updates?