使用parse-server 2.2.6,如何在云代码中使用SimpleMailgunAdapter?
我像这样初始化我的parseserver:
var api = new ParseServer({
databaseURI: databaseUri,
cloud: process.env.CLOUD_CODE_MAIN,
appId: process.env.APP_ID || 'myAppId',
masterKey: process.env.MASTER_KEY || '', //Add your master key here. Keep it secret!
serverURL: process.env.SERVER_URL, // Don't forget to change to https if needed
appName: 'AppName',
publicServerURL: process.env.SERVER_URL,
verifyUserEmails: true,
emailAdapter: {
module: 'parse-server-simple-mailgun-adapter',
options: {
fromAddress: 'mymail@mail.com',
apiKey: process.env.MAILGUN_API_KEY,
domain: process.env.MAILGUN_DOMAIN
}
}
});
这是有效的(即,验证电子邮件已发送)
现在我想从云代码发送电子邮件,而无需创建新的SimpleMailgunAdapter。
我今天使用的是什么:
var SimpleMailgunAdapter = require('parse-server-simple-mailgun-adapter');
var adapter = SimpleMailgunAdapter({
apiKey: process.env.MAILGUN_API_KEY,
domain: process.env.MAILGUN_DOMAIN,
fromAddress: 'mymail@email.com'
})
这很有效。但我觉得最好的方法是从我的parse-instance中获取已经初始化的邮件适配器。但我不知道该怎么做。这会被认为是最佳做法,还是我可以坚持我的解决方案?
答案 0 :(得分:2)
您可以使用mailgun从您的云代码发送电子邮件。
首先,您需要安装npm模块[https://www.npmjs.com/package/mailgun-js]
使用此命令:
npm install mailgun-js --save
然后更新您的服务器(我使用AWS elasticbeanstalk)
然后在你的main.js文件上,通过
初始化mailgunvar mailgun = require('mailgun-js')({apiKey: 'XXX', domain: 'XXX.com'});
然后创建一个云代码功能
Parse.Cloud.define("sendEmail", function(request, response) {
console.log("sendEmail " + new Date());
var emailBody = request.params.emailBody;
var emailSubject = request.params.emailSubject;
var fromName = request.params.fromName;
var fromEmail = request.params.fromEmail;
var toEmail = request.params.toEmail;
var toName = request.params.toName;
var fromString = fromName + " <"+fromEmail+">";
var toString = toName + " <"+toEmail+">"
console.log("emailBody " + emailBody);
console.log("emailSubject " + emailSubject);
console.log("fromName " + fromName);
console.log("fromEmail " + fromEmail);
console.log("toEmail " + toEmail);
console.log("toName " + toName);
var data = {
from: fromString,
to: toString,
subject: emailSubject,
html: emailBody
};
mailgun.messages().send(data, function (error, body) {
if (error) {
console.log("got an error in sendEmail: " + error);
response.error(err);
}else {
console.log("email sent to " + toEmail + " " + new Date());
response.success("Email sent!");
}
});
});