Trying to Send Email Using Alexa and AWS SES. Email will send however Alexa will return and Error - node.js

I am writing an Alexa Skill where one of the functions is to request to send an email to a user using AWS SES.
When the utterance is said to send the email, the email will send however Alexa will always reply 'There was a problem with the requested skill's response'.
I have tested to make sure the 'getEmail' intent itself is working and it is.
I have also tried moving the function within the intent but this has the same results.
This is the function to send the email using SES which seems to be working:
function sendEmail (event, context, callback){
var params = {
Destination: {
ToAddresses: ["xyz#gmail.com"]
},
Message: {
Body: {
Text: { Data: "Hi. Here is your email"
}
},
Subject: { Data: "Here is an email"
}
},
Source: "abc#gmail.com"
};
ses.sendEmail(params, function (err, data) {
callback(null, {err: err, data: data});
if (err) {
console.log(err);
context.fail(err);
} else {
console.log(data);
context.succeed(event);
}
});
}
Here is the intent which call the sendEmail() function.
'getEmail': function () {
sendEmail();
var bodyTemplate1 = new Alexa.templateBuilders.BodyTemplate1Builder();
var template1 = bodyTemplate1.setTitle("email").setTextContent(makeRichText("email")).setBackgroundImage(makeImage(image)).build();
this.response.speak("Your email has been sent").renderTemplate(template1).shouldEndSession(true);
this.emit(':responseReady');
},
When I run this I would like for the email to send to the the device and Alexa to just say "Your Email has been sent". At the moment it is just the email that is sending and the device saying "There was a problem with the requested skill's response"
Here is the error message from AWS Cloud

Looking at logs, your callback(null, {err: err, data: data}); is the issue here. From the code that you posted, callback doesn't exist (will be undefined). It doesn't look like you need that there so you can remove it.
AWS SDK - SES.sendEmail
The "callback" is just your function that's passed as the second argument.

Related

cannot set headers after they are sent to the client Nodejs?

im trying to make a create methode to create an user with sending email after creat account ,
when i test this method
cannot set headers after they are sent to the client Nodejs
My Method:
export function registerUser(req, res, next) {
// log the request body
console.log(JSON.stringify(req.body))
const { name, email, password, phone, role, level } = req.body
userModel.create(
{
name: name,
email: email,
password: password,
createdAd: Date.now(),
phone: phone,
photo: `${req.protocol}://${req.get('host')}/images/${
req.file.filename
}`,
level: 1,
role: role,
status: 0,
},
(err, result) => {
if (err){ next(err) ;}
else {
sendMail(req, res)
res.status(201).json({
message: 'User added successfully!',
})
}
}
)
}
Email Method
export function sendMail(req, res) {
var mailOptions = {
from: 'neublua.game10#gmail.com',
to: req.body.email,
subject: 'Sending Email using Node.js',
text: 'That was easy!',
}
transporter.sendMail(mailOptions, function (error, info) {
if (error)
res.status(500).json({
message: 'Error sending email',
})
else console.log('Email sent: ' + info.response)
})
}
i put res and req to can access to the email
how can i solve it
This error shows up when you send the response two times.
What express does is that it will only send the first response back to the client, while any responses after that won't be sent. That's basically how the HTTP protocol works, you send a request, you get one response back.
1 request = 1 response
So, the problem actually comes from here
You're saying:
sendMail()
and after that you're saying:
res.status(201).json({
message: 'User added successfully!',
})
where the sendMail is actually sending a response, because an error is happening when it tries to send the email look:
the following screenshot is inside your sending email middleware:
So, you shouldn't send a response twice, either send it here, or there.
What exactly are you trying to achieve? If it's an unimportant email you're trying to send such as the "welcome" email, then keep checking for an error if emails are failing to sent but remove the response from there, the client doesn't need to know that the "welcome" email had a problem while being sent.
If this is an important email, such as an email verification email, then, remove the response sending lines from the user creation middleware, and only send the response (Success or fail) inside the email middleware.

Send email from app with firebase functions

I want to now if its possible to send e-mail using firebase triger mail from the App with callable functions. I want to be able to call the function and in that function i want to send email or/ i want to use the extention of firebase "triger mail". I have tried this but nothing happens
exports.sendEmail = functions.https.onCall((data, context) => {
return admin.firestore().collection('mail').add({
to: data,
message: {
subject: 'Hello from Firebase!',
html: 'This is an <code>HTML</code> email body.',
},
})
})
and in my function i call the function as
sendmail() {
let send = firebase.functions().httpsCallable('sendEmail')
send({mail:'v#x.com'})
},

Twilio Check the Verification Token API+postman timeout

I am setting up phone authentication with Twilio, using nodejs....All is working as expected, except when verifying the token. Below is the function which I am using:
verifyPhone: function (req, res) {
client.verify.services('VAxxxxxxxxxxxxxxxx')
.verificationChecks
.create({ to: '+15017122661', code: '123456' })
.then(function (err, verification_check) {
if (err) {
res.status(500).send(err);
} else if (verification_check ==='approved') {
res.status(200).send('Account Verified');
}
})
}
I am checking the endpoint in postman, and it is timing out with no response.
When, I checked in twilio portal, the number status changed to approved.
Any idea why the response is not showing up.
Btw, I turned off SSL certificate
Thanks,
The Twilio Function JavaScript below works for me.
Since Verify v2 is relatively new, make sure your Twilio Helper library is up to date, https://github.com/twilio/twilio-node/releases.
Example to execute function (I have Check for valid Twilio signature turned off for my Twilio function to demo this):
(Example URL to Run Function):
https://mango-abcdef-1234.twil.io/v2start?phoneNumber=%2b15555551234&code=720732
exports.handler = function(context, event, callback) {
const client = context.getTwilioClient();
const code = event.code;
let phoneNumber = event.phoneNumber;
client.verify.services('VAxxxxxxxxxxxxxxxx')
.verificationChecks
.create({to: phoneNumber, code: code})
.then(verification_check => {
console.log(`***VERIFICATION STATUS***: ${verification_check.status}`);
callback(null, `${verification_check.status}`);
})
.catch(err => {
console.log(err);
callback();
});
};
Also recent blog on this topic.
Serverless Phone Verification with Twilio Verify and Twilio Functions

AWS SES Error: Email address is not verified

I am trying to send email using aws-sdk ses in nodejs.
While executing the code the response I am getting is:
message:
'Email address is not verified. The following identities failed the check in region US-EAST-1:xxxtestemailxxx#gmail.com',
code: 'MessageRejected'
I have already verified the sender as well as receiver email(destination email in a array).
On SES settings of aws console, it is showing the email is verified. I removed the email and then once again successfully verified it.
Last time(few months back) when I used it, everything was working fine.
Below is the screenshot of my aws console related to SES:
Also when I am sending the test email using aws console, its working fine. This is only happening when I am trying to send email using aws-sdk.
Does anyone knows what is wrong, just to be clear I am also posting the code below:
const send_email = function (email, subject, source, payload) {
console.log(email, subject, source, payload);
let email_param = {
Destination: {
ToAddresses: email
},
// ConfigurationSetName: 'XXXRANDOMTEXT_PLATFORM',
Message: {
Body: {
Html: {
Charset: "UTF-8",
Data: payload
}
// Text: {
// Charset: "UTF-8",
// Data: payload
// }
},
Subject: {
Charset: "UTF-8",
Data: subject
}
},
Source: source
};
let send_email = SES.sendEmail(email_param).promise();
send_email
.then(data => {
return true;
})
.catch(error => {
console.log('sending email error:', error);
return false
});
}
If your AWS account is still in the sandbox mode then you have to verify your sender, receiver etc. Go to the prod mod to have access to the full features.

AWS SES Nodejs SDK - email with dash bounces

I'm using the aws nodejs SES sdk and emails with to address with something like 'email#host-name.com' appear to all be bouncing even though they exist. Works for other emails without the dash.
I looked and the emails do look like they end up as query parameters, do I just need to individually url encode them? I dont get an error anymore, but I dont have any email address to test with.
var params = {
Destination: {
ToAddresses: [
'email#host-name.com'
]
},
Message: {
Body: {
Html: {
Data: body
}
},
Subject: {
Data: subject
}
},
Source: fromAddress
};
ses.sendEmail(params, function (err, data) {
if (err){
console.log(err, err.stack);
} else {
//sent
}
});
This ended up not having to do with the dash. The emails were bouncing because an AWS email service IP was blacklisted.

Resources