zoukankan      html  css  js  c++  java
  • mongodb nodemailer

    npm i --save nodemailer
    
    
    var nodemailer = require("nodemailer");
    
    
    app.get("/sendEmail",function(req,res){
    	var mailTrans = nodemailer.createTransport({
    		service:'Gmail',
    		auth:{
    			user:'xxx@gmail.com',
    			pass:xxx
    		},
    		port:587,
    		secure: false,
    		tls:{
    			rejectUnauthorized:false
    		}
    	});
    	mailTrans.sendMail({
    		from:'xx@163.com',
    		to:'xxx@163.com',
    		subject:'my subject is here',
    		text:'thank your for your letter !'
    	},function(err){
    		if(err) console.log('unabled to send email'+err);
    	});
    	res.json({code:0})
    })
    
    
    

    question:

    Reference:
    https://stackoverflow.com/questions/31473292/etimedout-connect-error-using-nodemailer-in-nodejs-openshift-application
    http://masashi-k.blogspot.com/2013/06/sending-mail-with-gmail-using-xoauth2.html
    https://nodemailer.com/about/


    update again .haha

    app.get("/sendEmail",function(req,res){
    nodemailer.createTestAccount((err, account) => {
        // create reusable transporter object using the default SMTP transport
        let transporter = nodemailer.createTransport({
                host: "smtp.163.com",
                secureConnection: true,
                port:465,
            auth: {
                user: 'xxx@163.com', // generated ethereal user
                pass: '' // generated ethereal password(授权码)
            }
        });
    
        // setup email data with unicode symbols
        let mailOptions = {
            from: 'xxx@163.com', // sender address(注意和上面的要一致)
            to: 'xxx@qq.com', // list of receivers
            subject: 'Hello ✔', // Subject line
            text: 'Hello world?', // plain text body
            html: `<div>
            <h2 style="font-size:30px;color:#53ff53;">haha</h2>
            <a href="http://www.cyany.com">1345648</a>`, // html body
            attachments:[
            	{
            		filename:'text.txt',
            		content:'hello,world'
            	},
            	{
            		filename:'text.txt',
            		path:'text.txt'
            	}
            ]
        };
    
        transporter.verify(function(error, success) {
       if (error) {
            console.log(error);
       } else {
            console.log('Server is ready to take our messages');
       }
    });
        // send mail with defined transport object
        transporter.sendMail(mailOptions, (error, info) => {
            if (error) {
                return console.log(error);
            }
            console.log('Message sent: %s', info.messageId);
            // Preview only available when sending through an Ethereal account
            console.log('Preview URL: %s', nodemailer.getTestMessageUrl(info));
    
            // Message sent: <b658f8ca-6296-ccf4-8306-87d57a0b4321@example.com>
            // Preview URL: https://ethereal.email/message/WaQKMgKddxQDoou...
        });
    });
    res.json({code:0})
    })
    

    https://nodemailer.com/message/attachments/

  • 相关阅读:
    神马搜索 面试小结
    我的第一篇paper
    【转载】技巧:Vim 的纵向编辑模式
    实习求职小结
    将博客园界面打造成Hexo经典主题Light
    试一下Markdown
    四色标记算法
    射雕三部曲的优美片段
    Docker
    Sublime Text 3 文档
  • 原文地址:https://www.cnblogs.com/cyany/p/9973791.html
Copyright © 2011-2022 走看看