3

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@example.com?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

Martin Skec
  • 118
  • 2
  • 9

3 Answers3

7

Just use plain old <br> tag.

example :

string newLine = `<br>`;

let body = 'Hi,'
            + newLine
            + 'This will come after 1 Line'
            + newLine +newLine + 'This will come after 2 Lines';

Output in default mail app :

Default mail application is :

Rajesh Sharma
  • 161
  • 1
  • 4
1

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.

Rishabh876
  • 3,010
  • 2
  • 20
  • 37
-1

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:test@gmail.com?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@example.com?subject=${subject}&body=${body}`);

If this works then you have an issue with encodeURIComponent.

Adriaan De Bolle
  • 225
  • 1
  • 3
  • 20
  • I saw this question and I tried to replace `\n` with `%0D%0A`, didn't work. – Martin Skec Feb 04 '20 at 21:40
  • Can you manually enter this in the address bar: `mailto:email@example.com?subject=test&body=my email%0D%0Abody`? Can you change, if needed, your default mail provider to a browser service (gmail.com). Then you could check what the generated output is: `https://mail.google.com/mail/u/0/?view=cm&fs=1&tf=1&source=mailto&su=test&to=email@example.com&body=my+email%0D%0Abody`. Maybe this will give us some idea what is going wrong. – Adriaan De Bolle Feb 04 '20 at 21:54
  • It works on iOS gmail, but can't get it to work on Android. Maybe it a gmail app issue. – Martin Skec Feb 05 '20 at 21:06
  • On my iPhone `mailto:email@example.com?subject=test&body=my email%0D%0Abody` does not work either. Because I deleted my default Mail client. Maybe you do not have a default mail client on your Android either? – Adriaan De Bolle Feb 10 '20 at 09:58
  • It's also working in outlook on Android. It's not working in gmail app on multiple android devices I tried. Maybe it's due to missing default mail client, can't tell. – Martin Skec Feb 10 '20 at 11:01