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.
Related
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.
I am using the nodemailer module to send emails through my gmail account.
The problem i am facing is that they are sent from my account but never received by the other mail.
Whats more confusing to me is that when I send the mail to the same mail as the sender than it works, but that is of course not what I want.
Any one knows how to fix this?
Here is what i tried:
const transporter = nodemailer.createTransport({
name: "smtp.gmail.com",
host: "smtp.gmail.com",
service: 'gmail',
auth: {
user: 'email',
pass: 'pass'
},
secure: false,
logger: true,
debug: true
});
const mailOptions = {
from: '...',
to: '...',
subject: '...',
html: '...'
};
transporter.sendMail(mailOptions, function (error, info) {
if (error) {
console.log(error);
} else {
console.log('Email sent: ' + info.response);
}
});
No errors and when i look in my mail account it shows that it was sent
I'm trying to send email using node.js and nodemailer but when I'm pressing the submit button its just loading and loading and in the end gives me a "504 Gateway Timeout Error" in the server, and "page is not working" locally.
I'm using the following code:
app.post("/postmail", function (req, res) {
var transporter = nodemailer.createTransport({
host: "smtp.mailtrap.io",
port: 2525,
secure: false,
debug: true,
auth: {
user: "xxx",
password: "xxx",
},
});
var message = {
from: "a#b", // Sender address
to: "b#c", // List of recipients
subject: "Design Your Model S | Tesla", // Subject line
text: "Have the most fun you can in a car. Get your Tesla today!", // Plain text body
};
transporter.sendMail(message, function (err, info) {
if (err) {
console.log(err);
} else {
console.log(info);
res.render("landing");
}
});
});
I must also mention that, I tried multiple smtp servers with multiple ports and configuration.
does someone know what to do?
thanks
cuase all of the smtp providers use tls/ssl for secuirity reasons. try to use secure configuration:
var transporter = nodemailer.createTransport({
host: 'smtp.mailtrap.io',
port: 465,
secure: true,
debug: true,
auth: {
user: 'xxx',
password: 'xxx'
}
});
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);
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');
});
});