最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Is it possible to add a new line in mailto url? - Stack Overflow

programmeradmin3浏览0评论

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 badges
Add a ment  | 

4 Answers 4

Reset to default 7

Just 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.

发布评论

评论列表(0)

  1. 暂无评论