使用来自node.js的AWS SES在邮件中上传.jpg图像附件 [英] upload .jpg image attachment in mail using AWS SES from node.js

查看:74
本文介绍了使用来自node.js的AWS SES在邮件中上传.jpg图像附件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是来自 https://github.com/andrewpuch/aws-ses -node-js-examples ,其中有一个带有附件发送和发送电子邮件的示例,

Below is the code from https://github.com/andrewpuch/aws-ses-node-js-examples where there is an example to send and email with attachment,

我修改了代码以从aws s3中获取图像文件并以邮件作为附件发送,当我将其作为文本文件时,它可以正常工作,但是当我发送图像时,我在邮件中图片已损坏,因此无法查看.

I have modified the code to fetch a image file from aws s3 and and send it with mail as attachment, when i did it for an text file it work perfectly, but when I have sent an image, in the mail I was not able to see the image since it is corrupted.

当我尝试使用Apple Photo App打开时,它表明缺少元数据,当我尝试使用utf8,utf-8和UTF-时,我还在邮件标题中添加了Content-Transfer-Encoding:base64.在标题的 Content-Transfer-Encoding 中的8中,我收到了来自AWS的以下响应

when I tried opening with apple photo app it has shown that meta data is missing, also I have added Content-Transfer-Encoding: base64 in the header of the mail, when I tried with utf8, utf-8 and UTF-8 in Content-Transfer-Encodingin the header I got the below response from aws

{
  "message": "Unknown encoding: utf8",
  "code": "InvalidParameterValue",
  "time": "2017-03-14T08:42:43.571Z",
  "requestId": "2e220c33-0892-11e7-8a5a-1114bbc28c3e",
  "statusCode": 400,
  "retryable": false,
  "retryDelay": 29.798455792479217
}

我修改后的代码发送带有邮件的图像附件,我什至尝试将缓冲区编码为utf-8,base-64,为此花费了足够的时间,不知道为什么它损坏了,如果有人以前做过的话,帮帮我

My modified code to send an image attachment with mail, I even tried encoding the buffer to utf-8, base-64,wasted enough time on this, no clue why it is corrupted, if some one have done this before, help me

// Require objects.
var express = require('express');
var app = express();
var aws = require('aws-sdk');

// Edit this with YOUR email address.
var email = "*******@gmail.com";

// Load your AWS credentials and try to instantiate the object.
aws.config.loadFromPath(__dirname + '/config.json');

// Instantiate SES.
var ses = new aws.SES();
var s3 = new aws.S3();

// Verify email addresses.
app.get('/verify', function (req, res) {
    var params = {
        EmailAddress: email
    };

    ses.verifyEmailAddress(params, function (err, data) {
        if (err) {
            res.send(err);
        }
        else {
            res.send(data);
        }
    });
});

// Listing the verified email addresses.
app.get('/list', function (req, res) {
    ses.listVerifiedEmailAddresses(function (err, data) {
        if (err) {
            res.send(err);
        }
        else {
            res.send(data);
        }
    });
});

// Deleting verified email addresses.
app.get('/delete', function (req, res) {
    var params = {
        EmailAddress: email
    };

    ses.deleteVerifiedEmailAddress(params, function (err, data) {
        if (err) {
            res.send(err);
        }
        else {
            res.send(data);
        }
    });
});

// Sending RAW email including an attachment.
app.get('/send', function (req, res) {
    var params = { Bucket: 's3mailattachments', Key: 'aadhar.jpg' };
    var attachmentData;
    s3.getObject(params, function (err, data) {
        if (err)
            console.log(err, err.stack); // an error occurred
        else {
            console.log(data.ContentLength);
            console.log(data.ContentType);
            console.log(data.Body);
            var ses_mail = "From: 'AWS Tutorial Series' <" + email + ">\n";
            ses_mail = ses_mail + "To: " + email + "\n";
            ses_mail = ses_mail + "Subject: AWS SES Attachment Example\n";
            ses_mail = ses_mail + "MIME-Version: 1.0\n";
            ses_mail = ses_mail + "Content-Type: multipart/mixed; boundary=\"NextPart\"\n\n";
            ses_mail = ses_mail + "--NextPart\n";
            ses_mail = ses_mail + "Content-Type: text/html; charset=us-ascii\n\n";
            ses_mail = ses_mail + "This is the body of the email.\n\n";
            ses_mail = ses_mail + "--NextPart\n";
            ses_mail = ses_mail + "Content-Type: image/jpeg; \n";
            ses_mail = ses_mail + "Content-Disposition: attachment; filename=\"aadhar.jpg\"\n";
            ses_mail = ses_mail + "Content-Transfer-Encoding: base64\n\n"
            ses_mail = ses_mail + data.Body;
            ses_mail = ses_mail + "--NextPart";


            var params = {
                RawMessage: { Data: new Buffer(ses_mail) },
                Destinations: [email],
                Source: "'AWS Tutorial Series' <" + email + ">'"
            };

            ses.sendRawEmail(params, function (err, data) {
                if (err) {
                    res.send(err);
                }
                else {
                    res.send(data);
                }
            });

        }
    });
});

// Start server.
var server = app.listen(3003, function () {
    var host = server.address().address;
    var port = server.address().port;

    console.log('AWS SES example app listening at http://%s:%s', host, port);
});

推荐答案

首先,您的MIME消息格式不正确.最后一行应该是--NextPart--而不是--NextPart.

First, your MIME message is not well formatted. The last line should be --NextPart-- instead of just --NextPart.

您还应该使用new Buffer(data.Body).toString('base64')data.Body数组转换为其base64字符串表示形式,如下所示:

You should also convert the data.Body array into its base64 string representation using new Buffer(data.Body).toString('base64') as shown below:

var ses_mail = "From: 'AWS Tutorial Series' <" + email + ">\n";
ses_mail += "To: " + email + "\n";
ses_mail += "Subject: AWS SES Attachment Example\n";
ses_mail += "MIME-Version: 1.0\n";
ses_mail += "Content-Type: multipart/mixed; boundary=\"NextPart\"\n\n";
ses_mail += "--NextPart\n";
ses_mail += "Content-Type: text/html; charset=us-ascii\n\n";
ses_mail += "This is the body of the email.\n\n";
ses_mail += "--NextPart\n";
ses_mail += "Content-Type: image/jpeg; \n";
ses_mail += "Content-Disposition: attachment; filename=\"aadhar.jpg\"\n";
ses_mail += "Content-Transfer-Encoding: base64\n\n"
ses_mail += new Buffer(data.Body).toString('base64');
ses_mail += "--NextPart--";

然后,您可以将ses_mail字符串作为原始消息数据传递为RawMessage: { Data: ses_mail }而不是RawMessage: { Data: new Buffer(ses_mail) }.

Then, you can pass the ses_mail string as raw message data as RawMessage: { Data: ses_mail } instead of RawMessage: { Data: new Buffer(ses_mail) }.

这篇关于使用来自node.js的AWS SES在邮件中上传.jpg图像附件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆