SmtpJS a few lines of code to implement javascript to send mail (record)


foreword

On the premise of not considering security, the function of sending emails can be realized directly without using javascript through background means


1. What is SmtpJS?

SmtpJS is a plug-in for sending mail through javascript code. After importing the plug-in, it only takes a few lines of code to realize the mail sending function
SmtpJS official website

Two, use

Ready to send email (From sender email)

The sender’s mailbox must be a mailbox with POP3/SMTP service enabled (you can use other client software to send and receive emails by setting POP3/SMTP). The mailbox opening method of each platform is similar, and you can check the opening method online. Example: I use Tencent Enterprise Mailbox to activate POP3/SMTP service

insert image description here

Import SmtpJs using

1. You can download the SmtpJs plug-in from the official website and directly import it locally.
2. Use cdn to import it

<script src="https://smtpjs.com/v3/smtp.js"></script>
<script>
Email.send({
    
    
  Host : "smtp.exmail.qq.com",// 邮箱开启POP3/SMTP服务时对应的发送服务器 smtp.exmail.qq.com对应腾讯企业邮箱发送服务器 
  Username : "发件人邮箱",
  Password : "密码",
  To: '收件人邮箱',
  From: "发件人邮箱",
  Subject: "主题",
  Body: `<span>啊哈:邮件内容</span>`
}).then(
  message => {
    
    
    if (message == 'OK') {
    
    
      // 成功发送了邮件
    } else {
    
    
		console.error(message)
	}
  }
);
</script>

Error-prone

1. The call parameter Username must be consistent with From
2. The password parameter is not the password of the mailbox directly. For example, the Tencent corporate mailbox uses a client-specific password, and the qq mailbox uses the authorization code obtained when the POP3/SMTP service is enabled ( obtain 3. Host
must use the smtp sending server address corresponding to the sender's mailbox, otherwise an error will be reported

Safeguard


We've thought of that, so instead you can encrypt your SMTP credentials, and lock it to a single domain, and pass a secure token instead of the credentials instead It locks down to a single domain and passes security tokens instead of credentials

If you don't want your sending server, username, and password to be directly exposed in the code, you can follow the steps below to replace them with the SecureToken specified by
insert image description here
insert image description here
SmtoJS

<script src="https://smtpjs.com/v3/smtp.js"></script>
<script>
Email.send({
    
    
  SecureToken : "C973D7AD-F097-4B95-91F4-40ABC5567812",//获取到的token
  To: '收件人邮箱',
  From: "发件人邮箱",
  Subject: "主题",
  Body: `<span>啊哈:邮件内容</span>`
}).then(
  message => {
    
    
    if (message == 'OK') {
    
    
      // 成功发送了邮件
    } else {
    
    
		console.error(message)
	}
  }
);
</script>

Solve the error bootbox is not defined when obtaining SecureToken

Its official website reported an error when getting the token... It should be my network problem that caused the bootbox not to be loaded.
insert image description here
1. Use its default SMTP Host parameter to send it and then get its request address
insert image description here

2. Postman to understand
insert image description here

Attach the port of each mailbox and the address of the smtp sending server

insert image description here
Screenshot source: https://blog.csdn.net/qq_45034708/article/details/106600049

Guess you like

Origin blog.csdn.net/weixin_42508580/article/details/123569829