0

I'm trying to send a welcome email for my app. I'm using the following form with a post method.

<form action="/signup" method="post">

                    <h1> Registro </h1>

                    <div class="usuario">
                        <label> Usuario </label> <br />
                        <input type="text" id="userSign" name="email" onFocus="entrarFoco(this)" onBlur="salirFoco(this); revisarObligatorio(this)">
                    </div> <!-- Cierre del usuario-->

                    <div class="password">
                        <label> Contraseña </label> <br />
                        <input type="password" name="password" onFocus="entrarFoco(this)" onBlur="salirFoco(this); revisarObligatorio(this)">
                    </div> <!-- Cierre del password-->

                    <button type="submit"> Registrarse </button>

                </form> 

Using express and passport I make the sign-up:

app.post('/signup', 

    passport.authenticate('local-signup', {

        successRedirect : '/profile', //si los datos son correctos entraremos al perfil
        failureRedirect : '/', //si hay un error o los datos no son correctos redirecciona a la página principal
    })

);

I want to send an email once the user is succesfully registered. I'm using this code for that:

// email info
    var api_key = '********'; //my apikey
    var domain = '*********'; //my domain
    var mailgun = require('mailgun-js')({apiKey: api_key, domain: domain});
    var email = req.body.email; 

    console.log('email '+email);

    // POST /sendmail
    // curl --data "to=<email>, ..." http://localhost:3000/sendmail
    var data = {
    from: 'foodjoysocial@gmail.com',
    to: email,
    subject: 'Nuevo registro',
    text: 'Testing some Mailgun awesomness!'
    };

    mailgun.messages().send(data, function (error, body) {
    if (error) {
    res.json(error);
    } else {
    console.log(body);
    res.json(body);
    }
    });

I'm unable to get the email from the input texfield of my form using req.body.email. I've tried using an email instead of "req.body.name" and it works.

How can I get the user's email?

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
sheilapbi
  • 325
  • 1
  • 15

1 Answers1

0

As you said you are trying to send email after successful registration, Thus you can get user information from "req.user". Try this "req.user.email". Also for accessing req you can use this email sending utility as middleware.

// email info
var api_key = '********'; //my apikey
var domain = '*********'; //my domain
var mailgun = require('mailgun-js')({apiKey: api_key, domain: domain});
var email = req.user.email; 

console.log('email '+email);

// POST /sendmail
// curl --data "to=<email>, ..." http://localhost:3000/sendmail
var data = {
from: 'foodjoysocial@gmail.com',
to: email,
subject: 'Nuevo registro',
text: 'Testing some Mailgun awesomness!'
};

mailgun.messages().send(data, function (error, body) {
if (error) {
res.json(error);
} else {
console.log(body);
res.json(body);
}
});
Farhan Tahir
  • 2,096
  • 1
  • 14
  • 27