How to configure email headers with node js - node.js

function sendmail(req,res){
const transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: 'myemail#gmail.com',
pass: 'mypassword'
}
});
const mailOptions = {
from: 'Something',
to: 'to_email',
subject: 'My subject',
html: `<button>My Button</button>`
};
transporter.sendMail(mailOptions)
}
When I send the email everything arrives without problems, however, for the email from it doesn't work, when the email I received arrives, the email from which should be Something is the email of the sender the email, so basically if I send an email to 123#outlook.com from my email 123#gmail and set the from to something when I send and the email is received , the email from is not something, but 123#gmail which is the email responsible for sending it. How can I fix this to display Something instead of the email responsible for sending

Related

Receiving email from users by nodemailer

I am trying to receive email from users using nodemailer. The email will be sent from users email and received by my email. But all it doing is sending email from my email to my email though I set different email account for from and to. But when I revesre the email acounts and set from:myemail#example.com and to:usersemail#example.com this works fine. Why this is happening? How can I fix this so that I can receive emails from users? Here is the code:
// create reusable transporter object using the default SMTP transport
let transporter = nodemailer.createTransport({
service: "gmail",
secure: false,
auth: {
user: process.env.MY_EMAIL,
pass: process.env.MY_PASSWORD,
},
});
// Creating the email
let info = {
from: process.env.USERS_EMAIL,
to: process.env.MY_EMAIL,
subject: 'Demo message',
text: 'For clients with plaintext support only',
html: `
<p>This is a demo email</p>
`
};
//send the email
transporter.sendMail(info, function (err, info) {
if (err) {
console.log(err);
}
else{
console.log(info);
}
})

Nodemailer fails to send email to outlook address

I am trying to use nodemailer using my Gmail address. It works when an email is sent to Gmail address but when I try to send mail to any Outlook address, it gets lost in the matrix. Help is appreciated. Thanks in advance.
let transporter = nodemailer.createTransport({
service: "gmail",
auth: {
user: "Mysendergmail", // generated ethereal user
pass: "password", // generated ethereal password
}
});
transporter.sendMail({
from: '"service#onlineshop" <Mysendergmail>', // sender address
to: "myoutlookreciever", // list of receivers
subject: "Hello ✔", // Subject line
text: "Hello world?", // plain text body
html: "<b>Hello world?</b>", // html body
},(err,info)=>{
if(err){
res.status(400).json(err)
}
else res.status(200).json("sent email")
});

this is my nodemailer program Even if i give wrong mail in TO addrress also it is showing message sent how to rectifyy that

const nodemailer = require('nodemailer');
let mailTransporter = nodemailer.createTransport({
host: 'smtp.gmail.com',
port: 587,
secure: false,
auth: {
user: '*************',
pass: '*********',
},
});
let mailDetails = {
from: '********',
to: 'uuwdwuvw#', //guguygugiug
subject: 'Test mail',
//text: 'redeem your gift',
html: 'redeem your coupon code ',
};
mailTransporter.sendMail(mailDetails, function (err, data) {
if (err) {
console.log('Error Occurs');
} else {
console.log('Email sent successfully');
}
});
this is my nodemailer program Even if i give wrong mail in TO addrress also it is showing message sent how to rectifyy that
Nodemailer itself can't tell if an email has been delivered or not. It can tell you only if the email has been sent. There's a difference between email being sent and being delivered. Detecting bounced emails is out of scope for the Nodemailer.
In order to do that you need to implement your own bounced email mechanism, probably with your own SMTP server. It's not an easy thing to do, so you should probably use some real email provider that has this functionality instead of Gmail (Google likes to block such applications).
Have a look at the similar question I've found on GitHub and on SO.

Nodejs controller sends the wrong data with Nodemailer

We are trying to create a contact form. the problem is that Node mailer's from data shows my own while I set to the persons email who sends it. Here is the code:
app.post('/api/v1/contact', (req, res) => {
var data = req.body;
var smtpTransport = nodemailer.createTransport({
service: 'Gmail',
port: 465,
auth: {
user: '',
pass: ''
}
});
var mailOptions = {
from: data.email,
to: 'myemail#domain.com',
subject: data.title,
html: `<p>${data.email}</p>
<p>${data.message}</p>`,
...
...
data.email inside the HTML tag inside email body shows correctly the email of the sender while in the from field the same data.email shows my own email. It looks like I am sending email to me. What's the problem?
That is not a bug. You simply not have access to send emails from someone else email address.
The purpose of contact form in any website to make a communication from the customer to the site owner.
You can not make customer to send email using contact page.You can respond to their email.
So I will suggest you put customer email which you are getting in req.body.email parameter so that you can get to know who is trying to make contact.
It is better to give Website admin /support email address in
auth: {
user: '',
pass: ''
}
user attribute and the password for the same in pass attribute.
It is not the issue of nodemailer.

Nodemailer does'nt work properly

The email comes from my e-mail address rather than the sender. Any ideas?
var transporter = nodemailer.createTransport({
service: 'Gmail',
auth: {
user: 'mail',
pass: 'pass'
}
});
var mailOptions = {
from: req.body.email , // sender address
to: 'mail', // list of receivers
subject: ' ', // Subject line
text: req.body.message,
html: '<p>'+req.body.message+'</p>'// plain text body
};
This is how Gmail works. It won't let you to send emails as anyone but you, for security reasons.
If you need more flexibility then you should use a transactional email service like Mailgun, Mandrill or SendGrid - which you can easily use with Nodemailer (there are Nodemailer transports available for them, just like for Gmail).
See: https://www.npmjs.com/browse/keyword/nodemailer

Resources