No message is displayed for the error but also for the message for success is also not displayed. Also no email is recieved.
This is the code i had in the config file of protractor. Here there is no error response for the email but also there is no successful message also and no email is received.
this is in the config file
onComplete: function () {
creating reusable transport method (opens pool of SMTP connections)
var smtpTransport = nodemailer.createTransport("SMTP", {
host: "smtp.company.com",
port: "110",
auth: {
user: "anuvrat.singh#companyemail.com",
pass: "XXXX#2015"
}
});
setup the email data
var mailOptions = {
from: "anuvrat.singh#Company.com", // sender address
to: "anuvrat.singh#Company.com",
subject: "Hello ✔", // Subject line
text: "Hello world ✔", // plaintext body
html: "<b>Hello world ✔</b>" // html body
}
send mail with defined transport object
smtpTransport.sendMail(mailOptions, function (error, response) {
if there is an error it will display other wise it will send the message
if (error) {
console.log(error);
} else {
console.log("Message sent: " + response.message);
}
shut down the connection pool, no more messages
smtpTransport.close();
});
}
Related
When nodemailer sends out an email from a contact form, it goes to an admin, who then responds to the email address found in req.body.email
I'd like to set the reply-to address as this value, so that the admin can simply hit reply to send a reply with the message data, without having to copy paste the email into the 'to' field. Here's what I've got:
Send email util
const sendEmail = async options => {
const transporter = nodemailer.createTransport({
host: process.env.SMTP_HOST,
port: process.env.SMTP_PORT,
auth: {
user: process.env.SMTP_EMAIL,
pass: process.env.SMTP_PASSWORD
}
});
// send mail with defined transport object
let message = {
// from: `${options.fromName} <${process.env.FROM_EMAIL}>`,
from: `${options.fromName} <${options.fromEmail}>`,
to: options.email,
subject: options.subject,
text: options.message,
html: options.body
};
const info = await transporter.sendMail(message);
console.log('Message sent: %s', info.messageId);
};
module.exports = sendEmail;
Send email function
try {
await sendEmail({
fromName: emailBody.name,
fromEmail: emailBody.email,
email: 'admin#email.com',
subject: `${emailBody.name} sent a message!`,
body: emailBodyText
});
} catch (err) {
console.log(err);
return next(new ErrorResponse('Email could not be sent', 500));
}
Here's what I get:
response: '553 5.7.1 <user#gmail.com>: Sender address rejected: not owned by user admin#email.com'
I get that admin#email.com cannot send email from user#gmail.com, but I'd like to have that ux of being able to simply hit reply and have the email directed to the user. Is there a good way to make it work like this?
I have question about the nodemailer that I am working on. The result returns email sent,
but I do not get any email. I am sure I am doing something wrong and I have no idea what that is.
I will post my code below.
let transporter = nodemailer.createTransport({
//pool: true,
host: "*****.net",
port: ****,
secure: false,
auth: {
user: "****.com",
pass: "*****",
},
tls: {
// do not fail on invalid certs
rejectUnauthorized: false,
},
});
var mailOptions = {
from: '"xxx"<info#****.com>;', // sender address
to: result.recordset[0].toEmail, // list of receivers
subject: "NEW USER", // Subject line
html: result.recordset[0].content, // plain text body
};
transporter.sendMail(mailOptions, function (error, info) {
if (error) {
console.log(error);
} else {
console.log("Email sent: " + info.response);
}
});
transporter.verify(function (error, success) {
if (error) {
console.log(error);
} else {
console.log("Server is ready to take our messages");
}
});
AND THIS IS THE MESSAGE I GET FROM THE TERMINAL BELOW
Server is ready to take our messages
Email sent: 250 uPMSkJg8an7bs mail accepted for delivery
If you are using for example a cheap mail service, mails might be sent after some time. I had that problem before. Services use queue system and prioritize the mail requests based on the pricing. Other than that, you can check your spam folder just in case if that might be a problem.
I am trying to use session.send instead of console.log in transporter.sendMail, so that the user knows when the Email was sent successfully, but it does not work.
The error is "session is not defined".
This is how my code looks like:
var nodemailer = require('nodemailer');
// Create the transporter with the required configuration for Gmail
// change the user and pass !
var transporter = nodemailer.createTransport({
host: 'smtp.gmail.com',
port: 465,
secure: true, // use SSL
auth: {
user: 'myemail#gmail.com',
pass: 'myPassword'
}
});
// setup e-mail data
var mailOptions = {
from: '"Our Code World " <myemail#gmail.com>', // sender address (who sends)
to: 'mymail#mail.com, mymail2#mail.com', // list of receivers (who receives)
subject: 'Hello', // Subject line
text: 'Hello world ', // plaintext body
html: '<b>Hello world </b><br> This is the first email sent with Nodemailer in Node.js' // html body
};
// send mail with defined transport object
transporter.sendMail(mailOptions, function(error, info,session){
if(error){
return console.log(error);
}
session.send('Message sent: ' + info.response);
}
);
this is an example of how to doing it. Just make sure you call this method inside a session context like:
const sendmail = require('./email'); // in case you have the class called email
bot.dialog('/', function(session) {
sendmail.sendmail(session);
session.send("hello")
});
function sendmail(session){
var nodemailer = require('nodemailer');
// Create the transporter with the required configuration for Outlook
// change the user and pass !
var transport = nodemailer.createTransport( {
service: "hotmail",
auth: {
user: "",
pass: ""
}
});
// setup e-mail data, even with unicode symbols
var mailOptions = {
from: '"Our Code World " <shindar902009#hotmail.com>', // sender address (who sends)
to: 'shindar902009#hotmail.com', // list of receivers (who receives)
subject: 'Hello ', // Subject line
text: 'Hello world ', // plaintext body
html: '<b>Hello world </b><br> This is the first email sent with Nodemailer in Node.js' // html body
};
// send mail with defined transport object
transport.sendMail(mailOptions, function(error, info){
if(error){
return console.log(error);
}
session.send('Message sent');
});
}
module.exports.sendmail = sendmail;
transporter.sendMail(mailOptions, function(error, info , session)
You are not using a correct function definition, since the callback of transport.SendMail can only have two parameters (error & info). Have a look at the Nodemailer example.
In your case it would look like this. Just make sure you use this snippet where the session context is available.
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
return session.error(error);
}
session.send('Message sent: ' + info.messageId);
});
I just ran this snippet replacing the username and password appropriately and ended up with this:
{ Error: Invalid login: 534-5.7.14 <https://accounts.google.com/signin/continu> Please log in via
534-5.7.14 your web browser and then try again.
534-5.7.14 Learn more at
534 5.7.14 https://support.google.com/mail/answer/ - gsmtp
at SMTPConnection._formatError (C:\projects\nodemailertest\node_modules\nodemailer\lib\smtp-connection\index.js:605:19)
at SMTPConnection._actionAUTHComplete (C:\projects\nodemailertest\node_modules\nodemailer\lib\smtp-connection\index.js:1340:34)
at SMTPConnection._responseActions.push.str (C:\projects\nodemailertest\node_modules\nodemailer\lib\smtp-connection\index.js:378:26)
at SMTPConnection._processResponse (C:\projects\nodemailertest\node_modules\nodemailer\lib\smtp-connection\index.js:764:20)
at SMTPConnection._onData (C:\projects\nodemailertest\node_modules\nodemailer\lib\smtp-connection\index.js:570:14)
at TLSSocket._socket.on.chunk (C:\projects\nodemailertest\node_modules\nodemailer\lib\smtp-connection\index.js:522:47)
at emitOne (events.js:96:13)
at TLSSocket.emit (events.js:188:7)
at readableAddChunk (_stream_readable.js:176:18)
at TLSSocket.Readable.push (_stream_readable.js:134:10)
code: 'EAUTH',
response: '534-5.7.14 <https://accounts.google.com/signin/continue?..._sniped> Please log in via\n534-5.7.14 your web browser and then try again.\n534-5.7.14 Learn more at\n534 5.7.14 https://support.google.com/mail/answer/78 - gsmtp',
responseCode: 534,
command: 'AUTH PLAIN' }
Furthermore I received an email from Google:
Someone just used your password to try to sign in to your account from a non-Google app. Google blocked them, but you should check what happened. Review your account activity to make sure no one else has access.
Are you sure this is the code that you're running? The error you posted, "session is not defined" seems like a syntax error.
var nodemailer = require('nodemailer');
// Not the movie transporter!
var transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: '*****#gmail.com', // Your email id
pass: '*******' // Your password
}
});
var mailOptions = {
from: varfrom_name, // sender address
to: varto, // list of receivers
subject: varsubject, // Subject line
text: vartext, // plaintext body
html: varhtml // html body
};
console.log(mailOptions);
// send mail with defined transport object
transporter.sendMail(mailOptions, function (error, info) {
if (error) {
return console.log(error);
}else{
return console.log(info);
}
});
I want different sender address from the authenticated one ?
Suppose I authenticated data with abc#gmail.com, but I want to send the mail from xyz#gmail.com to def#gmail.com.
How to do that in node-mailer ?
// Using send mail npm module
var sendmail = require('sendmail')({silent: true})
sendmail({
from: ' xyz#gmail.com',
to: 'def#gmail.comh',
subject: 'MailComposer sendmail',
html: 'Mail of test sendmail ',
attachments: [
]
}, function (err, reply) {
console.log(err && err.stack)
console.dir(reply)
})
But the mails coming in the span box and the mails that we are sending is won't showing in the sent mail of sender mail address ?
I hope i will able to elaborate my question
I don't think you can. Gmail does not allow the change of the sender address.
However, you can look for another service, like postmark, sendgrid, or create your own smtp server.
I am testing sending email with meteor js and nodemailer plugin:
meteor add mrt:meteor-nodemailer
when the page loaded, i saw error in the console of the navigator :
Cannot read property 'createTransport' of undefined.
so what is the problem ?
this is the code :
///////////////////////////////////////////
var nodemailer = Nodemailer;
var smtpTransport = nodemailer.createTransport("SMTP",{
service: "Gmail",
auth: {
user: "myname#gmail.com",
pass: "mypass"
}
});
var emailNodemailer = function() {
// setup e-mail data with unicode symbols
var mailOptions = {
from: "Sender Name ✔ ", // sender address
to: "someone#yahoo.fr", // list of receivers
subject: "Hello ✔", // Subject line
text: "Hello world ✔", // plaintext body
html: "Hello world ✔" // html body
};
// send mail with defined transport object
smtpTransport.sendMail(mailOptions, function(error, response){
if(error){
console.log(error);
}else{
console.log("Message sent: " + response.message);
}
// if you don't want to use this transport object anymore, uncomment following line
//smtpTransport.close(); // shut down the connection pool, no more messages
});
};
///////////////
This worked for me import * as nodemailer from 'nodemailer';