SES.sendEmail in Lambda not calling Promise - node.js

When I make a call to ses.sendEmail the promise is never called. This is function in my lambda layer being called by my lambda function.
Here's the sendEmail function I have..
var aws = require ('aws-sdk')
var ses = new aws.SES ({region: 'us-west-2'});
exports.sendEmail = async (to, from, subject, body) => {
var params = {
Destination: {
ToAddresses : [to]
},
Message: {
Body: {
Html: {
Charset: "UTF-8",
Data : body
}
},
Subject: {
Charset: "UTF-8",
Data : subject
}
},
Source: from
}
console.log ("Sending email with params (stringify) : ", JSON.stringify(params));
console.log ("SES = ", ses);
await ses.sendEmail (params, function (err, data) {
console.log ("inside email");
});
}
I see the "Sending email with params (stringify)" log output and the "SES = " log output (which shows what appears to be a valid SES). But I never see "inside email" in the log nor do I receive any emails.
I'm also outside of the ses sandbox as I've gotten approval after setting up my domain and successfully verifying it all. I am on the us-west-2 region. I have not verified any email addresses. Just the domain (ie. name#user.host... user.host is verified) as I assumed if the domain is verified then any email from that domain should be good for use in the "from".

I have used async await instead of promises. I was facing the same issue. Email is dispatched successfully in my local computer but when I deploy the lambda and start testing it live. Emails were not sent to the email address mentioned.
async function sendMail(content, email) {
const params = {
Destination: {
ToAddresses: [email.toLowerCase()],
},
Message: {
Body: {
Text: {
Charset: "UTF-8",
Data: content.text,
},
Html: {
Charset: "UTF-8",
Data: content.html,
},
},
Subject: {
Charset: "UTF-8",
Data: content.subject,
},
},
Source: secretManagerData.senderEmail,
};
const sendPromise = await AWS_SES.sendEmail(params).promise();
console.log(sendPromise);
}

There are mistakes in calling the functions. When you call the function, you don't need to give a space with it.
var aws = require('aws-sdk'); // these are functions, call them without space
var ses = new aws.SES({region: 'us-west-2'}); // this
exports.sendEmail = async (to, from, subject, body) => {
try {
var params = {
Destination: {
ToAddresses : [to]
},
Message: {
Body: {
Html: {
Charset: "UTF-8",
Data : body
}
},
Subject: {
Charset: "UTF-8",
Data : subject
}
},
Source: from
}
console.log("Sending email with params (stringify) : ", JSON.stringify(params));
console.log("SES = ", ses);
const data = await sendmail(params);
return data;
} catch (e) {
console.log(e);
}
}
const sendmail = (params) => {
return new Promise((res, rej) => {
sendEmail(params = {}, (err, data)=>{
if (err) rej(err);
else res(data);
})
})
}
When you have to use async/await, you don't need to use callback and always use try/catch with async/await.

I got somehow same issue, it was resolved after used await before invoke sendEmail with promise and added API-Version for SES object.
const AWS = require ('aws-sdk')
const SES = new AWS.SES({apiVersion: '2010-12-01'});
try{
await SES.sendEmail(params).promise();
console.log("Email sent successfully");
}catch(err){
console.log("Failed to send email");
}

Related

AWS COGNITO: How to implement Post Confirmation Trigger which works only after SignupConfirmation not after ForgotPassword/ResetPassword Confirmation

Someone can please tell me, that how can send this mail after the Post Confirmation of Signup only. This code is sending mail after every confirmation whether it is forget password/reset password or Signup.
var aws = require('aws-sdk');
var ses = new aws.SES();
exports.handler = (event, context, callback) => {
console.log(event);
if (event.request.userAttributes.email) {
sendEmail(event.request.userAttributes.email, "Congratulations " + event.userName + ", you have been confirmed: ", function(status) {
// Return to Amazon Cognito
callback(null, event);
});
} else {
// Nothing to do, the user's email ID is unknown
callback(null, event);
}
};
function sendEmail(to, body, completedCallback) {
var eParams = {
Destination: {
ToAddresses: [to]
},
Message: {
Body: {
Text: {
Data: body
}
},
Subject: {
Data: "Cognito Identity Provider registration completed"
}
},
// Replace source_email with your SES validated email address
Source: "<source_email>"
};
var email = ses.sendEmail(eParams, function(err, data){
if (err) {
console.log(err);
} else {
console.log("===EMAIL SENT===");
}
completedCallback('Email sent');
});
console.log("EMAIL CODE END");
};
You can check triggerSource attribute of the event object.
Post Confirmation of Signup event will have trigger source is PostConfirmation_ConfirmSignUp
Your handle function will look like this:
exports.handler = (event, context, callback) => {
console.log(event);
if (
event.request.userAttributes.email
&& event.triggerSource === "PostConfirmation_ConfirmSignUp") {
sendEmail(event.request.userAttributes.email, "Congratulations " + event.userName + ", you have been confirmed: ", function (status) {
// Return to Amazon Cognito
callback(null, event);
});
} else {
// Nothing to do, the user's email ID is unknown
callback(null, event);
}
};
Reference: User Pool Lambda Trigger Sources

How to send an email using AWS in Node project

My Angular app sends some data to a node server (app.js) via a POST request, the request body is then returned in the response.
I am now trying to send an email that contains this data sent in the request body.
Currently, I can read a HTML file to populate the email body, & then send the email but I need to replace that HTML file with the data sent in my req.body.
Here is what I have so far in app.js:
const express = require('express')
const app = express()
const bodyParser = require('body-parser')
app.post('/postData', bodyParser.json(), (req, res) => {
res.json(req.body)
readFile();
sendEmail();
})
app.listen(3000, () => console.log('Example app listening on port 3000!'))
var AWS = require('aws-sdk');
const fs = require('fs');
var params;
var htmlFileName = '/Users/myName/Desktop/test.html'
AWS.config.loadFromPath('config-aig.json');
const fromEmail = 'myName';
const toEmail = 'myName'
const subject = 'Test Email' + Date()
function sendEmail() {
// Create the promise and SES service object
var sendPromise = new AWS.SES({ apiVersion: '2010-12-01'}).sendEmail(params).promise();
sendPromise.then(
function (data) {
console.log('send email success');
}).catch(
function (err) {
console.error('error --> ', err, err.stack);
});
}
function readFile(callback) {
return new Promise(
function (resolve, reject) {
fs.readFile(htmlFileName, 'utf8',
function read(err, data) {
if (err) {
return reject(err)
}
else {
console.log('file read success');
return resolve(data);
}
})
}
)
}
readFile()
.then((res) => {
// Create sendEmail params
params = {
Destination: { /* required */
ToAddresses: [
toEmail,
]
},
Message: { /* required */
Body: { /* required */
Html: {
Charset: "UTF-8",
Data: res
}
},
Subject: {
Charset: 'UTF-8',
Data: subject
}
},
Source: fromEmail, /* required */
}
sendEmail();
})
.catch((err) => {
console.log('File Read Error : ', err)
}
)
Can someone please show me how I can replace my htmlFileName with the req.body?
I use ejs to template my email here is a code i frenquently use to send email !
If you have questions i would be glade to answer
const ejs = require('ejs');
const AWS = require('aws-sdk');
const mailcomposer = require('mailcomposer');
const config_aws = {
accessKeyId: '',
secretAccessKey: '',
region: 'eu-west-1',
expeditor: '',
limitExpeditor: 50
};
AWS.config.update(config_aws);
const ses = new AWS.SES();
async function sendAnEmail(
expeditor,
subject,
destinator,
body,
destinator_name = null,
bcc = null,
callback
) {
ejs.renderFile(
`${__dirname}/templates/template.ejs`,
{
destinator,
destinator_name,
subject,
body
},
(err, html) => {
if (err) return console.error(err);
const sesEmailProps = {
Source: config_aws.expeditor,
Destination: {
ToAddresses: [`${destinator}`]
},
Message: {
Body: {
Html: {
Charset: 'UTF-8',
Data: html
},
Text: {
Charset: 'UTF-8',
Data: html ? html.replace(/<(?:.|\n)*?>/gm, '') : ''
}
},
Subject: {
Charset: 'UTF-8',
Data: subject
}
}
};
if (bcc) {
sesEmailProps.Destination = {
ToAddresses: [`${destinator}`],
BccAddresses: bcc // ARRAY LIMIT OF 49
};
}
ses.sendEmail(sesEmailProps, (error, data) => {
if (error) console.error(error);
callback(error, data);
});
}
);
}

Sending a mail via NODE.JS in an IBM Cloud function

I have similar problem as here: PHP mail function doesn't complete sending of e-mail
But after several tries I don't think it's my solution...
Objective:
Create an action which can send email.
Code:
function main(params) {
const params = {
"payload": {
"id": "sender.address#gmail.com",
"password": "CodeForTheSenderAccount",
"receiver": "another.mail.address#gmail.com",
"subject": "Test Wikit",
"body": "<html>HELLO WORLD</html>"
}
}
const nodemailer = require('nodemailer');
//Creation of a SMTP transporter
var transporter = nodemailer.createTransport({
service: 'Gmail',
auth: {
user: params.payload.id,
pass: params.payload.password
}
});
//Creation of data to send
const mail = {
from: '"Wikitest" <' + params.payload.id + '>',
to: params.payload.receiver,
subject: params.payload.subject,
text: 'Hello World',
html: params.payload.body
}
//Sending the mail
return(transporter.sendMail(mail, function (err, info) {
if (err) {
const ret = {status: 'OK'};
} else {
const ret = {status: 'KO'};
}
transporter.close();
return (ret);
}));
}
This code works locally and I receive the email. But not when running the function in the IBM Cloud console.
I think it's due to SMTP servers but I'm not sure...
Some of you will see the "payload" param. It's because this action is in a sequence and the action before send the params.
When working with asynchronous JavaScript in serverless functions, you need to return and resolve a Promise. Here is relevant documentation for your example https://github.com/apache/incubator-openwhisk/blob/master/docs/actions-node.md#creating-asynchronous-actions.
return(new Promise(function(resolve, reject) {
transporter.sendMail(mail, function (err, info) {
if (err) {
const ret = {status: 'OK'};
} else {
const ret = {status: 'KO'};
}
transporter.close();
resolve(ret);
}}));

SQS to Lambda + SES

I'm new with Lambda & SQS and I'm trying to create a function to send emails, queued in an SQS service, but I don't understand how to call the process function that contains the send + delete queue methods.
Here bellow I paste my code:
'use strict';
const AWS = require('aws-sdk');
const SQS = new AWS.SQS({ apiVersion: '2012-11-05' });
const Lambda = new AWS.Lambda({ apiVersion: '2015-03-31' });
const ses = new AWS.SES({ accessKeyId: "xxxxxxxx", secretAccesskey: "xxxxxxx/xxxxxxxxx" });
const s3 = new AWS.S3({ apiVersion: "2006-03-01", region: "us-west-2" });
const QUEUE_URL = 'https://sqs.us-west-2.amazonaws.com/xxxxxxx/queue';
const PROCESS_MESSAGE = 'process-message';
function getPieceOfMail (path, mapObj, replace) {
return new Promise(function (resolve, reject) {
s3.getObject({
Bucket: "myBucket",
Key: "myKey/" + path
}, function (err, data) {
if (err) {
reject(err);
} else {
if (replace === true) {
var re = new RegExp(Object.keys(mapObj).join("|"), "gi");
data = data.Body.toString().replace(re, function (matched) {
return mapObj[matched.toLowerCase()];
});
resolve(data);
} else {
resolve(data.Body.toString());
}
}
});
});
}
function getRegisterSource (nickname, activate_link) {
var activate_link, pieces;
pieces = [
getPieceOfMail("starts/start.html", {}, false),
getPieceOfMail("headers/a.html", {}, false),
getPieceOfMail("footers/a.html", {}, false),
];
return Promise.all(pieces)
.then(function (data) {
return (data[0] + data[1] + data[2]);
})
.catch(function (err) {
return err;
});
}
function sendEmail (email, data) {
return new Promise(function (resolve, reject) {
var params = {
Destination: { ToAddresses: [email] },
Message: {
Body: {
Html: {
Data: data
},
Text: {
Data: data
}
},
Subject: {
Data: "myData"
}
},
Source: "someone <noreply#mydomain.co>",
};
ses.sendEmail(params, function (err, data) {
if (err) {
reject(err);
} else {
resolve(data);
}
});
});
}
function process(message, callback) {
console.log(message);
// process message
getRegisterSource(event['nickname'], event['user_id'])
.then(function (data) {
return sendEmail(event["email"], data);
})
.catch(function (err) {
console.log("==ERROR==");
callback(err, err);
})
.finally(function () {});
// delete message
const params = {
QueueUrl: QUEUE_URL,
ReceiptHandle: message.ReceiptHandle,
};
SQS.deleteMessage(params, (err) => callback(err, message));
}
function invokePoller(functionName, message) {
const payload = {
operation: PROCESS_MESSAGE,
message,
};
const params = {
FunctionName: functionName,
InvocationType: 'Event',
Payload: new Buffer(JSON.stringify(payload)),
};
return new Promise((resolve, reject) => {
Lambda.invoke(params, (err) => (err ? reject(err) : resolve()));
});
}
function poll(functionName, callback) {
const params = {
QueueUrl: QUEUE_URL,
MaxNumberOfMessages: 10,
VisibilityTimeout: 10,
};
// batch request messages
SQS.receiveMessage(params, (err, data) => {
if (err) {
return callback(err);
}
// for each message, reinvoke the function
const promises = data.Messages.map((message) => invokePoller(functionName, message));
// complete when all invocations have been made
Promise.all(promises).then(() => {
const result = `Messages received: ${data.Messages.length}`;
callback(null, result);
});
});
}
exports.handler = (event, context, callback) => {
try {
if (event.operation === PROCESS_MESSAGE) {
console.log("Invoked by poller");
process(event.message, callback);
} else {
console.log("invoked by schedule");
poll(context.functionName, callback);
}
} catch (err) {
callback(err);
}
};
can somebody throw me some light to this?
Thanks in advice.
UPDATE
After so much misconception, I've decided to start looking on how the example of polling-SQS works provided by AWS.
There I've found that I lacked some basic SQS permissions, but solved now by adding the right policy:
{
"Version": "2012-10-17",
"Statement": [{
"Effect": "Allow",
"Action": [
"lambda:InvokeFunction"
],
"Resource": ["*"]
}]
}
This allows Lambda.invoke() to call process().
When the process(message, callback) is called, if I console.log(message);, it seems that there's no message, although the queue is being cleared by the line SQS.deleteMessage(params, (err) => callback(err, message));
What I was trying was to combine my sendMail function that is currently working with a SQS service so I only have to push each message to the queue.
This is a common requirement where AWS SES has its own limitations in sending emails at once. If these limitations are violated, the SES account will sandbox itself. It seems like you have solved the problem using proper access credentials.
This code contains a Python3 Lambda code that can be used to handle a situation like this, where a Lambda polls from SQS using threading, and sends emails using SES, without exceeding the given limitations.
Link to Github Project.
You can also consider using the new feature in SQS, which is capable of invoking lambdas, when a new message is placed within SQS. But, be careful not to exceed the maximum number of lambda functions within the AWS Account region. (See this document)

Sending email via AWS SES within AWS Lambda function

I have created a very basic simple function on AWS Lambda which will be used to accept form submissions.
Part of the function will be to send an email to a particular person, pretty simple. I am trying to use AWS SES in order to do this. I have setup the SES service etc, and verified the account I wish to send to and have been able to send out a test email. All works!!
Now when I try and do the same within AWS Lambda and use the aws sdk it doesn't send out the email. I don't get an error or anything.
Below is the code that I am using for the AWS Lambda function.
Has anyone had any experience using lambda and sending emails via ses, through a lambda function? Or even just using the node.js aws sdk would more than likely be helpful.
var aws = require('aws-sdk');
var ses = new aws.SES({
accessKeyId: 'myAccessKey',
secretAccesskey: 'mySecretKey',
region: 'eu-west-1'
});
exports.handler = function(event, context) {
console.log("Incoming: ", event);
var output = querystring.parse(event);
var eParams = {
Destination: {
ToAddresses: ["toAddress#email.com"]
},
Message: {
Body: {
Text: {
Data: output.Key1
}
},
Subject: {
Data: "Ses Test Email"
}
},
Source: "mysource#source.com"
};
console.log('===SENDING EMAIL===');
var email = ses.sendEmail(eParams, function(err, data){
if(err) console.log(err);
else {
console.log("===EMAIL SENT===");
console.log(data);
}
});
console.log("EMAIL CODE END");
console.log('EMAIL: ', email);
context.succeed(event);
};
It would appear that I had the context.succeed(event) placed in the wrong area of code.
Once I moved it into the sendEmail callback all worked.
var aws = require('aws-sdk');
var ses = new aws.SES({
accessKeyId: 'myAccessKey',
secretAccesskey: 'mySecretKey',
region: 'eu-west-1'
});
exports.handler = function(event, context) {
console.log("Incoming: ", event);
var output = querystring.parse(event);
var eParams = {
Destination: {
ToAddresses: ["toAddress#email.com"]
},
Message: {
Body: {
Text: {
Data: output.Key1
}
},
Subject: {
Data: "Ses Test Email"
}
},
Source: "mysource#source.com"
};
console.log('===SENDING EMAIL===');
var email = ses.sendEmail(eParams, function(err, data){
if(err) {
console.log(err);
context.fail(err);
} else {
console.log("===EMAIL SENT===");
console.log("EMAIL CODE END");
console.log('EMAIL: ', email);
console.log(data);
context.succeed(event);
}
});};
var aws = require("aws-sdk");
var ses = new aws.SES({ region: "us-west-2" });
exports.handler = async function (event) {
var params = {
Destination: {
ToAddresses: ["RecipientEmailAddress", ...],
},
Message: {
Body: {
Text: { Data: "Test" },
},
Subject: { Data: "Test Email" },
},
Source: "SourceEmailAddress",
};
return ses.sendEmail(params).promise()
};
This is because Lambda freezes the container when the function exits and any async processes are frozen, such as your email. This is especially true with Node. See Lambda Programming Model. http://docs.aws.amazon.com/lambda/latest/dg/lambda-introduction.html
My case is: when you set VPC, the issue happens cause of internet limitation access.
If you remove VPC, everything works fine.
It seems a AWS bug for me.
I opened today a AWS Support for it.
No anwers yet.

Resources