Last active
July 7, 2023 08:21
-
-
Save diegofcornejo/c18f52ba66bff60c947ebf252c3a1397 to your computer and use it in GitHub Desktop.
AWS SES send raw email with attachment
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
const AWS = require('aws-sdk'); | |
const EMAIL = require('./mail.js'); | |
exports.handler = (event, context, callback) => { | |
//HERE YOUR CODE | |
EMAIL.send(); | |
callback(null, ':)'); | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use strict"; | |
const AWS = require('aws-sdk'); | |
const SES = new AWS.SES({ | |
apiVersion: '2010-12-01' | |
}); | |
var sender = "[email protected]"; | |
var recipient = "[email protected]"; | |
//This should be your file colors.json | |
var json = { | |
"colors": [{ | |
"color": "black", | |
"category": "hue", | |
"type": "primary", | |
"code": { | |
"rgba": [255, 255, 255, 1], | |
"hex": "#000" | |
} | |
}, | |
{ | |
"color": "white", | |
"category": "value", | |
"code": { | |
"rgba": [0, 0, 0, 1], | |
"hex": "#FFF" | |
} | |
}, | |
{ | |
"color": "red", | |
"category": "hue", | |
"type": "primary", | |
"code": { | |
"rgba": [255, 0, 0, 1], | |
"hex": "#FF0" | |
} | |
}, | |
{ | |
"color": "blue", | |
"category": "hue", | |
"type": "primary", | |
"code": { | |
"rgba": [0, 0, 255, 1], | |
"hex": "#00F" | |
} | |
}, | |
{ | |
"color": "yellow", | |
"category": "hue", | |
"type": "primary", | |
"code": { | |
"rgba": [255, 255, 0, 1], | |
"hex": "#FF0" | |
} | |
}, | |
{ | |
"color": "green", | |
"category": "hue", | |
"type": "secondary", | |
"code": { | |
"rgba": [0, 255, 0, 1], | |
"hex": "#0F0" | |
} | |
}, | |
] | |
}; | |
//Encode your file | |
var file = new Buffer(JSON.stringify(json)).toString("base64"); | |
// var file = "ewogICJjb2xvcnMiOiBbCiAgICB7CiAgICAgICJjb2xvciI6ICJibGFjayIsCiAgICAgICJjYXRlZ29yeSI6ICJodWUiLAogICAgICAidHlwZSI6ICJwcmltYXJ5IiwKICAgICAgImNvZGUiOiB7CiAgICAgICAgInJnYmEiOiBbMjU1LDI1NSwyNTUsMV0sCiAgICAgICAgImhleCI6ICIjMDAwIgogICAgICB9CiAgICB9LAogICAgewogICAgICAiY29sb3IiOiAid2hpdGUiLAogICAgICAiY2F0ZWdvcnkiOiAidmFsdWUiLAogICAgICAiY29kZSI6IHsKICAgICAgICAicmdiYSI6IFswLDAsMCwxXSwKICAgICAgICAiaGV4IjogIiNGRkYiCiAgICAgIH0KICAgIH0sCiAgICB7CiAgICAgICJjb2xvciI6ICJyZWQiLAogICAgICAiY2F0ZWdvcnkiOiAiaHVlIiwKICAgICAgInR5cGUiOiAicHJpbWFyeSIsCiAgICAgICJjb2RlIjogewogICAgICAgICJyZ2JhIjogWzI1NSwwLDAsMV0sCiAgICAgICAgImhleCI6ICIjRkYwIgogICAgICB9CiAgICB9LAogICAgewogICAgICAiY29sb3IiOiAiYmx1ZSIsCiAgICAgICJjYXRlZ29yeSI6ICJodWUiLAogICAgICAidHlwZSI6ICJwcmltYXJ5IiwKICAgICAgImNvZGUiOiB7CiAgICAgICAgInJnYmEiOiBbMCwwLDI1NSwxXSwKICAgICAgICAiaGV4IjogIiMwMEYiCiAgICAgIH0KICAgIH0sCiAgICB7CiAgICAgICJjb2xvciI6ICJ5ZWxsb3ciLAogICAgICAiY2F0ZWdvcnkiOiAiaHVlIiwKICAgICAgInR5cGUiOiAicHJpbWFyeSIsCiAgICAgICJjb2RlIjogewogICAgICAgICJyZ2JhIjogWzI1NSwyNTUsMCwxXSwKICAgICAgICAiaGV4IjogIiNGRjAiCiAgICAgIH0KICAgIH0sCiAgICB7CiAgICAgICJjb2xvciI6ICJncmVlbiIsCiAgICAgICJjYXRlZ29yeSI6ICJodWUiLAogICAgICAidHlwZSI6ICJzZWNvbmRhcnkiLAogICAgICAiY29kZSI6IHsKICAgICAgICAicmdiYSI6IFswLDI1NSwwLDFdLAogICAgICAgICJoZXgiOiAiIzBGMCIKICAgICAgfQogICAgfSwKICBdCn0=" | |
// var ses_mail = "From: 'Diego Cornejo' <" + 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: text/plain; \n"; | |
// ses_mail += "Content-Disposition: attachment; filename=\"text.txt\"\n"; | |
// ses_mail += "Content-Transfer-Encoding: base64\n\n" | |
// ses_mail += "VGVzdCBzZW5kcmF3IGVtYWls\n"; | |
// ses_mail += "--NextPart--"; | |
var payload = `From: 'Diego Cornejo' <${sender}> | |
To: ${recipient} | |
Subject: AWS SES Attachment | |
MIME-Version: 1.0 | |
Content-Type: multipart/mixed; boundary="NextPart" | |
--NextPart | |
Content-Type: text/html; charset=us-ascii | |
This is the <b>body</b> of the email. | |
--NextPart | |
Content-Type: application/json; | |
Content-Disposition: attachment; filename="colors.json" | |
Content-Transfer-Encoding: base64 | |
${file} | |
--NextPart--`; | |
module.exports = { | |
send: function () { | |
var params = { | |
RawMessage: { | |
Data: payload | |
}, | |
Source: "[email protected]", | |
}; | |
SES.sendRawEmail(params, function (err, data) { | |
if (err) console.log(err, err.stack); // an error occurred | |
else console.log(data); // successful response | |
}); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment