forwardemail / email-templates

Create, preview (browser/iOS Simulator), and send custom email templates for Node.js. Made for @forwardemail, @ladjs, @cabinjs, @spamscanner, and @breejs.
https://forwardemail.net/docs/send-emails-with-node-js-javascript
MIT License
3.67k stars 337 forks source link

Error: connect ECONNREFUSED 127.0.0.1:587 #359

Closed MANTENN closed 5 years ago

MANTENN commented 5 years ago
{ Error: connect ECONNREFUSED 127.0.0.1:587
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1083:14)
  errno: 'ECONNREFUSED',
  code: 'ECONNECTION',
  syscall: 'connect',
  address: '127.0.0.1',
  port: 587,
  command: 'CONN' }

My setup

/* eslint-disable no-console */
import nodemailer from "nodemailer";
import mg from "nodemailer-mailgun-transport";
import { MAILGUN_API_KEY, MAILGUN_DOMAIN } from "@env";
import Email from "email-templates";

const auth = {
  auth: {
    api_key: MAILGUN_API_KEY,
    domain: MAILGUN_DOMAIN
  }
};

let transporter = nodemailer.createTransport(mg(auth));

// works without a problem
transporter.sendMail(
  {
    from: "noreply@example.org",
    to: "xxxxx@xxxxx.xxx",
    subject: "Test",
    text: "Test"
  },
  function(error, response) {
    if (error) {
      console.log(error);
    } else {
      console.log("Email was successfully sent.");
    }
  }
);

const email = new Email({
  send: true,
  views: {
    root: __dirname + "/templates"
  },
  message: { from: "noreply@xxxx.xxx" },
  transporter
});

// test invocation
email
  .send({
    template: "mars",
    message: {
      to: "xxxxxx@xxxxx.xxx"
    },
    locals: {
      name: "John Smith"
    }
  })
  .then(e => {
    console.log(e);
    return true;
  })
  .catch(e => {
    console.log(e);
    return false;
  });

export default email;

Package.json

{
  "nodemailer": "^6.1.0",
  "nodemailer-mailgun-transport": "^1.4.0",
  "email-templates": "^5.0.4",
}
MANTENN commented 5 years ago

Typo transporter is supposed to set transport

MrAkash33 commented 2 years ago

Use this code this will work finely

var transporter = nodemailer.createTransport({ host: 'smtp.gmail.com',
port: 587, secure:false, auth: { user: '*', pass: process.env.PASSWORD } }); var mailOptions = { from: '**', to: **, subject: 'Thankyou for contacting!', text: Hii ${req.body.username} thanks for contacting. I will touch you soon! }; var mailOptionsSender = { from: '*', to: '****', subject: 'Someone Wants to connect With you !', text: Hello ${req.body.username} wants to connect with you please contact him to further query! }; transporter.sendMail(mailOptions, function(error, info){ if (error) { console.log(error); } else { console.log('Email sent: ' + info.response); transporter.sendMail(mailOptionsSender, function(error, info){ if (error) { console.log(error); } else { console.log('Email sent: ' + info.response); } }); } });

dtechoracle commented 1 year ago

This worked for me using gmail smtp

const nodemailer = require('nodemailer');

const msg = { from: "example@gmail.com", to: "receiver@gmail.com", subject: "Nodemailer", text: "Testing out" };

nodemailer.createTransport({ service: 'gmail', auth: { user: "example@gmail.com", pass: "**" //This should be a password generated by google App password }, port: 465, host: 'smtp.gmail.com' })

.sendMail(msg , (err)=>{ if (err) { return console.log('Error occurs', err); }else{ return console.log('Email Sent'); } })