如何通过具有自定义标题的Amazon SES服务发送电子邮件?(例如,我想在电子邮件中附加一个文件)
这里似乎有一个JavaScript AWS SDK - http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/frames.html,但是通过浏览器包含它不工作,因为SES(尚未)不可用,并且似乎太复杂了,无法使其在后端与node和meteor一起工作。
使用nodemailer
可能更容易
mrt add nodemailer
服务器端代码
var transport = nodemailer.createTransport("SES", {
AWSAccessKeyID: "AWSACCESSKEY",
AWSSecretKey: "AWS/Secret/key"
});
transport.sendMail({
to: '',
from: '',
headers : ['one', 'two'],
text: 'body here',
html: 'html here',
subject: 'subject'
attachments : [
{
filename: "Filename.jpg",
filePath: "<path to file on your server>"
}
]
}, function(err, result) {
console.log(err,result);
});
transport.close();
在https://github.com/andris9/Nodemailer
您在正确的轨道上,但最终您需要在服务器端使用HTTP.call()
方法,以便创建对Amazon的SES服务的POST/GET
请求。
首先确保为Meteor
到Meteorite
安装了以下软件包-
-
mrt add crypto-base
-
mrt add crypto-base64
-
mrt add crypto-hmac
-
mrt add crypto-md5
-
mrt add crypto-sha1
-
mrt add crypto-sha256
还要确保您有http
流星包meteor add http
。
其次,获得您的AWS AccessKeyId
, secretAccessKey
组合-只有keyID
在亚马逊上显示,如果您不记得secretKey
,您只需重新生成它们。
var url = 'https://email.us-east-1.amazonaws.com',
awsAKID = '<Access Key ID goes here>',
secretAccessKey = '<Secret Access Key goes here>',
signature = '',
currentDate = new Date().toUTCString(),
xAmznHeader = '',
emailContent = '',
resultSend = '-1';
// Encrypt the currentDate with your secretAccessKey
// afterwards encode it in base64
// Note: that you can substitute the SHA256 with SHA1 for lower encryption
signature = CryptoJS.enc.Base64.stringify(CryptoJS.HmacSHA256(currentDate, secretAccessKey));
// this is the POST request header that Amazon uses to verify the validity of your request
xAmznHeader = 'AWS3-HTTPS AWSAccessKeyId=' + awsAKID + ', Algorithm=HmacSHA256, Signature=' + signature;
emailContent =
'From: <your verified sender e-mail here>n' +
'To: <wherever you want to send it>n' +
'Date: ' + currentDate +'n' +
'Subject: Hi Theren' +
'MIME-Version: 1.0n' +
'Content-Type: multipart/mixed; boundary="someBoundaryNameHere"n' +
'--someBoundaryNameHeren' +
'Content-Transfer-Encoding: 7bitnn' +
// body starts here, SES wants you to seperate the header from the body
//with an empty line, notice the extra 'n' above
'Readable text heren' +
'--someBoundaryNameHeren' +
'Content-Type: application/xml;n' +
xmlString +
'rn';
// now we base64 encode the whole message, that's how Amazon wants it
emailContent = CryptoJS.enc.Base64.stringify(CryptoJS.enc.Utf8.parse(emailContent));
// we can finally make the POST request
resultSend = HTTP.call("POST", url, {
params: {
Action : "SendRawEmail",
"RawMessage.Data" : emailContent
},
headers : {
'Date' : currentDate,
'X-Amzn-Authorization' : xAmznHeader
}
});
您可以通过在aws Builder中创建自己的缩小版本来包含aws .js。将这个.js文件包含在你的html页面&你可以参考我的答案从这里调用sendRawEmail代码从javascript客户端本身。
AWS.config.update({region: '<your_region>'});
AWS.config.update({apiVersion: '2010-12-01'});
var ses = new window.AWS.SES({"accessKeyId": "your_access_key", "secretAccessKey": "your_secret_key", "region": "<your_region>"});