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?