0

I keep getting the error:

Mail command failed: 554 5.7.8 User [contact@example.com] not authorized to send on behalf of <test@test.com>

This is my code:

api/contact.js

import nodemailer from "nodemailer"

export default async (req, res) => {
    const { name, email, phone, message} = req.body;
    const transporter = nodemailer.createTransport({
        host: "send.one.com",
        port: 465,
        secure: false,
        auth: {
            user:'contact@example.com',
            pass: 'password'
        },
        tls: {
            rejectUnauthorized: false
        }
    });

    try {
        await transporter.sendMail({
            from: {
                name: req.body.name,
                address: email
            },
            to: 'contact@example',
            subject: `Contact form submission from ${name}`,
            html: `<p>You have received a contact form submission</p><br>
            <p><strong>Email: </strong> ${email}</p><br>
            <p><strong>Phone: </strong> ${phone}</p><br>
            <p><strong>Message: </strong> ${message}</p><br>`
        });
    } catch (error) {
        return res.status(500).json({error: error.message || error.toString() })
    }
    return res.status(200).json({ error: ""});
};

contact.js:

import { useState } from 'react'

export default function Contact() {
    const [inputs, setInputs] = useState({
        name: '',
        email: '',
        phone: '',
        message: ''
    })

    const [form, setForm] = useState('')

    const handleChange = (e) => {
        setInputs((prev) => ({
            ...prev,
            [e.target.id]: e.target.value
        }))
    }

    const onSubmitForm = async (e) => {
        e.preventDefault()

        if (inputs.name && inputs.email && inputs.phone && inputs.message) {
            setForm({ state: 'loading' })
            try {
                const res = await fetch(`api/contact`, {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify(inputs)
                })

                const { error } = await res.json()

                if (error) {
                    setForm({
                        state: 'error',
                        message: error
                    })
                    return
                }

                setForm({
                    state: 'success',
                    message: 'Your message was sent successfully.'
                })
                setInputs({
                    name: '',
                    email: '',
                    phone: '',
                    message: ''
                })
            } catch (error) {
                setForm({
                    state: 'error',
                    message: 'Something went wrong.'
                })
            }
        }
    }

None of my Google searches seem to bear any fruit. Does it have something to do with my domain provider? I have tested my code with Gmail, and it works like a charm, but not with one.com.

I am open for suggestions. This error has had me stumbled for days now.

Steve
  • 37
  • 5

1 Answers1

0

The reason you're trying to send an email from an unauthorized source is because your from option is using data from the request. You wont be able to send an email from a source you aren't authorized to use. You should be sending from contact@example.com.

I'm not sure of the exact goal of the form, but consider redesigning the flow of the email service to send emails from your own source (contact@example.com) otherwise, you have to take the users email authorization credentials as input which can and will go south quickly.