I am trying to open an email client with mailto
url from my react-native app.
Code snippet used for opening a client:
const body = 'my email\nbody';
Linking.openURL(`mailto:[email protected]?subject=${subject}&body=${encodeURIComponent(body)}`);
The email client is opening (Android, Gmail), but no newlines are rendered.
I have also tried to use %0D%0A
and \r\n
, but no results.
Is it possible to add new lines in mailto
in react-native?
react-native: 0.61.0
Android: 9
I am trying to open an email client with mailto
url from my react-native app.
Code snippet used for opening a client:
const body = 'my email\nbody';
Linking.openURL(`mailto:[email protected]?subject=${subject}&body=${encodeURIComponent(body)}`);
The email client is opening (Android, Gmail), but no newlines are rendered.
I have also tried to use %0D%0A
and \r\n
, but no results.
Is it possible to add new lines in mailto
in react-native?
react-native: 0.61.0
Android: 9
Share Improve this question edited Oct 7, 2024 at 7:09 benomatis 5,6437 gold badges39 silver badges60 bronze badges asked Feb 4, 2020 at 20:52 Martin SkecMartin Skec 1282 silver badges9 bronze badges4 Answers
Reset to default 7Just use plain old <br>
tag.
example :
string newLine = `<br>`;
let body = 'Hi,'
+ newLine
+ 'This will e after 1 Line'
+ newLine +newLine + 'This will e after 2 Lines';
Output in default mail app :
Since replace('\n', '<br>')
will only replace first occurrence. You can use
emailBody.replace(/(\n)/g, '<br>')
This will make sure all new lines are replaced.
What worked for me was a bination of previously provided answers, adding it here, it may help someone.
Basically I'm replacing %0A
with %0D%0A
on the already encodeURIComponent
'd string as follows:
const body = encodeURIComponent('my email\nbody').replaceAll('%0A', '%0D%0A');
Linking.openURL(`mailto:[email protected]?subject=${subject}&body=${body}`);
This should work %0D%0A
. Check this question
You do not have to use your ReactNative app to test this href, you can just paste the following within your URL bar and you will see it works as expected:
mailto:[email protected]?body=test%0D%0Anext
So what if you use it like this:
let body = 'my email\nbody';
body = body.replace('\n', '%0D%0A');
Linking.openURL(`mailto:[email protected]?subject=${subject}&body=${body}`);
If this works then you have an issue with encodeURIComponent.