I am trying to send out emails when a user signs up to my site. I decided on Nodemailer and MailGun and have come to realize there is not many tutorials or good docs on them working together. Seems to be a similar issue for the rest of the services like MailGun. Below is my code for an attempt at getting it to work. I watched a tutorial on getting it to work but cant seem to figure it out. I think the code is right but maybe the auth is wrong? Or I could just be off on both. I am using cloud9.io for my dev environment so maybe that is the issue? Or that my domain is not verified yet?
Route:
app.post('/signup',
setRedirect({auth: '/dashboard', success: '/dashboard', failure: '/signup'}),
isUnauthenticated,
registrations.postSignup,
(req, res) =>{
const output = `
<h1>Your email is: ${req.body.email}</h1>
`;
// create reusable transporter object using the default SMTP transport
let transporter = nodemailer.createTransport({
host: 'postmaster@MYSUBDOMAIN',
port: 587,
secure: false, // true for 465, false for other ports
auth: {
user: 'postmaster@MYSUBDOMAIN', // generated ethereal user
pass: 'MYPASSWORD' // generated ethereal password
},
tls:{
rejectUnauthorized:false
}
});
// setup email data with unicode symbols
let mailOptions = {
from: '"Nodemailer Contact"<your@email.com>', // sender address
to: '`${req.body.email}`', // list of receivers
subject: 'Node Contact Request', // Subject line
text: 'Hello world?', // plain text body
html: output // html body
};
// send mail with defined transport object
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
return console.log(error);
}
console.log('Message sent: %s', info.messageId);
console.log('Preview URL: %s', nodemailer.getTestMessageUrl(info));
// render view here and pass in data/message
});
});
Then the domain information from mailgun.com:
State
Unverified
IP Address
IPASSIGNED Manage IPs
SMTP Hostname
smtp.mailgun.org
Default SMTP Login
postmaster@MYSUBDOMAIN
API Base URL
https://api.mailgun.net/v3/MYSUBDOMAIN
Default Password
MYPASSWORD SMTP credentials
API Key
MYKEY