node.jsamazon-web-servicesamazon-simple-email-service

How to send attachment using AWS SDK SES


I'm having some issue using AWS SDK SES.

I have a Lambda whose goal is to generate a file and send it in csv format.

After creating my AWS Lambda and writing the code to fetch data, i checked the Aws docs to send an email with attachment and wrote the following function:

function sendEmail (textAttachment) {
        const SENDER = 'sender_email@domain.com';
        const RECEIVER = 'receiver_email@domain.com';

        let buff = new Buffer(textAttachment);
        let base64data = buff.toString('base64');

        let Data = `
            From: "Sender Name" ${SENDER}
            To: ${RECEIVER}
            Subject: "Activity export"
            Content-Type: text/plain; name="export.txt";
            Content-Disposition: attachment;filename="export.txt"
                creation-date="${new Date()}";
            Content-Transfer-Encoding: base64
            ${base64data}
        `
        var params = {
            Destinations: [ RECEIVER ],
            RawMessage: { Data },
            Source: SENDER
        };
        return ses.sendRawEmail(params).promise();
    }

Unfortunately, even tho i receive the email, I'm having differents issues:

1) the whole text is in the body and not within an attachment
2) the email has no subject (minor issue)
3) the enconding doesn't seems to work properly (i get the encoded text, but I could just avoid the encoding)

Clearly the first point is the main issue i'd like to solve, but if you can help with the others 2 point I'd appreciate :)


Solution

  • The RawMessage needs to be base64 encoded, not the file data.

        var params = {
            Destinations: [ RECEIVER ],
            RawMessage: { Buffer.from(Data).toString('base64') },
            Source: SENDER
        };