无服务器 Mailgun 不生成邮件
Serverless Mailgun not generating mail
我想尝试一下 Serverless,看看我是否可以生成一个函数来通过 Mailgun 发送消息。我的函数成功运行并显示消息“Go Serverless v1.0!您的函数已成功执行!”但没有通过 Mailgun 发送消息:
我的handler.js:
'use strict';
var mailgun = require('mailgun-js')({apiKey: 'xxx', domain: 'email.mydomain.co.uk'})
module.exports.hello = async event => {
var data = {
from: 'no-reply@email.mydomain.co.uk',
to: 'me@somewhere.co.uk',
subject: 'Hello',
text: 'Testing some Mailgun awesomeness!'
};
mailgun.messages().send(data, function (error, body) {
if(error)
{
console.log(error)
}
console.log(body);
});
return {
statusCode: 200,
body: JSON.stringify('Go Serverless v1.0! Your function executed successfully!')
};
};
我的serverless.yml很简单:
service: helloworld
provider:
name: aws
runtime: nodejs12.x
region: eu-west-2
functions:
hello:
handler: handler.hello
events:
- http:
path: hello
method: post
我已经在 AWS 中使用 curl 和 UI 测试了该功能,但均未提供任何与 mailgun 相关的调试消息。
我怀疑,由于 .send()
方法是异步的,您的处理程序没有等待足够长的时间并在消息完成之前完成 运行。
Return 一个承诺(mailgun-js
的 API 已经产生了承诺,你只需要 return 它们):
module.exports.hello = event => {
return mailgun.messages().send({
from: 'no-reply@email.mydomain.co.uk',
to: 'me@somewhere.co.uk',
subject: 'Hello',
text: 'Testing some Mailgun awesomeness!'
}).then(msgBody => {
statusCode: 200,
body: JSON.stringify({status: 'sent a message!', text: msgBody})
});
};
当您 return 自己做出实际承诺时,async
关键字就变得多余了。你可以用 async
/await
风格重写同样的东西:
module.exports.hello = async event => {
var msgBody = await mailgun.messages().send({
from: 'no-reply@email.mydomain.co.uk',
to: 'me@somewhere.co.uk',
subject: 'Hello',
text: 'Testing some Mailgun awesomeness!'
});
return {
statusCode: 200,
body: JSON.stringify({status: 'sent a message!', text: msgBody})
};
};
我想尝试一下 Serverless,看看我是否可以生成一个函数来通过 Mailgun 发送消息。我的函数成功运行并显示消息“Go Serverless v1.0!您的函数已成功执行!”但没有通过 Mailgun 发送消息:
我的handler.js:
'use strict';
var mailgun = require('mailgun-js')({apiKey: 'xxx', domain: 'email.mydomain.co.uk'})
module.exports.hello = async event => {
var data = {
from: 'no-reply@email.mydomain.co.uk',
to: 'me@somewhere.co.uk',
subject: 'Hello',
text: 'Testing some Mailgun awesomeness!'
};
mailgun.messages().send(data, function (error, body) {
if(error)
{
console.log(error)
}
console.log(body);
});
return {
statusCode: 200,
body: JSON.stringify('Go Serverless v1.0! Your function executed successfully!')
};
};
我的serverless.yml很简单:
service: helloworld
provider:
name: aws
runtime: nodejs12.x
region: eu-west-2
functions:
hello:
handler: handler.hello
events:
- http:
path: hello
method: post
我已经在 AWS 中使用 curl 和 UI 测试了该功能,但均未提供任何与 mailgun 相关的调试消息。
我怀疑,由于 .send()
方法是异步的,您的处理程序没有等待足够长的时间并在消息完成之前完成 运行。
Return 一个承诺(mailgun-js
的 API 已经产生了承诺,你只需要 return 它们):
module.exports.hello = event => {
return mailgun.messages().send({
from: 'no-reply@email.mydomain.co.uk',
to: 'me@somewhere.co.uk',
subject: 'Hello',
text: 'Testing some Mailgun awesomeness!'
}).then(msgBody => {
statusCode: 200,
body: JSON.stringify({status: 'sent a message!', text: msgBody})
});
};
当您 return 自己做出实际承诺时,async
关键字就变得多余了。你可以用 async
/await
风格重写同样的东西:
module.exports.hello = async event => {
var msgBody = await mailgun.messages().send({
from: 'no-reply@email.mydomain.co.uk',
to: 'me@somewhere.co.uk',
subject: 'Hello',
text: 'Testing some Mailgun awesomeness!'
});
return {
statusCode: 200,
body: JSON.stringify({status: 'sent a message!', text: msgBody})
};
};