How can I get my nodemailer email as a variable? - javascript

I'm using the nodemailer module with node.js and have done the following:
let transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: 'email#gmail.com',
pass: 'mypassword'
}
});
To declare the transporter. My question is if I can access the email - 'user' as a variable to save me updating it everywhere if I need to change it. I've tried to do
console.log(transporter.auth.user);
but I get 'Cannot read property 'user' of undefined.'

What you are looking for is :
let transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: 'email#gmail.com',
pass: 'mypassword'
}
});
console.log(transporter.transporter.auth.credentials);
Will give you :
Object {pass: "mypassword", user: "email#gmail.com"}
Like our friend Sapher stated in the comment :
console.log(transporter.options.auth);
will also return the same Object (containing the pass & user)

You can simply use a variable for that.
const email = "myemail#gmail.com";
let transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: email,
pass: 'mypassword'
}
});
// ...
console.log(email);

Related

How to send mail using nodemailer and Zoho mail?

I'm trying to setup ZOHO mail with Nodemailer. The mail is configured correctly and I'm using following code to send the mail, but still getting error in sending mail:
const nodemailer = require('nodemailer');
let from = `Company Name <contact#company.com>`
let transporter = nodemailer.createTransport({
// host: "smtp-mail.gmail.com",
host: 'smtp.zoho.com',
port: 465,
secure: true,
auth: {
user: "contact#company.com",
pass: "mypassword"
}
});
// Mail response to User
const mailResponse = {
from: from,
to: `userName`,
subject: "📞 Thanks For Connecting With Company Name",
html: // mail body
}
try {
await transporter.sendMail(mailResponse);
res.status(200).json({ message: "Message Sent" });
} catch (err) {
res.status(400).json({ message: "Unexpected Error!!! Please try again" });
}
Please let me know how can I fix this issue. i have tried every possible solution given on website.
Try this -
service: "gmail",
host: "smtp.gmail.com",
auth: {
user: "username#company.com",
pass: "yourpassword"
}
At the place of user put your email and in pass but your password or you can get these from env to be safe.

Unable to send email nodemailer / office365

Im unable to use nodemailer to send emails through office365 using following code:
var transporter = nodemailer.createTransport({
host: 'smtp.office365.com',
port: '587',
auth: {
user: 'user#domain.com',
pass: 'userpassword',
},
});
I have tried with different flags for nodemailer but nothing works:
secureConnection: false,
port: 587,
tls: {
ciphers:'SSLv3'
I have setup app-password in office 365 and can send email with following pyhton code:
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
username = "user#domain.com"
password = "userpassword"
mail_from = "email#email.com"
mail_to = "email#email.com"
mail_subject = "Test Subject"
mail_body = "This is a test message"
mimemsg = MIMEMultipart()
mimemsg['From']=mail_from
mimemsg['To']=mail_to
mimemsg['Subject']=mail_subject
mimemsg.attach(MIMEText(mail_body, 'plain'))
connection = smtplib.SMTP(host='smtp.office365.com', port=587)
connection.starttls()
connection.login(username,password)
connection.send_message(mimemsg)
connection.quit()
You can also use the "Outlook365" service of Nodemailer, which sets up the connection options for you.
Example
let transporter = nodemailer.createTransport({
service: 'Outlook365', // no need to set host or port etc.
auth: {
user: 'account.email#example.com',
pass: 'smtp-password'
}
});
Check the Nodemailer services

Nodemailer error (Error: queryA EREFUSED)

sorry for my english
I am using nodemailer and when querying through the api it gives this error:
Error: queryA EREFUSED smtp.gmail.com
at QueryReqWrap.onresolve [as oncomplete] (dns.js:210:19) {
errno: undefined,
code: 'EDNS',
syscall: 'queryA',
hostname: 'smtp.gmail.com',
command: 'CONN'
}
My code:
this.transporter = nodemailer.createTransport({
service: 'gmail'
auth: {
user: mygmail#gmail.com,
pass: mypass,
},
})
UPD: I connected the phone to the Internet instead of my LAN cable, checked, everything worked. There was no error, I connected the LAN back, disconnected the phone, this error appeared again
Make sure that Less secure app access is on in your Gmail account:
Less secure app access > ON
Then fix your code:
this.transporter = nodemailer.createTransport({
service: "gmail",
auth: {
user: email,
pass: password
}
});
You can use the built-in service in the Nodemailer and make your code simpler with this one.
this.transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: mygmail#gmail.com,
pass: mypass,
},
})
Please make sure you have disabled 2-Step verification.

From in the message appearing wrong

How do I make the email of who sent it from appear? Currently, when the person sends the email, it appears that the person who sent the email was the person who received it
let transporter = nodemailer.createTransport({
service: "gmail",
host: "smtp.gmail.com",
auth: {
user: "***********#gmail.com",
pass: "********",
},
});
const message = {
from: `${request.body.name} <${request.body.email}>`,
to: "**********#gmail.com",
cc: request.body.email,
subject: request.body.subject,
html: `Message`,
};
You cannot send an email on behalf of the user without having access to his account, setting the from field will not change the fact that you are the sender. To try and see the difference, send the email from the account you used for the trasporter, to another account of yours.
let transporter = nodemailer.createTransport({
service: "gmail",
host: "smtp.gmail.com",
auth: {
user: "yourFirstEmail#gmail.com",
pass: "********",
},
});
const message = {
from: "yourFirstEmail#gmail.com"`,
to: "yourSecondEmail#gmail.com",
cc: request.body.email,
subject: request.body.subject,
html: `Message`,
};
This should work.

Nodemailer, when I get an email the from is incorrect?

I thought this was working fine until I realized when I get the message in gmail the sender(from) is me myemail#gmail.com? It should be coming from req.body.email right?
router.post('/send_message', function(req,res) {
// create reusable transporter object using SMTP transport
var transporter = nodemailer.createTransport({
service: 'Gmail',
auth: {
user: 'myemail#gmail.com',
pass: 'pass'
}
});
transporter.sendMail({
from: req.body.email,
to: 'myemail#gmail.com',
subject: req.body.subject,
text: req.body.message
}, function(err, info) {
res.send('Emailed successfully');
});
});

Categories

Resources