Mailtrap not sending email using NodeJS Nodemailer

265 views Asked by At

I have been trying to send email trough Mailtrap SMTP server but it does not send the email. I am using Nodemailer module in NodeJS application to send the email. The given SMTP information(Email Sending; not Email Testing) by Mailtrap is as below:

enter image description here

Following is the configuration set up as in the code:

const transporter = nodemailer.createTransport({
        host: 'live.smtp.mailtrap.io',
        port: '587',
        secure: false,
        requireTLS: true,
        auth: {                  
            user: 'api',
            pass: '********cc8d'
        }
    })
const mailOptions = {
        from: req.body.email, 
        to: '[email protected]', 
        subject: `Mssage from ${req.body.email}: ${req.body.subject}`,
        text: req.body.message
    }
transporter.sendMail(mailOptions, (error, info) => {
        if (error) {
            res.send('error');
        } else {
            res.send('success');
        }
    })

I could not find out the cause behind not sending the email. Ay solution or suggestion will be highly appreciated!

2

There are 2 answers

0
Adrian Flisc On

If you have a domain registered to Mailtrap, try to put an email from you domain in "from" field of mailOptions(even thought you don't have that email) because in some cases i faced a domain validation of the email sender.

0
NonsoBarn On

The port is not a string, loose the quotation marks, then you can also interchange to know which works for you, 587 and 25 did'nt work for me, but 2525 did.

const transporter = nodemailer.createTransport({
    host: 'live.smtp.mailtrap.io',
    port: 587,
    secure: false,
    requireTLS: true,
    auth: {                  
        user: 'api',
        pass: '********cc8d'
    }
})