Nodemailer Error: Recipient command failed: 553 5.7.1: Sender address rejected: not owned by user

Viewed 970

I have been searching a lot for an answer, but I did not find my mistake yet. So maybe you can help me with this error I get when trying to send a mail from an unknown mail via nodemailer to my personal mail (for a contact form on my website):

api/mail.js

import nodemailer from 'nodemailer';
import {valueMissing} from "../../utils/functions";
export default function sendMail (req,res){
    if (valueMissing(req.body.name) || valueMissing(req.body.subject) ||
        valueMissing(req.body.message) || valueMissing(req.body.mail))
        return res.status(202).json({ success: false });
    else {
        const transporter = nodemailer.createTransport({
            port: 465,
            host: 'smtp.xxx.xxx',
            auth: {
                user: process.env.MAIL_USER,
                pass: process.env.MAIL_PW
        },
        });
        const message = {
            from: {
                name: req.body.name,
                address: req.body.mail
            },
            to: process.env.MAIL_USER,
            subject: req.body.subject,
            text: req.body.message
        };

        transporter.sendMail(message,error => {
            if(error) {
                console.log(error);
                return res.status(202).json({
                    success: false
                });
            } else return res.status(200).json({
                    success: true
                });
        })
    }
}

I have worked with nodemailer before, but back then I was sending mails from my mail to another person, not the other way around. In my research I found this tutorial - Creating A Contact Form With JavaScript / Nodemailer. What am I doing different? Seems like it should be working either way...

Thanks in advance :)

1 Answers

Okay I found the solution, for anyone with the same issue: In fact, Nodemailer can not send mails from another person´s e-mail. So I changed my message options to the following:

const message = {
  from: {
    name: req.body.name,
    address: process.env.MAIL_USER,
  },
  to: process.env.MAIL_USER,
  subject: req.body.subject,
  text: req.body.message,
  replyTo: req.body.mail
}

Basically I send the mail from my mail to the same mail. To keep the person´s mail for replies I added the replyTo option. Now it works like a charm!

Related