最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

如何在变量中存储电子邮件并使用node.js发送

运维笔记admin19浏览0评论

如何在变量中存储电子邮件并使用node.js发送

如何在变量中存储电子邮件并使用node.js发送

const functions = require('firebase-functions');
var nodemailer = require('nodemailer');

var transporter=nodemailer.createTransport('smtps://[email protected]:[email protected]');
exports.sendMail=functions.https.onRequest((req,res)=>{
	var mailOptions={
		to: '[email protected]',
		subject: 'Test Mail',
		html: 'Testing with Node.js'
	}
	transporter.sendMail(mailOptions,function(err,response){
		if(err)
		{
			res.send('Mail not sent');
			console.log(err);
		}
		else{
			res.send('Mail sent');
		}
	});
});
回答如下:

您需要设置一个正在监听我们其他JS应用程序的快速服务器,如下所示:

var express  = require('express');
var app      = express();  

现在,您可以从指定端口上的其他应用程序收听:

var port = process.env.PORT || 8080;
app.listen(port);
console.log("App listening on port " + port);

最后,您必须拦截http post请求以发送您的邮件并从请求中获取用户信息:

app.post('/api/sendmail', function(req, res) {
    var mail = {
                from: req.body.user.emailFrom,
                to: req.body.user.emailTo,
                subject: "your subject",
                html: "<p>email body</p>"
                }



    transporter.sendMail(mail, function(error, response){
                    if(error){
                        console.log("Error!");
                        console.log(error);
                        res.send(JSON.stringify(error));
                    }else{
                        res.send(JSON.stringify(response));
                        console.log("Success!")
                        transporter.close();
                    }

    }

您的服务器现已准备就绪,您可以使用以下命

node serverName.js

在你的其他js文件(assmt.js)中,你发送带有参数的http请求:

send(user : User) {
    let body = {user};
     this.httpClient.post('http://localhost:8080/api/sendconge', body)
    .subscribe(function(data) {
            console.log(data);
    });
}
发布评论

评论列表(0)

  1. 暂无评论