使用Meteor的AWS SendRawEmail

时间:2014-07-03 03:14:44

标签: javascript jquery amazon-web-services meteor amazon-ses

如何通过具有自定义标头的亚马逊SES服务发送电子邮件? (即我想在电子邮件中附上一个文件)

似乎这里有一个JavaScript AWS SDK - http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/frames.html,但是通过浏览器包含它并不起作用,因为SES现在还不可用而且它看起来太复杂了在后端有节点和流星。

3 个答案:

答案 0 :(得分:1)

使用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

答案 1 :(得分:0)

您走在正确的轨道上,但最终您需要在服务器端使用HTTP.call()方法,以便向亚马逊的SES服务创建POST/GET请求。< / p>

首先确保为MeteorMeteorite -

安装了以下软件包
  • 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软件包meteor add http

其次获取您的AWS AccessKeyIdsecretAccessKey组合 - 只有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 There\n' +
    'MIME-Version: 1.0\n' +
    'Content-Type: multipart/mixed; boundary="someBoundaryNameHere"\n' +
    '--someBoundaryNameHere\n' +
    'Content-Transfer-Encoding: 7bit\n\n' +
    // body starts here, SES wants you to seperate the header from the body 
    //with an empty line, notice the extra '\n' above
    'Readable text here\n' +
    '--someBoundaryNameHere\n' +
    'Content-Type: application/xml;\n' +
    xmlString +
    '\r\n';

// 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
    }
});

答案 2 :(得分:0)

您可以通过从AWS Builder创建自己的缩小版本来包含aws .js。 在您的html页面中包含此.js文件&amp;你可以从here引用我的答案来调用javascript客户端本身的sendRawEmail代码。

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>"});