Amazon SES - Can't get email to be delivered - node.js

I have an aws ubuntu server running a node.js application. I am trying to build a simple forgot password feature which sends users email. The code runs but no email is delivered, so I assume I am still missing something.
1) Within SES I verified the email domain a2zcribbage.com. I assume this means I can now send email from any alias under this domain, right?
2) I npm installed the SES module onto my ec2 server.
3) My code looks as follows:
aws.config.update({accessKeyId: 'MYKEY', secretAccessKey: 'MYKEY', region: 'us-west-2'});
var ses = new aws.SES({apiVersion: '2010-12-01'}); // load AWS SES
var to = [post.email]; // send to list
var from = 'support#a2zCribbage.com'; // this must relate to a verified SES account
ses.sendEmail( { // this sends the email #todo - add HTML version
Source: from,
Destination: { ToAddresses: to },
Message: {
Subject: { Data: 'a2zCribbage - Temporary Password' },
Body: {
Text: {
Data: "Your username is: " + userName + ". Your temporary password is: " + tempPassword
}
}
}
}, function(err, data) { if (err) console.log(err); });
The code runs without error, but no email is delivered. How do I debug this further given there's no error?

My suggestion for your issue:
Make sure you have been removed from sandbox, to do that, you need contact aws to extend the limits, that's free service for any aws accounts.
register a real mailbox directly to SES, for example, support#a2zCribbage.com to test if the code works or not.
From the code, seems you missed to load aws sdk.
var aws = require('aws-sdk');
create a SNS task, subscript a SMS or mailbox, enable bounced or complain notification in SES, with it, you can confirm if the setting is proper or not.
let me know if this can help you to fix your issue.

I'll answer my own question here. Good feedback from everyone. The one thing I was missing is that by default, when you create an SES account, you are put into a "sandbox" mode, and not a full fledged email sending mode, so I was trying to send emails and it wasn't allowing it. Once I requested I be taken out of sandbox mode everything worked just fine.

Related

SMTP server using hmailserver. Host cannot be resolved to a type

I am trying to create a local SMTP server. I configured everything according to this: Link to set-up hmailserver
After that, I ran my diagnostics thing and it said host(in my case, noreply#bufferoverflow.com) cannot be resolved. I googled and read the documentation and question section of hmailserver Docs and found that I am missing a DNS configuration on my machine.
I followed this article to set-up the DNS ip to name: How to add ip domain mapping in windows
127.0.0.1 bufferoverflow.com
on my
C:\Windows\System32\drivers\etc>notepad hosts.ics
It still said host cannot be resolved to a type. I tried to see if the port was actually open and listening. So I did this command in CMD:
netstat -a
It's listening. I doubled checked everything but coudn't found where I was going wrong. I created a simple nodejs server and hosted it on port 80 at localhost and typed "bufferoverflow.com" in my browser it opened 127.0.0.1 aka localhost which is because it has an ip domain mapping I have given.
Now I am lost as why the hostname can't be resolved. My DNS is cool. Everthing is configured as docs says and the port is also open and listening. Please help me.
So based on what you said you created your own SMTP server which is nice and all to learn, but I would recommend using SendinBlue 3rd party service FREE and gives you 300 mails/day
Getting Started with sendinblue:
Link: https://www.sendinblue.com/
Go make and an account free of charge till you decide to pay you'll read there
Once your account is all set, and activated often SMTP service for ones account is not automatically activate so you'd have to email them and they literally respond within 24hr I LOVE THE SERVICE(I am not promoting nor sponsored to hype them or anything just saying the truth of what I have experienced).
So now at this point your account and SMTP service is activated, now question is how do you use sendinBlue?
In your dashboard on your Top right you will see your username
Click on that and a dropdown menu should appear
Then you want to go to SMTP & API
Once you then you will see a menu with API Keys on the left and SMTP on the right
You want to click on the SMTP.
Once you the you will see Login that would be the email you registered with and you will see Master password now that is what you use to authenticate yourself.
Implementation: Now you have everything sorted you know where you keys are and login
In .env file
sendinBlue__login=<Your email that your saw in that smtp tab>
sendinBlue__key= <Key you saw in that smtp tab>
In your file where you want to send email
const nodemailer = require('nodemailer');
router.post('/api/email', (request, response)=>{
const transporter = nodemailer.createTransport({
service:'SendinBlue',
auth:{
user:process.env.sendinBlue__login,
pass:process.env.sendinBlue__key
}
})
const mailOptions = {
from:process.env.sendinBlue__login
to:'example#gmail.com',
subject:'Order confirmation',
html:`
<h1>Confirmation of your received order</h1>
`
}
transporter.sendMail(mailOptions, (error, info)=>{
if (error){
console.log(error);
return response.status(500).json({msg:"Email failed to send probably network problems or SMTP not activated"});
}
return reponse.status(200).json({msg:'Email sent'});
})
})
Now this is how easy sendinBlue works just simple signup and account activation also SMTP activation if not activated from get go by email customer support
If that guide above did not make sense to you: Shameless plug
You can go checkout my video that I did and I was using sendinBlue with nodemailer for emailing it's a bit at the end
Link: https://youtu.be/5vWXbGIdDQw
Now If you want to send an email using noreply#domain.com with sendinBlue you do not even have to have the email activated but still can send
How you go about this is the way you craft your mailOptions
router.get("/api/test", (request, response) => {
const transpoter = nodemailer.createTransport({
service: "SendinBlue",
auth: {
user: process.env.sendinBlue__email,
pass: process.env.sendinBlue__key,
},
});
const mailOptions = {
from: "noreply#Test.com",
to: "juniorWebProjects#gmail.com",
subject: "Confirmation Order",
html: `
<h1>Order Received</h1>
`,
};
transpoter.sendMail(mailOptions, (err, info) => {
if (err) {
return console.log(err);
}
return console.log("Message sent");
});
});
See on my mailOptions I explicitly wrote myself the email that I want and when sendinBlue sends that email the receiver will see that is coming from noreply#Test.com

Unlock a nodejs program through email answer

I'm working with dialog flow on Google home, and I have a program which sends an email (via Nodemailer), I would like to lock a function, and unlock it depending on the email's answer.
Maybe I could put a link inside the email as "if you agree with this, click on this link, otherwise don't".
Can I get the answer with my program? If yes, how?
Here is the function I use to send my email:
var transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: '**********',
pass: '**********'
}
});
var mailOptions = {
from: '*********#gmail.com',
to: '********.********#gmail.com',
subject: 'Sending Email using Node.js',
text: 'That was easy!'
};
transporter.sendMail(mailOptions, function(error, info){
if (error) {
console.log(error);
} else {
console.log('Email sent: ' + info.response);
}
Thanks.
You have a lot of things you need to think about when designing this - nevermind actually coding it.
Somehow you need to get the user's address in the first place. Google Home (and the Actions on Google platform) don't have a way to give this to you, and asking the user for their info is a bad UX.
One way you can get their address is through Account Linking, which lets you link their Assistant account to an account in your system. This won't give you the address directly, but since they have to log in through your website first, you can either use Google Login (and thus get their profile info) or ask them for it as part of your account process.
Once you have their address - you want to verify it. There are a ton of suggestions on the web about how to correctly do double-opt-in. Make sure you follow them. There are node modules that will help you do this as well - you don't need to write your own.
Coding this securely is not something to be taken lightly. Doing it wrong lets you become a spam relay. Doing it not-quite-right may just have you considered as spam by the email providers.
All you have to do is Define System so that from that E-mail box e-mail will be sent. Example, you can add your Gmail account and password. Add this code just in Server.js just below the var app=express() line.
var smtpTransport = nodemailer.createTransport({
service: "gmail",
host: "smtp.gmail.com",
auth: {
user: "",
pass: ""
}
});

Authentication errors when sending intermittent emails from gmail using Heroku / node.js / emailjs

I am running a scraper app on Heroku that sends emails every once in a while. Sometimes it'll be every 2 minutes, sometimes it won't send anything for 2 days-depends on what the scraper finds. I am running into continual errors with email authentication. I have tried configuring the email with nodemailer and emailjs. Both run into the same issue. I am wondering if it is because of the way Heroku servers are distributed?
I have taken the following steps:
1) unlocked the account at: https://accounts.google.com/DisplayUnlockCaptcha
2) allowed less secure apps here: https://myaccount.google.com/lesssecureapps
With both nodemailer and emailjs, I notice that it works fine for a couple hours but then if no emails are sent for a day, as soon as it tries to send another one I receive an authentication error. Here is the code using emailjs that works for a couple hours then fails with an authentication error:
var server = email.server.connect({
user: process.env.CRAWLER_MAIL,
password:process.env.CRAWLER_PWD,
host: "smtp.gmail.com",
ssl: true
});
var message = {
text: "Placeholder",
from: 'NAME <'+ process.env.CRAWLER_MAIL+'>',
to: eachEmail[0],
subject: emailSubject,
attachment:
[
{data:emailBody, alternative:true}
]
};
server.send(message, function(err, message) { console.log(err || message); });
And here is the error from the Heroku log:
Error: authorization.failed (bad response on command '[redacted]': -5.7.14 https://accounts.google.com/signin/continue?sarp=1&scc=1&plt=[redacted]-)
I don't use 2-step verification for this account, so I don't think I need an app password. Any other ideas about how I can get this to consistently work?

Receive GCM push notification in node.js app

I'm building a command-line application in node.js and would like to receive GCM push notifications (the command-line app will be interacting with the same set of services that iOS/Android apps use, hence wanted to use the same notification service).
Given that GCM can be used on iOS (and thus is not Android-specific) I am hoping it can be used from node.js as well.
I've seen many articles about sending push notifications from node.js, but haven't been able to find anything about using node.js on the receiving end.
i think if you have to send push notification ,to ios and andriod then fcm is better then gcm use this
router.post('/pushmessage', function (req, res) {
var serverKey = '';//put server key here
var fcm = new FCM(serverKey);
var token = "";// put token here which user you have to send push notification
var message = {
to: token,
collapse_key: 'your_collapse_key',
notification: {title: 'hello', body: 'test'},
data: {my_key: 'my value', contents: "abcv/"}
};
fcm.send(message, function (err, response) {
if (err) {
res.json({status: 0, message: err});
} else {
res.json({status: 1, message: response});
}
});
});
I believe you can using service workers.
Push is based on service workers because service workers operate in the background. This means the only time code is run for a push notification (in other words, the only time the battery is used) is when the user interacts with a notification by clicking it or closing it. If you're not familiar with them, check out the service worker introduction. We will use service worker code in later sections when we show you how to implement pushes and notifications.
So basically there is a background service that waits for push and thats what you are going to build.
Two technologies
Push and notification use different, but complementary, APIs: push is invoked when a server supplies information to a service worker; a notification is the action of a service worker or web page script showing information to a user.
self.addEventListener('push', function(event) {
const promiseChain = getData(event.data)
.then(data => {
return self.registration.getNotifications({tag: data.tag});
})
.then(notifications => {
//Do something with the notifications.
});
event.waitUntil(promiseChain);
});
https://developers.google.com/web/fundamentals/engage-and-retain/push-notifications/handling-messages
I don't think it possible (in a simple way)...
Android/iOS has an OS behind with a service that communicates with GCM...
If you are trying to run a CLI tool, you'll need to implement a service on top of the OS (Linux, Windows Mac) so it can receive notifications.
GCM sends the notifications against the device tokens which are generated from iOS/Android devices when they are registered with push notification servers. If you are thinking of receiving the notifications without devices tokens it is fundamentally incorrect.
It's not mandatory to depend only on GCM, today there are many packages are available for sending pushNotification.
Two node packages are listed below.
fcm-call - you can find documentation from https://www.npmjs.com/package/fcm-call
fcm-node
fcm-call is used - you can find documentation from https://www.npmjs.com/package/fcm-node/
let FCM = require('fcm-call');
const serverKey = '<Your Server Key>';
const referenceKey = '<Your reference key>'; //Device Key
let title = '<Your notification title here.>';
let message = '<Your message here>';
FCM.FCM(serverKey, referenceKey, title, message);
And Your notification will be sent within 2-3 seconds.
Happy Notification.

Google Cloud Pub/Sub API - Push E-mail

I'm using node.js to create an app that gets a PUSH from Gmail each time an email is received, checks it against a third party database in a CRM and creates a new field in the CRM if the e-mail is contained there. I'm having trouble using Google's new Cloud Pub/Sub, which seems to be the only way to get push from Gmail without constant polling.
I've gone through the instructions here: https://cloud.google.com/pubsub/prereqs but I don't understand how exactly this is supposed to work from an app on my desktop. It seems that pub/sub can connect to a verified domain, but I can't get it to connect directly toto the .js script that I have on my computer. I've saved the api key in a json file and use the following:
var gcloud = require('gcloud');
var pubsub;
// From Google Compute Engine:
pubsub = gcloud.pubsub({
projectId: 'my-project',
});
// Or from elsewhere:
pubsub = gcloud.pubsub({
projectId: 'my-project',
keyFilename: '/path/to/keyfile.json'
});
// Create a new topic.
pubsub.createTopic('my-new-topic', function(err, topic) {});
// Reference an existing topic.
var topic = pubsub.topic('my-existing-topic');
// Publish a message to the topic.
topic.publish('New message!', function(err) {});
// Subscribe to the topic.
topic.subscribe('new-subscription', function(err, subscription) {
// Register listeners to start pulling for messages.
function onError(err) {}
function onMessage(message) {}
subscription.on('error', onError);
subscription.on('message', onMessage);
// Remove listeners to stop pulling for messages.
subscription.removeListener('message', onMessage);
subscription.removeListener('error', onError);
});
However, I get errors as if it isn't connecting to server and on the API list I see only errors, no actual successes. I'm clearly doing something wrong, any idea what it might be?
Thank you in advance!
TL;DR
Your cannot subscribe to push notifications from the client side.
Set up an HTTPS server to handle the messages. Messages will be sent
to the URL endpoint that you configure, representing that server's
location. Your server must be reachable via a DNS name and must
present a signed SSL certificate. (App Engine applications are
preconfigured with SSL certificates.)
Just subscribe to the push notifications on your server, and when you get the notification, you can figure out who it concerns. The data you will get from the notifications is what user that it concerns, and the relevant historyId, like so:
// This is all the data the notifications will give you.
{"emailAddress": "user#example.com", "historyId": "9876543210"}
Then you could e.g. emit an event through Socket.io to the relevant user if he is online, and have him do a sync with the supplied historyId on the client side.

Resources