Search code examples
node.jsgmailnodemailer

Some emails from Nodemailer are not reaching the clients


I was sending register verification emails through Nodemailer using the code below, and it turns out that some of my clients are either not receiving anything or the email goes to spam. Some other clients can receive the email normally. I asked Google Support but they said it is not possible that the same kind of emails goes to some users' spam folder and some other users' inbox folder. That's why I am confused here.

BTW, Google confirmed with me that the DKIM and other verifications are good. And the emails that are sent have arrived at those clients' mailboxes. But without their approval, Google doesn't know if the email is not actually there or is sent to the spam folder.

function registerEmailSender(firstName, lastName, email, uuid) {
    console.log('registerEmailSender is triggered');
    console.log('email: ', email);

    let transporter = nodemailer.createTransport({
        service: 'gmail',
        auth: {
            user: 'info@mydomain.com', // generated ethereal user
            pass: 'mypassword' // generated ethereal password
        },
    });

    const html = `
    Hi ` + firstName + `,<br>
    <br>
    Please click the link to active your account: <a href="https://www.google.com/url?q=https://example.com/sessions/email-verification/` + uuid + `">https://example.com/sessions/email-verification/` + uuid + `</a><br>
    <br>
    Sincerely, <br>
    My Team<br>
    `
    const mailOptions = {
        from: 'My Email <info@mydomain.com>', // sender address
        to: email, // list of receivers
        subject: 'Happy to have you here', // Subject line
        html: html
    };
    
    return transporter.sendMail(mailOptions, function (err, info) {
            console.log('sendMail is triggered');
        // if(err) 
        //     console.log('Error occurs: ', err)
        // else
        //     console.log('Email sent: ', info);

        if(err) {
            console.log('Error occurs: ', err)
        }
        else {
            console.log('Email sent: ', info);
        }
    });
}

Solution

  • It turns out nodemailer is not very reliable due to it is not recognized as a 'trusted application' unless your server itself is trusted by Google. And that seems to be reducing your reputation and cause the email to be rejected.

    Best solution I can find for now is to use some paid mailing service like Mailchimp. Just don't use Nodemailer if you don't have to.