send email with Nodemailer

Asked

Viewed 895 times

0

I created the following Model with Nodejs:

'use strict'

function UserDAO(model) {
    this.model = model;
}

UserDAO.prototype.email = function(callback) {

//TESTE de email
var filePath = 'services/content/test.html';
var fs = require('fs');
var config = require('config');
var mail = require('../services/email.service');

//TESTE de email
fs.readFile(filePath, function (err, content)
{
    if (err) {
        console.log('Failed to read email file template html, err = ' + err);
        callback('Failed to read email file template html, err = ' + err);
    } 
    else {
        var url = config.get('smtpConfig.redirectUrl') + '/#/novo-usuario?token=' + '123456'
        var rendered = content.toString().replace('#link#', 'teste.com.br');
        mail.send('[email protected]',
            'Bem-vindo',
            'Acesse o link: ',
            rendered, function (err, info)
            {
                if (err) {
                    console.log(err);
                    callback(err)
                }
                else {
                    console.log('E-Mail enviado: ' + info);

                    var retorno = { message: 'Usuário criado com sucesso!' };
                    callback(null, retorno);
                }
        });
     }   
});
//FIM TESTE email
}

module.exports = function(mongoose) {
    var User = mongoose.model('User', {
        name: {type: String, required: true},
        username: {type: String, required: true},
        password: {type: String, required: true}, 
        email: {type: String, required: true},
        ra: {type: Number, required: false},
        tipo: {type: String, required: true}
    });
    return new UserDAO(User);
}

The email.service is as follows:

var nodemailer = require("nodemailer"); //para enviar emails
var smtpTransport = require('nodemailer-smtp-transport');
var config = require('config');

function enviarEmail (to, subject, text, html, callback) {

//console.log("entrou no email.service.js");

var transporter = nodemailer.createTransport(smtpTransport({
    host: config.get('smtpConfig.host'),
    port: config.get('smtpConfig.port'),
    secure: config.get('smtpConfig.secure'),
    auth: {
        user: config.get('smtpConfig.auth.user'),
        pass: config.get('smtpConfig.auth.pass')
    }
}));

// setup e-mail data with unicode symbols
var mailOptions = {
    from: config.get('smtpConfig.from'),
    to: to, 
    subject: subject, 
    text: text, 
    html: html
};

//verify connection configuration
transporter.verify(function(error, success) {
    if (error) {
        console.log(error);
        transporter.close();
    } else {
        console.log('Server is ready to take our messages');
        //send mail with defined transport object
        transporter.sendMail(mailOptions, function (error, info)
        {
            if (error) {
                return callback(error);
            }
            callback(null, info);
        });
    }
});
}

exports.send = function (to, subject, text, html, callback) {
  return enviarEmail(to, subject, text, html, callback);  
} 

But I get the following mistake:

Server is ready to take our messages { [Error: Mail command failed: 553 From address not verified ] code: 'EENVELOPE', Sponse: '553 From address not verified - responseCode: 553 } { [Error: Mail command failed: 553 From address not verified] code: 'EENVELOPE', Sponse: '553 From address not verified, responseCode: 553 }

1 answer

0

Eduardo,

Test this way on a separate JS and ve resolve.

var nodemailer = require('nodemailer');

var conta = nodemailer.createTransport({
  host: 'smtp.seudominio.com.br',
  port: '587',
  auth: {
    user: '[email protected]',
    pass: 'suaSenha'
  },
  secure: false,
  tls: {
    rejectUnauthorized: false
  }
});

var enviarSenha = function(nome, email, callback){
  var config = {
    from: 'Nome remetente <[email protected]>', // Quem está mandando
    to: nome + ' <' + email + '>', // Para quem o e-mail deve chegar
    subject: 'Recuperação de Senha', // O assunto
    html: '<strong>Olá ' + nome + '!</strong>' // O HTMl do nosso e-mail
  };
  conta.sendMail(config, function(err, info){
    callback(err, info);
  });
};

User and pass is username and password of the sender’s email so there is no error in sending, because some providers do not let send without this authentication.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.