10

I'm generating a mailto: link that also contains the body of an email. I'm opening the link using JavaScript to launch the mailto: client of the OS. On Chromebooks the link opens Gmail with the email address, but not the body of the email. This is the link:

var MailToLink = 'mailto:test@test.com?subject=Test%20Email%20Subject&body=Great,%20the%20mailto%20protocol%20works%20and%20you%27re%20good%20to%20go.%20Good%20luck.'

This is the line I use to open the link: window.open(MailToLink, '_blank');

It works just fine on Windows 10 OS with Thunderbird and Gmail for Android.

Is there something I need to change for Chromebooks?

Trevor Reid
  • 3,310
  • 4
  • 27
  • 46
frenchie
  • 51,731
  • 109
  • 304
  • 510

4 Answers4

3

What about setting location.href instead of creating a popup?

location.href = "mailto:test@test.com?subject=Test%20Email%20Subject&body=Great,%20the%20mailto%20protocol%20works%20and%20you%27re%20good%20to%20go.%20Good%20luck."

Looking for an answer drawing from credible and/or official sources.

Good to know is that subject and body in mailto links are described in RFC 2368 - The mailto URL scheme

Clients that resolve mailto URLs into mail messages should be able to correctly create RFC 822-compliant mail messages using the "subject" and "body" headers.

Please also note there is a paragraph over "unsafe headers" - so I think the content could be also important.

  1. Unsafe headers

    The user agent interpreting a mailto URL SHOULD choose not to create a message if any of the headers are considered dangerous; it may also choose to create a message with only a subset of the headers given in the URL. Only the Subject, Keywords, and Body headers are believed to be both safe and useful.

Julian
  • 33,915
  • 22
  • 119
  • 174
2

Try this

var MailToLink = 'mailto:test@test.com?subject=Test%20Email%20Subject&body=Great,%20the%20mailto%20protocol%20works%20and%20you%27re%20good%20to%20go.%20Good%20luck.'
var sendEmail = document.getElementById('sendEmail');

  sendEmail.addEventListener('click', function (e){
    window.location.href = MailToLink;
  });
<input type="button" id="sendEmail" value="submit">
Fadi
  • 267
  • 2
  • 6
1

Another stable option is to a <a> and edit the href with javascript.

e.g.

var mailto = "mailto:test@test.com?subject=Test%20Email%20Subject&body=Great,%20the%20mailto%20protocol%20works%20and%20you%27re%20good%20to%20go.%20Good%20luck."

document.getElementById("myLink").setAttribute("href", mailto)
<html>
<body>
    <a id="myLink">Create email now!</a>
</body>
</html>

Not sure if this fits in your requirements.

Julian
  • 33,915
  • 22
  • 119
  • 174
0

The easiest thing to do might be to use the classic mail to link using an anchor tag, however, I am guessing you are using JavaScipt for a specific reason so maybe if you specify a simple name as the second argument rather than one of the '_blank' or '_self' values. for example, you could call it 'emailWindow' or something like that.

Here is the is the MDN link that inspired using the window name: https://developer.mozilla.org/en-US/docs/Web/API/Window/open#Parameters

and here is some code below to test it out.

*Note: For security reasons I believe StackOverflow has disabled the ability to open a new window so you will have to test the button code locally, sorry

var MailToLink = 'mailto:test@test.com?subject=Test%20Email%20Subject&body=Great,%20the%20mailto%20protocol%20works%20and%20you%27re%20good%20to%20go.%20Good%20luck.'

const sendEmailButton = document.getElementById('sendEmailButton');
sendEmailButton.onclick = () => {
    window.open(MailToLink, 'emailWindow');
    if (window.open && !window.closed) {window.close();}
};
<h1>Anchor Tag and Button Versions of Mail To</h1>

<h2>The anchor tag version</h2>
<a href="mailto:test@test.com?subject=Test%20Email%20Subject&body=Great,%20the%20mailto%20protocol%20works%20and%20you%27re%20good%20to%20go.%20Good%20luck.">test mail to using href</a>


<h2>the button version</h2>
<button type="button" id="sendEmailButton">test mail to using button</button>
srsheldon
  • 26
  • 2