1

According to Sending emails with Javascript, one way to do it is the following:

function sendMail() {
    var link = "mailto:[email protected]"
             + "[email protected]"
             + "&subject=" + encodeURIComponent("This is my subject")
             + "&body=" + encodeURIComponent(document.getElementById('myText').value)
    ;
    window.location.href = link;
}

However I'm trying to personalize it, sending emails with variables. I did this:

function sendMail(subject="test", body, mail="[email protected]") {
  var link = `mailto:${mail}`
           + "&subject=" + encodeURIComponent(`${subject}`)
           + "&body=" + encodeURIComponent(`${body}`)
  ;
  window.location.href = link;

But, when sending the email, I achieve this fail:

fail (image)

It seems like it is not recognizing each variable. How to solve it?

2
  • 4
    You're missing the ?, which separates the URL from the query string. Also, a better way to construct a full URL with query string is to use the new URL() object Commented Aug 18, 2021 at 11:53
  • Note that you can't rely on your visitor having a mail client installed and working that mailto: can use. I don't have one and mailto: links just don't work for me. If you want to be able to send email from a form pass the message to your server and have that send the message. Commented Aug 18, 2021 at 12:00

3 Answers 3

1

The query string should be start with ? not &

change &subject to ?subject

it should be //[email protected]?subject=blahblahblah&body=testtest

Sign up to request clarification or add additional context in comments.

Comments

1

The link have to be like that :

var link = `mailto:${mail}`
           + "?subject=" + encodeURIComponent(`${subject}`)
           + "&body=" + encodeURIComponent(`${body}`);

Comments

0

Just try this code for sending email. I am sure it will work..

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <script src="https://smtpjs.com/v3/smtp.js"></script>  
    <script type="text/javascript">
        function sendEmail() {
            Email.send({
                Host: "smtp.gmail.com",
                Username : "<sender’s email address>",
                Password : "<email password>",
                To : '<recipient’s email address>',
                From : "<sender’s email address>",
                Subject : "<email subject>",
                Body : "<email body>",
            })
            .then(function(message){
                alert("mail sent successfully")
            });
        }
    </script>
</head>
<body>  
    <form method="post">
        <input type="button" value="Send Email" onclick="sendEmail()"/>
    </form>  
</body>

And just remember if you are using JavaScript in any other file then just link it will your HTML file! And this is the not fully coded by me I have done some research from internet.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.