node js script to send email over SMTP

I’ve had this challenge for which the only solution was building an nodejs webhook adapter for my smtp server. Using webhooks and the following script I was able to enable API access for my email server (which it lacked).

 

 

const SMTPConnection = require(‘smtp-connection’);
var options = {
host: ‘smtp.gmail.com’,

port: 465,
secure: true,
// requireTLS: true,
ignoreTLS: true,
debug: true
};
var envelope = {
from: ‘<user>@gmail.com’,
to: ‘badr42@gmail.com’,

};

var auth = {
user: ‘<user>@gmail.com’,
pass: ‘<pass>’,

};
var message=”hello world”

let connection = new SMTPConnection(options);
connection.connect(function(err, connect){
if(err){
console.log(“error connecting”);
console.log(err);
}else{
console.log(“attempting to login”);
console.log(connect);

connection.login(auth, function(err, connect){
if(err){
console.log(“error logging in”);
console.log(err);
}else{
console.log(“logged in”);
console.log(connect);
connection.send(envelope, message, function(err, result){
if(err){
console.log(err);
}else{
console.log(“email sent”);
console.log(result);
connection.quit();
}
});

}
});

}
});

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s