Search code examples
node.jssmtpnodemailerhapi

Nodemailer without domain in Hapi returns needs domain error. It ran perfectly when the code is run outside of Hapi


Im currently writing a simple nodejs application. One of the features is to send email by cron job using hapi & nodemailer.

The app ran successfully with transporter such as

            const transporter = nodemailer.createTransport({
                host: mail.somesmtpserver.com,
                port: 587,
                auth: {
                    user: name@example.com,
                    pass: somepassword
                }
            })

I'm currently trying to make it work using smtp2go, I tried it first outside of the Hapi server with the user as 'some-username' without the domain. It worked perfectly and smtp2go logs the sent email.

I tried to put the same code inside of Hapi, it returns

(node:3211) UnhandledPromiseRejectionWarning: Error: Mail command failed: 501 <some-username>: sender address must contain a domain

Ive tried to put the sender code inside an async function then call them using await, it still did not worked

Any ideas on how should I write this code? Thanks in advance!


Solution

  • Apparently, on the sendEmail function, I added it as

    
                    let mailOptions = transporter.sendMail({
                        from: `application email <${process.env.MAIL_DOMAIN}>`,
                        to: `${process.env.EMAIL_RECEIVER}`,
                        subject: `Email Subject`,
                        html: htmlToSend
                    })
    

    This won't work if my MAIL_DOMAIN is set as the SMTP server's username (not an email). The test file was using a hardcoded email name!

    I resolved this by changing it to an email domain since it lets me send from 'nonexistant' email!