sending email with nodemailer

Asked

Viewed 834 times

0

I am making an application with Angularjs and have a contact form with name, email, phone and message.

I need the contents of this form to go to the client’s email and I’m trying to use the nodemailer but I’m having doubts.

I created a structure as follows:

root: |index.html |js: |script.js

Inside the js scrirpt I put the code below:

var nodemailer = require('nodemailer');

var transportador = nodemailer.createTransport({
      service: 'gmail',
      auth: {
        user: '[email protected]',
        pass: 'futebol11'
        }
    });

exports.send = function(){

    var configuracoes = {
        from: 'Seu Nome <[email protected]>',
        to: 'Nome do Destinatário <[email protected]>, Outra Pessoa <[email protected]>',
        subject: 'Assunto do Email',
        text: 'Conteúdo do email em texto',
        html: '<h1>Conteúdo do email em HTML</h1>'
    };

    transportador.sendMail(configuracoes, function(error, info) {
        if (error) {
            console.log(error);
        } else {
            console.log('Email enviado ' + info.response);
        }
    });
}

I don’t know how to call the upload file from Submit or an event on the button.

Can someone help me?

3 answers

0

I use the nodemailer with the React, the structure remains the same. In Act I do the following:

In the form I call a sending function that I call handleSubmit(), within this function is set the data that will be sent via e-mail. I use the Adonis to transpose this data by express.

Basically, to effect the sending of the email, you need the function of the nodemailer that is the transport.sendMail(jsonDaMensagem, function (capturaDeErro, informacaoErro)

Basically, you need to be able to send the data to your file and store it in some variable. In this case, if you receive any other request, it is necessary to receive them and use them in the body of the email, within the json message.

In express you can receive a route as:

app.post('/enviarEmail', (req, res) => {
  const {
   userEmail,
   outraPessoa
  } = request.body;

  transportador.sendMail (configuracoes, (erro, info) {
   ...
  }

});

In the nodemailer settings, you will be able to access these variables if it is configured in the same script, when called the above route it is important that the carrier.sendmail, this because this route who is receiving the data and already makes the function of effecting the sending of email.

var configuracoes = {
    from: 'Seu Nome <[email protected]>',
    to: 'Nome do Destinatário <${userEmail}>, Outra Pessoa <${outraPessoa}>',
    subject: 'Assunto do Email',
    text: 'Conteúdo do email em texto',
    html: '<h1>Conteúdo do email em HTML</h1>'
};

When the function is called, the.sendmail carrier will forward your email with the new settings that were received by requesting your form. I don’t know if the angle follows this model, but in React I use this way using Adonis to request the express.

0

I use the library nodemailer-smtp-transport along with the lib nodemailer. The submission script I use in my projects is this one:

const nodemailer = require("nodemailer"),
     smtpTransport = require('nodemailer-smtp-transport');


const smtpConfig = smtpTransport({
    host: "smtp.seuhost.com",
    port: 587,
    ignoreTLS: true,
    secure : true,
    tls: {
        rejectUnauthorized: true
    },
    auth: {
        user: "Seu login",
        pass: "sua senha"
    }
});

const transporter = nodemailer.createTransport(smtpConfig);

const message  = {
    from: "Seu Nome ",
    to: "Nome do Destinatário ",
    subject: "Assunto do Email",
    text: "Conteúdo do email em texto",
    html: "<h1>Conteúdo do email em HTML</h1>",
    headers: {
        'X-Laziness-level': 1000
    }
};


transporter.sendMail(message, function(error, info) {               
    if (error) {
        console.log(error);
    } else {
        console.log('Email enviado ' + info.response);
    }
});

0

I believe you will have to insert the script inside a Page Controller index.html. Add email function to controller scope.

Example:

$scope.sendMail = function {    

    transportador.sendMail(configuracoes, function(error, info) {
        if (error) {
            console.log(error);
        } else {
            console.log('Email enviado ' + info.response);
        }
    });
 }

in index.html insert into the button: ng-click=sendMail()

Browser other questions tagged

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