简体   繁体   English

用 Javascript 发送电子邮件

[英]Sending email in Javascript

According to Sending emails with Javascript , one way to do it is the following:根据Sending emails with Javascript ,一种方法如下:

function sendMail() {
    var link = "mailto:me@example.com"
             + "?cc=myCCaddress@example.com"
             + "&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="test@gmail.com") {
  var link = `mailto:${mail}`
           + "&subject=" + encodeURIComponent(`${subject}`)
           + "&body=" + encodeURIComponent(`${body}`)
  ;
  window.location.href = link;

But, when sending the email, I achieve this fail:但是,在发送电子邮件时,我失败了:

失败(图片)

It seems like it is not recognizing each variable.似乎它没有识别每个变量。 How to solve it?如何解决?

The link have to be like that :链接必须是这样的:

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

The query string should be start with ?查询字符串应该以 ? not &不是 &

change &subject to ?subject将 &subject 更改为 ?subject

it should be //abc@example.com?subject=blahblahblah&body=testtest应该是 //abc@example.com?subject=blahblahblah&body=testtest

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!请记住,如果您在任何其他文件中使用 JavaScript,那么只需将其链接到您的 HTML 文件即可! And this is the not fully coded by me I have done some research from internet.这是我没有完全编码的我从互联网上做了一些研究。 Hope it helps you!希望对你有帮助! Happy Coding快乐编码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM