Error sending email using own SMTP server with Strapi

System Information
  • Strapi Version: 4.4.1
  • Operating System: windows
  • Database: pg
  • Node Version: 16.13.2
  • Yarn Version: 1.22.15

I’m using Strapi as the backend for my project and I have configured my own SMTP server as the email provider using the steps mentioned in the Strapi documentation. However, when I try to send an email after creating a new entry, I get the following error:

Error: connect ECONNREFUSED 145.145.145.145:587                                   
    at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1161:16)            
    at TCPConnectWrap.callbackTrampoline (node:internal/async_hooks:130:17) {    
  errno: -4078,                                                                  
  code: 'ESOCKET',                                                               
  syscall: 'connect',                                                            
  address: '145.145.145.145',                                                     
  port: 587,                                                                     
  command: 'CONN'                                                                
}

Here is my configuration from plugins.js :

email: {
    config: {
      provider: 'nodemailer',
      providerOptions: {
        host: env('SMTP_HOST'),
        port: env('SMTP_PORT'),
        auth: {
          user: env('SMTP_USERNAME'),
          pass: env('SMTP_PASSWORD'),
        },
        pool: true,
        logger: true,
        debug: true,
        maxConnections: 10000,
        secure: false,
        tls: {
          rejectUnauthorized: false,
        },
      },

      settings: {
        defaultFrom: env('SMTP_FROM'),
        defaultReplyTo: env('SMTP_REPLY_TO'),
      },
    },
  },

and there is my lifecycle hook that I use to send email:

module.exports = {
    async afterCreate(event) {  
        const { result } = event;

        try {
            await strapi
                .plugin('email')
                .service('email')
                .send({
                    to: result.send_to[0].email,
                    subject: result.subject,
                    text: result.content,
                })
        } catch (error) {
            console.log(error)
        }
    }
}

As you can see in the error, it can’t connect on port 587. it’s refused. Meaning also check your SMTP server setup that it’s allowing connections etc.

1 Like