Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nodejs实现发送邮件 #20

Open
ChenJiaH opened this issue Aug 2, 2019 · 0 comments
Open

Nodejs实现发送邮件 #20

ChenJiaH opened this issue Aug 2, 2019 · 0 comments
Labels

Comments

@ChenJiaH
Copy link
Owner

ChenJiaH commented Aug 2, 2019

首发于微信公众号《前端成长记》,写于 2017.01.04

背景

在日常工作中,一些监控系统都支持预警邮件,即当页面的某些指标达到阀值的时候,自动发送邮件给相关负责人。

团队站项目想实现一个定制化邮件发送的功能,在不同的时期给不同的人发送邮件。

核心代码

由于本项目开发环境为Node环境,故去github上找了一个封装好的邮件发送模块 nodemailer

安装

npm install nodemailer

使用

var nodemailer = require('nodemailer');
 
var config = {
    // service: 'jd',
    // pool: true,
    host: 'smtp.jd.com',
    ignoreTLS: true,
    port: 25,
    // secure: true, // use SSL
    // debug: true,
    auth: {
        user: '******',
        pass: '******'
    },
    logger: true
};
 
// create reusable transporter object using the default SMTP transport
var transporter = nodemailer.createTransport(config);
 
// verify connection configuration
transporter.verify(function(error, success) {
    if (error) {
        console.log(error);
    } else {
        console.log('Server is ready to take our messages');
    }
});
// setup e-mail data with unicode symbols
var mailOptions = {
    from: '[email protected]', // sender address
    to: '[email protected], [email protected]', // list of receivers
    subject: '测试邮件标题20170104', // Subject line
     // text: 'Hello world11', // plaintext body
    html: '<b>测试邮件内容20170104</b>' // html body
};
// send mail with defined transport object
transporter.sendMail(mailOptions, function(error, info){
    if(error){
        return console.log(error);
    }
    console.log('Message sent: ' + info.response);
});

常见错误

  1. ECONNECTION
{ [Error: 101057795:error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol:openssl\ssl\s23_clnt.c:794:
] code: 'ECONNECTION', command: 'CONN' }

** 解决方案 ** jd邮箱不需要设置 secure: true

  1. ECONNREFUSED
{ [Error: connect ECONNREFUSED 127.0.0.1:25]
  code: 'ECONNECTION',
  errno: 'ECONNREFUSED',
  syscall: 'connect',
  address: '127.0.0.1',
  port: 25,
  command: 'CONN' }

** 解决方案 ** jd邮箱不在service范围内,需要使用 host+auth+port 配置,而非service

  1. UNABLE_TO_VERIFY_LEAF_SIGNATURE
{ [Error: unable to verify the first certificate] code: 'UNABLE_TO_VERIFY_LEAF_SIGNATURE' }

** 解决方案 ** jd邮箱配置时需要带上 ignoreTLS:true

  1. EAUTH
{ [Error: Invalid login: 535 5.7.3 Authentication unsuccessful]
  code: 'EAUTH',
  response: '535 5.7.3 Authentication unsuccessful',
  responseCode: 535,
  command: 'AUTH LOGIN' }

** 解决方案 ** jd邮箱配置auth时user不能带@jd.com

其他

如果您的开发过程中遇到不在列表中的坑,可以添加到列表中,感谢!

(完)


本文为原创文章,可能会更新知识点及修正错误,因此转载请保留原出处,方便溯源,避免陈旧错误知识的误导,同时有更好的阅读体验
如果能给您带去些许帮助,欢迎 ⭐️star 或 ✏️ fork
(转载请注明出处:https://chenjiahao.xyz)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant