how to setup emailjs to send email - node.js

I am trying to use mailjs to send out email, and here is what I got:
var server = email.server.connect({
user: "something#mydomain.com",
password: "pw",
host: "smtp.office365.com",
port: "587",
tls: true
});
server.send({
text: "test!!",
from: "someemail#mydomain.com",
to: "recipient#somedomain.com",
subject: "hello"
}, function(err, message){console.log(err || message);});
The above code eventually throw me an error:
{ [Error: authorization.failed]
code: 3,
previous: { [Error: timedout while connecting to smtp server] code: 4, smtp: undefined },
smtp: undefined }
So basically my corporate email service is on office365, configured to use mydomain.com. I don't really know what does the user and password mean for the server, so I just used the sender's email and password. Is it anything wrong with my code? Maybe this is the problem?? Or otherwise what would it be?

First check and confirm host.Here is no need to use port: "587", tls: true.
You can use, this is wrking code
var server = email.server.connect({
user: 'abc#india.com',
password: 'vvq3tng$',
host: 'your host',
ssl: true,
});
server.send({
from: '<abc#india.com>', // sender address
to: '<xyz#india.com>',// list of receivers
cc: '',
subject: 'your subject', // Subject line
text: 'contant', // html body
}, function (err, message) {
if(err) {
console.log(err)
} else {
console.log("email sent")
}
});

Related

could not setup dkim in nodemailer

I want to setup DKIM in nodemailer. Without DKIM, tls and "secure: true" my code works fine however, when I run code changed for DKIM it shows error:
Code:
var nodemailer = require('nodemailer');
var fs = require("fs");
const pr_key = fs.readFileSync("private.key", {encoding: "utf-8"})
const transporter = nodemailer.createTransport({
port: 465, // Postfix uses port 25
host: 'example.com',
secureConnection: false,
tls: {
ciphers:'SSLv3'
// rejectUnauthorized: false
},
secure: true,
dkim: {
domainName: "mail.example.com",
keySelector: "mail",
privateKey: pr_key
},
auth: {
user: "abc",
pass: "def"
},
});
var message = {
from: 'noreply#example.com',
to: 'pleasereply#example.com',
subject: 'Confirm Email',
text: 'Please confirm your email',
html: '<p>Please confirm your email</p>'
};
transporter.sendMail(message, (error, info) => {
if (error) {
return console.log(error);
}
console.log('Message sent: %s', info.messageId);
});
Error Message:
[Error: 7336:error:1408F10B:SSL routines:ssl3_get_record:wrong version
number:c:\ws\deps\openssl\openssl\ssl\record\ssl3_record.c:332: ] {
library: 'SSL routines', function: 'ssl3_get_record', reason:
'wrong version number', code: 'ESOCKET', command: 'CONN' }
I have already configured TXT record for DKIM in DNS and SMTP server running on my server side is programed using library "smtp-server" module of nodejs.
What I tried:
I tried changing port to 25, 2525, 465 and 143
Tried every combination of host, domainname for domain "example.com" and tried keySelector "mail" and "#".
I tried commenting "secureConnection" and "tls: {}" and then changing to "secure: false" which results in other error:
Error: self signed certificate
at TLSSocket.onConnectSecure (node:_tls_wrap:1530:34)
at TLSSocket.emit (node:events:526:28)
at TLSSocket._finishInit (node:_tls_wrap:944:8)
at TLSWrap.ssl.onhandshakedone (node:_tls_wrap:725:12) { code: 'ESOCKET', command: 'CONN' }
i tried to change "to:" property of message object to my gmail account just for testing but still got same message saying I got wrong version
Help me resolving this issue.

I got an error, after add my cpanel smtp mail service with nodemailer,

I got this error please help me , i am stuck , its happening with my cpanel smtp server.
This is error in my terminal:
Error: connect ETIMEDOUT 76.76.21.21:465
at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1187:16) {
errno: -4039,
code: 'ESOCKET',
}
My Code:
// async..await is not allowed in global scope, must use a wrapper
async function main() {
let transporter = nodemailer.createTransport({
host: "workbes.com",
port: 465,
secure: true, // true for 465, false for other ports
auth: {
user: 'customer#workbes.com', // generated ethereal user
pass: 'mypassword', // generated ethereal password
},
tls: {
rejectUnauthorized:false
}
});
// send mail with defined transport object
let info = await transporter.sendMail({
from: '"Fred Foo 👻" <foo#example.com>', // sender address
to: "almafuz97#gmail.com", // list of receivers
subject: "Hello ✔", // Subject line
text: "Hello world?", // plain text body
html: "<b>Hello world?</b>", // html body
});
console.log("Message sent: %s", info.messageId);
console.log("Preview URL: %s", nodemailer.getTestMessageUrl(info));
// Preview URL: https://ethereal.email/message/WaQKMgKddxQDoou...
}
main().catch(console.error);

Send Email Using Microsoft 365 Email Server In NodeJS

let transporter = nodemailer.createTransport({
service: "Outlook365",
host: 'smtp.office365.com',
port: 587,
tls: {
ciphers:'SSLv3'
},
auth: {
user: 'username',
pass: 'password'
}
});
I have an EAUTH error while sending an email, please check the image for error.
[1]: https://i.stack.imgur.com/snt3T.jpg
This code should do what you wish, you'll need to set your password to test this.
If the password is incorrect, you'll get an error:
Error: Invalid login: 535 5.7.3 Authentication unsuccessful message.
const nodemailer = require('nodemailer');
// Set this from config or environment variable.
const PASSWORD = '....';
async function send365Email(from, to, subject, html, text) {
try {
const transportOptions = {
host: 'smtp.office365.com',
port: '587',
auth: { user: from, pass: PASSWORD },
secureConnection: true,
tls: { ciphers: 'SSLv3' }
};
const mailTransport = nodemailer.createTransport(transportOptions);
await mailTransport.sendMail({
from,
to,
replyTo: from,
subject,
html,
text
});
} catch (err) {
console.error(`send365Email: An error occurred:`, err);
}
}
send365Email("from#example.com", "to#example.com", "Subject", "<i>Hello World</i>", "Hello World");
You will need to disable the SmtpClientAuthenticationDisabled through the online power shell with the command
Set-TransportConfig -SmtpClientAuthenticationDisabled $false
You can check for more information at Enable or disable authenticated client SMTP submission (SMTP AUTH) in Exchange Online
You can find the detailed information here:
https://developer.microsoft.com/en-us/graph/quick-start?code=M.R3_BAY.822b5ade-d816-85bb-ec94-8c349cdfca4b&state=option-node

Nodemailer and postfix

I have install postfix with default parameters as Internet site, nodemailer package into my node.js server. And trying to do this:
var transporter = nodemailer.createTransport({
host: '127.0.0.1',
port: 25,
// auth: {
// user: user,
// pass: pass
// },
secure: false,
tls:{
rejectUnauthorized: false
}
});
I dont know what is username and password, I did not set it.
transporter.sendMail({
from: '"Mysite.com" <robot#mysite.com>', // sender address
to: user.email, // list of receivers
subject: "Your password for mysite.com", // Subject line
html: html // html body
}, (err) => {console.log('mail send error', err)});
In tail -f /var/log/mail.log I see this
Jan 30 01:00:04 80523 postfix/smtpd[26772]: connect from localhost[127.0.0.1]
Jan 30 01:00:04 80523 postfix/smtpd[26772]: lost connection after EHLO from localhost[127.0.0.1]
Jan 30 01:00:04 80523 postfix/smtpd[26772]: disconnect from localhost[127.0.0.1] ehlo=2 starttls=1 commands=3
And mail is not sent...
But if I use command echo "First message" | mutt -s "msg" mymail#mail.ru, mail sent add I receive it.
Some ideas why?
If you followed similar set up for postfix as found here:
https://medium.com/codingtown/send-mail-using-postfix-server-bbb08331d39d
Then you should be able to get the following to run
const nodemailer = require('nodemailer');
let transporter = nodemailer.createTransport({
service: 'postfix',
host: 'localhost',
secure: false,
port: 25,
auth: { user: 'yourlinuxusername#edison.example.com', pass: 'yourlinuxuserpassword' },
tls: { rejectUnauthorized: false }
});
let mailOptions = {
from: 'yourlinuxusername#edison.example.com',
to: 'xxxyyyzzz111#gmail.com',
subject: 'nodemailer test',
text: 'hope it got there'
};
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
console.log(error);
} else {
console.log(info);
}
});

NodeJs Error When Submitting Email using Nodemailer

I have followed these steps to setup nodemailer
1) Allow access to less secure apps in gmail
2) Written the following in app.js
app.post('/reachus/send',function(req,res){
var transporter=nodemailer.createTransport({
service:'Gmail',
auth: {
user:'my#gmail.com',
pass:'***'
}
});
var mailOptions={
from:'Naveen DK <my#gmail.com>',
to:'my#gmail.com',
subject:'Email Sent from your website',
text:'You have a submission with the following details.. Name: '+req.body.name +'Email: '+req.body.email+' Message: '+ req.body.message,
html: '<p>You have a submission with the following details..</p><ul><li> Name :'+req.body.name + ' </li><li>Email: '+req.body.email +'</li><li>Message ' + req.body.message +'</li></ul>'
};
transporter.sendMail(mailOptions,function(error,info){
if(error){
console.log(error);
res.redirect('/');
} else{
console.log('Message Sent ' + info.response);
res.redirect('/');
}
});
});
3) Once I Click on Submit Email I get the following error
{ Error: read ECONNRESET
at exports._errnoException (util.js:1026:11)
at TLSWrap.onread (net.js:569:26)
code: 'ECONNECTION',
errno: 'ECONNRESET',
syscall: 'read',
command: 'CONN
}
Please find the below 2 vids for more details
1 https://www.dropbox.com/s/nc1zvivlfpabj6h/HowMyCodeLooksLike.wmv?dl=0
2 https://www.dropbox.com/s/tfsqu6ir90s682h/ErrorOnceSubmissionDone.wmv?dl=0
Thanks in advance
Naveen
use below code for sending email from nodemailer, inside function pass ur parameter and you will get ur result.
var AppConfig = {
'sendEmailID': 'useremail',
'sendEmailFromName': 'senderemail',
'sendEmailPassword': 'password'
}
function SendEmail(toEmail, Subject, html) {
// create reusable transporter object using the default SMTP transport
var transporter = nodemailer.createTransport({
host: 'smtp.gmail.com',
port: '587',
auth: {
user: "username",
pass: AppConfig.sendEmailPassword
},
secureConnection: 'false',
tls: {
ciphers: 'SSLv3'
}
});
// setup e-mail data with unicode symbols
var mailOptions = {
from: AppConfig.sendEmailFromName, // sender address
to: toEmail, // list of receivers
subject: Subject, // Subject line
html: html // html body
};
// send mail with defined transport object
transporter.sendMail(mailOptions, function (error, info) {
if (error) {
return console.log("ERROR----" + error);
}
console.log('Message sent: ' + info.response);
});
}
With the response I got from #chetan mekha I changed my code as follows:
var smtpTransport=nodemailer.createTransport({
host: 'smtp.gmail.com',
port: '587',
auth: {
user: "**",
pass: "**"
},
secureConnection: 'false',
tls: {
ciphers: 'SSLv3'
}
});
But there another error came up saying : { [Error: self signed certificate in certificate chain] code: 'ECONNECTION', command: 'CONN' } But adding the line
rejectUnauthorized: false under ciphers made it work! so final code snippet that worked for me looks like this..
var smtpTransport=nodemailer.createTransport({
host: 'smtp.gmail.com',
port: '587',
auth: {
user: "**",
pass: "**"
},
secureConnection: 'false',
tls: {
ciphers: 'SSLv3',
rejectUnauthorized: false
}
});

Resources