Receiving email from users by nodemailer - node.js

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);
}
})

Related

Why email is not receving if sent successfully using nodemailer in node js?

import nodemailer from "nodemailer";
// transporter object
const transporter = nodemailer.createTransport({
host: "smtp.gmail.com",
port: 587,
secure: false, // true then port will change
requireTLS:true,
auth: {
user: "mygmail#gmail.com",
pass:"myapppassword" //google app password
}
})
// mail object
const mailOptions = {
name:"hello",
from: "my#gmail.com",
to: "myfriend#gmail.com", //allowed comma separated mutiple emails
subject: "Node js mail text",
text: "hello buddy aapke father aaye hain!"
}
// send mail
transporter.sendMail(mailOptions, (err, res) => {
if (err) {
console.log(err)
} else {
console.log(res.response)
}
})
In the above code snippet i tried send text email to another account and it is sent successfully but i am not receiving the email but when i try to send the email to the same email it is getting inside the inbox , why it is happening ? Thank you

How to configure email headers with 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

E-mail getting rejected sent by Nodemailer

I am trying to send an email which includes HTML content with the help of nodemailer-express-handlebars. Every time my mail gets blocked by the Gmail which can be checked in sender's Gmail sent-box whereas I got success msg from nodemailer
Email sent: 250 2.0.0 OK 1595608108 i66sm6757247pfc.12 - gsmtp
I am unable to understand why this happening as when I send a mail with text, it gets delivered.
NODEMAILER CODE
sendMail=(email)=>{
var transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: 'emailId',
pass: 'password'
}
});
transporter.use('compile',hbs({
viewEngine:{
partialsDir:"./views/",
defaultLayout: "",
layoutsDir: "",
},
viewPath:"./views/",
extName:'.hbs',
}))
var mailOptions = {
from: '<xyz#gmail.com>',
to: email,
subject: 'Your order has been placed successfully.',
template:'mail',
context:{
name:"XYZ",
address:"133"
}
};
transporter.sendMail(mailOptions, function(error, info){
if (error) {
console.log(error);
} else {
console.log('Email sent: ' + info.response);
}
});
}
It's recommended to use OAuth2 with NodeMailer and Gmail. Using the plain username and password might be what's causing you problems.

How to update insert records into database with every replied-to email?

I'm testing sending emails to myself with Nodemailer, and I have the basic setup,
const transporter = nodemailer.createTransport({
service: "gmail",
auth: {
user: "foo#gmail.com",
pass: "foobar"
}
});
const mailOptionsUser = {
from: "John Doe <foo#bar.com>",
to: 'bar#foo.com',
subject: "Test | New email",
html: `<h3>test</h3>`
};
transporter.sendMail(mailOptionsUser, function(error, info) {
if (error) {
console.log(error);
} else {
console.log("Email sent: " + info.response);
}
});
I want to be able to track conversations on my web app, so that users can send emails to themselves, and they'll be able to see those messages when they log into the app.
I was thinking of using the replyTo feature, but that won't do what I need -- is there any way to update the database with every sent email (that isn't sent through Nodemailer)?

Sending email from a non existing mail id (noreply#myserver.com) in Nodejs using nodemailer npm

Below is the sample code am using for sending mail.
var nodemailer = require('nodemailer');
var transporter = nodemailer.createTransport({
host: 'smtp.office365.com',
port: 587,
auth: {
user: 'me#myserver.com',
pass: 'mypassword'
}
});
var mailOptions = {
from: 'no-reply#myserver.com', //It will work if i give me#myserver.com but i need no-reply#myserver.com in from option.
to: 'someuser#gmail.com',
subject: 'Sending Email using Node.js',
text: 'That was easy!'
};
transporter.sendMail(mailOptions, function(error, info){
if (error) {
console.log(error);
} else {
console.log('Email sent: ' + info.response);
}
});
I will give me below error if i use no-reply#myserver.com in from option.
Error: Message failed: 554 5.2.0
STOREDRV.Submission.Exception:SendAsDeniedException.MapiExceptionSendAsDenied;
Failed to process message due to a permanent exception with message
Cannot submit message.
Some email servers do not accept changing FROM address. This is not about nodemailer. You need to check your email server configuration.

Resources