I'm using nodemailer to try to send an email to myself via commandline:
var nodemailer = require('nodemailer');
// config
var smtpConfig = {
host: 'smtp.myhost.com',
port: 465,
secure: false, // dont use SSL
tls: {rejectUnauthorized: false}
};
// create reusable transporter object using the default SMTP transport
var transporter = nodemailer.createTransport(smtpConfig);
// setup e-mail data with unicode symbols
var mailOptions = {
from: '"Fred Foo 👥" <foo#blurdybloop.com>', // sender address
to: 'person#gmail.com', // list of receivers
subject: 'Hello ✔', // Subject line
text: 'Hello world 🐴', // plaintext body
html: '<b>Hello world 🐴</b>' // html body
};
// send mail with defined transport object
transporter.sendMail(mailOptions, function(error, info){
if(error){
return console.log(error);
}
console.log('Message sent: ' + info.response);
});
When I try to run this code I get the following error:
Error: Invalid login: 535-5.7.8 Username and Password not accepted. Learn more at\n535 5.7.8 https://support.google.com/mail/answer/14257
The link takes you to a page that tells you to register your app inside Google Console. But this is not what I'm trying to do.
There are loads of email clients that can send an email to a gmail inbox without having to sign into that email account. This is what I'm trying to do. I'm trying to turn my terminal into an smtp client that can send a mail message to any inbox. This shouldn't require extensive authentication. How do I do this?
NOTE
Just to provide some perspective, I'm trying to replicate in node whats possible with the unix sendmail command:
sendmail person#gmail.com < testemail.txt
How can I do this using nodemailer?
Try port 587.
Here are my gmail settings.
smtpServerName="smtp.gmail.com"
portNumber="587"
authenticationMode="SSL"
smtpUserName="somebody#gmail.com"
smtpUserPassword="xxxxxxxxxx"
I would experiment with ssl: false and ssl: true
nodemailer.SMTP = {
host: 'smtp.gmail.com',
port: 587,
ssl: false,
use_authentication: true,
user: 'my.username#gmail.com',
pass: 'my.password'
}
Make sure you read this:
https://support.google.com/accounts/answer/6010255
It looks like there are two ways with gmail.
Port 587 is (non ssl) but with TLS.
Port 465 is SSL (but not TLS).
Go figure.
//singleServer = new SmtpServerSettings("smtp.gmail.com",
// "myname#gmail.com", "587", AuthenticationType.Basic,
// "myname#gmail.com", "mypassword", "");
//singleServer = new SmtpServerSettings("smtp.gmail.com",
// "myname#gmail.com", "465", AuthenticationType.SSL,
// "myname#gmail.com", "mypassword", "");
Related
I have this:
app.post('/sendSessionConformationEmail', (req, res) => {
if (!req.session.loggedin) {
res.redirect('/login')
req.session.destroy()
}
var email = req.body.email;
var name = req.body.name;
var trainername = req.body.trainername;
var sessionDate = new Date().toLocaleString();
console.log(email)
console.log(name)
console.log(trainername)
console.log(sessionDate)
// async..await is not allowed in global scope, must use a wrapper
async function main() {
// Generate test SMTP service account from ethereal.email
// Only needed if you don't have a real mail account for testing
let testAccount = await nodemailer.createTestAccount();
// create reusable transporter object using the default SMTP transport
let transporter = nodemailer.createTransport({
host: "smtp.gmail.com",
port: 587,
secure: false, // true for 465, false for other ports
auth: {
user: "myemail#gmail.com",
pass: "myauthpassword",
},
tls:{
rejectUnauthorized:false
}
});
// send mail with defined transport object
let info = await transporter.sendMail({
from: 'my2email#gmail.com', // sender address
to: "gianluca#my3email.com", // list of receivers
subject: "Hello ✔", // Subject line
text: "Hello world?", // plain text body
});
console.log("Message sent: %s", info.messageId);
// Message sent: <b658f8ca-6296-ccf4-8306-87d57a0b4321#example.com>
// Preview only available when sending through an Ethereal account
console.log("Preview URL: %s", nodemailer.getTestMessageUrl(info));
// Preview URL: https://ethereal.email/message/WaQKMgKddxQDoou...
}
main().catch(console.error);
})
I copied the code straight from the nodemailer website, and made the necessary changes. However, when I run this code, nothing sends, and it actually gives me this error:
Error: connect ETIMEDOUT 13.49.22.0:443
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1146:16) {
errno: -60,
code: 'ETIMEDOUT',
syscall: 'connect',
address: '13.49.22.0',
port: 443,
type: 'FETCH',
sourceUrl: 'https://api.nodemailer.com/user'
}
I've been using nodemailer with this code for over 2 years, never gotten this error, and it is killing me. Is anyone able to help?
you should allow third party apps use your gmail.
https://support.google.com/accounts/answer/3466521?hl=en
ethereal.email (13.49.22.0) is down today. Runs on AWS.
I have set up an email server on my host. It's basically a SMTP server that listens on port 25.
const recvServer = new SMTPServer({
requireTLS: true,
authOptional: true,
logger: true,
onConnect(session, callback) {
return callback();
},
onMailFrom(address, session, callback) {
console.log('from', address, session);
return callback();
},
onData(stream, session, callback) {
console.log('new msg');
let message = '';
stream.on('data', chunk => {
message += chunk;
});
stream.on('end', () => {
callback(null, 'Message queued');
simpleParser(message)
.then(parsed => {
console.log(parsed);
// here I wish to forward the message to outside gmail addresses
})
.catch(err => {
console.log(ee)
});
});
}
});
recvServer.listen(25);
recvServer.on('error', err => {
console.log(err.message);
});
It works fine for receiving emails from outside, like gmail etc.
But I want to be able to send emails outside also, or forward emails that I receive to some gmail addresses.
I know that I can do that using Gmail SMTP servers, but then I need an gmail account and password.
I want to be able to send email with my own server, just like yahoo sends mail to gmail using their own server not gmail accounts :)
You need a MTA (Mail Transfer Agent) in order to send an email.
So the popular options is: Postfix, here a guid how to setup postfix on ubuntu: https://help.ubuntu.com/community/Postfix
Or you can spin up a docker container like: https://hub.docker.com/r/bytemark/smtp/
Then you can use nodemailer to send emails through postfix or docker instance.
And if you want there is a full stack docker image all batteries included: https://github.com/tomav/docker-mailserver
Technically you can use NodeMailer for sending emails.
"use strict";
const nodemailer = require("nodemailer");
// async..await is not allowed in global scope, must use a wrapper
async function main(){
// Generate test SMTP service account from ethereal.email
// Only needed if you don't have a real mail account for testing
let testAccount = await nodemailer.createTestAccount();
// create reusable transporter object using the default SMTP transport
let transporter = nodemailer.createTransport({
host: "smtp.ethereal.email",
port: 587,
secure: false, // true for 465, false for other ports
auth: {
user: testAccount.user, // generated ethereal user
pass: testAccount.pass // generated ethereal password
}
});
// send mail with defined transport object
let info = await transporter.sendMail({
from: '"Fred Foo 👻" <foo#example.com>', // sender address
to: "bar#example.com, baz#example.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);
// Message sent: <b658f8ca-6296-ccf4-8306-87d57a0b4321#example.com>
// Preview only available when sending through an Ethereal account
console.log("Preview URL: %s", nodemailer.getTestMessageUrl(info));
// Preview URL: https://ethereal.email/message/WaQKMgKddxQDoou...
}
main().catch(console.error);
As Sohail mentioned in the above answer you can use NodeMailer (https://nodemailer.com/about/) and its fairly easy. Below is a simple code snippet of using it. In addition there are other services that provided free tiers if you are looking for fancier services(examples are SendGrid (https://sendgrid.com/)).
async function sendemail(email, host , email_body) {
let transporter = nodemailer.createTransport({
host: "mail.example.com.au",
port: 465,
secure: true, // true for 465, false for other ports
auth: {
user: "test#example.com.au", // generated ethereal user
pass: "email_password" // generated ethereal password
}
});
let emailBody = email_body // This is the body of your email which is more or less a String
let info = await transporter.sendMail({
from: '" Name of sender" <test#example.com.au>', // sender address
to: email, // list of receivers
subject: "Email Subject", // Subject line
text: emailBody, // plain text body
// html: // html body
});
console.log("Message sent: %s", info.messageId);
return true;
}
In order to send email, you need an email client, not a server. Writing the client is usually easier than writing the server, so you could do that. If you don't want to write your own, you could use an MTA (like Postfix), which will contain both a client and a server.
You can solve this function by creating an account in SendGrid and then using its Node.js api to send emails.
These are the steps:-
npm install #sendgrid/mail
And then use this following code snippet :-
const mailer = require('#sendgrid/mail');
mailer.setApiKey(<YOUR_SENDGRID_API_KEY>);
const message = {
to: 'xyz#gmail.com',
from: 'abc#gmail.com',
subject: 'hello world',
text: 'hello world'
};
mailer.send(message);
You can get more samples and documentation in their Github repo.
Iam tryin to use nodemailer to send mails using both my local system as well as on the heroku app but in both case iam getting timeout here is my code , now i have tried using both gmail as well as smtp email but still nothing works
exports.sendMail=function(req,res)
{
if(req.METHOD=="POST")
{
var email=req.body.email;
var name=req.body.name;
var phone=req.body.phone;
var content=`
<ul>
<p>You have New Enquiry</p>
<h3>Contact Details</h3>
<ul>
<li>Email:email</li>
<li>Name:name</li>
<li>Phone:phone</li>
</ul>
`;
let transporter=nodemailer.createTransport({
host:'chi-node30.websitehostserver.net ',
port:465,
secure:true,
auth:
{
user:'chiragunplugged#chiragunplugged.com',
pass:'foo123'
},
tls:{
rejectUnauthorized:false
}
});
let mailOptions={
from:'<chiragunplugged#chiragunplugged.com',
to:'atul.11192#gmail.com',
subject:'Enquiry from datadock',
text:'you have got enquiry',
html:content
};
transporter.sendMail(mailOptions,(err,info)=>{
if(err){
res.render('final.ejs',{message:err});
return console.log(err);
}
var success="message sent";
res.render('final.ejs',{message:success});
});
}
};
please go through the code and let me know what changes I can make to get this working.
It's likely there your not actually running an SMTP server on either Heroku or from localhost, nodemailer does not do this for you. You can use googles free smtp server as detailed here https://www.digitalocean.com/community/tutorials/how-to-use-google-s-smtp-server Which will transport your mail.
let transporter = nodemailer.createTransport({
host: 'smtp.gmail.com',
port: 465,
secure: true, // true for 465, false for other ports
auth: {
user: account.user, // generated ethereal user
pass: account.pass // generated ethereal password
}
});
Change your transporter to the above and also provide gmail login details, then providing you have the same details inside your mail options it will show up from chiragunplugged#chiragunplugged.com as described in the options.
In the localhost its not working because a secure and safetly connection is required for sending an email but using gmail[smtp(simple mail transfer protocol)] we can achieve it functionality.
Don't forget to first do the setting - Allow less secure apps to access account.
its given permission for access gmail account.
by default this settings is off and you simply turn it on. Now move on coding part.
//////////////////////----------------------------------------------------------------////////////////////////////////////
const nodemailer = require('nodemailer');
let transporter = nodemailer.createTransport({
host: 'smtp.gmail.com',
port: 587,
secure: false,
requireTLS: true,
auth: {
user: 'your.gmail.account#gmail.com', // like : abc#gmail.com
pass: 'your.gmailpassword' // like : pass#123
}
});
let mailOptions = {
from: 'your.gmail.account#gmail.com',
to: 'receivers.email#domain.com',
subject: 'Check Mail',
text: 'Its working node mailer'
};
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
return console.log(error.message);
}
console.log('success');
});
I'm trying to use nodemailer (https://www.npmjs.com/package/nodemailer) to send e-mail using a local windows server we have, with an SMTP Virtual Server.
The SMTP Virtual Server works fine, we already use Jmail in Classic ASP, pass it the server name, and email gets sent.
var nodemailer = require('nodemailer');
var options = {
host: 'SERVERNAME'
};
var transporter = nodemailer.createTransport(options);
var email = {
from: 'no-reply#domain.co.uk',
to: 'webmaster#domain.co.uk',
subject: 'hello',
text: 'hello world!'
};
var callback = function(err, info){
if (err) { throw err }
console.log('sent');
}
transporter.sendMail(email, callback);
The error I get back from this is:
Can't send mail - all recipients were rejected: 550 5.7.1 Unable to
relay for webmaster#domain.co.uk
If I update the options object to include auth, like this:
var options = {
host: 'SERVERNAME',
auth: {
user: '...',
pass: '...'
}
};
I get this error:
Invalid login: 504 5.7.4 Unrecognized authentication type
How can I use nodemailer to send e-mail using our IIS SMTP Virtual Server..?
I still don't know why the above doesn't work, pointing nodemailer to our SMTP Virtual Server. However I've worked around it.
Our SMTP Virtual Server is setup to use our Office 365 account, so I've updated my nodemailer code to go to Office 365 direct, rather than via our local server.
var options = {
host: 'SMTP.office365.com',
port: 25,
secure: false,
ignoreTLS: false,
auth: {
user: '...',
pass: '...'
},
tls: {
ciphers: 'SSLv3'
}
}
This works.
app.get('/email', function(req,res){
var emailjs = require('emailjs');
var email_server = emailjs.server.connect({
host: 'smtp.gmail.com',
ssl: true,
tls: true,
port: "465",
user:'kateholloway#gmail.com',
password:'mypassword',
});
var h={
text: 'hey how you doing',
from: 'Kate Holloway <kateholloway#gmail.com>',
to: 'someonesemail#gmail.com',
subject: 'where is your phone'
};
var message = emailjs.message.create(h);
email_server.send(message, function(err,message){
console.log(err);
console.log(message);
res.send('ok');
});
});
Is this the right settings to do it?
The error message I get is:
{ [Error: connection has ended] code: 9, smtp: undefined }
Note: this code works with my own domain/server. But it doesn't work with Gmail. Therefore, I think my settings for Gmail smtp are incorrect.
We have used this library with Gmail. We have only set host, user, password and ssl options.
Its working as expected. Try removing other options.