DEV Community

Submit a form and receive email Using Express, Nodejs, Nodemailer and MailGun - Complete Guide

Ume Abraham Kalu on May 23, 2020

Hello, guys today, I'll be showing you how to create a Contact Form that allows users to submit and you receive a mail directly to Email Account ...
Collapse
 
fransco35 profile image
Fransco35

I tried this but I keep getting the internal error message. I think this is as a result of mailgun's rules whereby unless my credit card details are provided I won't be able to access their services and frankly Nigeria don't do credit cards so I can't access it.

Collapse
 
umekalu profile image
Ume Abraham Kalu

Your debit card should work. Perhaps, you won't be charged unless you use up your free trial. Make sure you have the right mailgun domain and apiKey.

Oh, I stay in Nigeria too.

Collapse
 
akwetey profile image
Jonathan Akwetey

Any reason why mailgun is been used? Is there any difference using the mailgun with nodemailer and sending emails without mailgun with just nodemailer alone? Thanks.

Collapse
 
umekalu profile image
Ume Abraham Kalu

The big deal here is SMTP protocol and Mailgun API.

Nodemailer provides a standard interface for sending both text and HTML-based emails.

Based on the mailgun-js module and the nodemailer module, the Mailgun Transport was born. This is a transport layer, meaning it will allow you to send emails using nodemailer, using the Mailgun API instead of the SMTP protocol!

Nodemailer allows you to write code once and then swap out the transport so you can use different accounts on different providers. On top of that, it's a super solid way of sending emails quickly on your node app(s).

The Mailgun transport for nodemailer is great to use when SMTP is blocked on your server or you just prefer the reliability of the web api!

Collapse
 
riverbr profile image
riverbr

afaik, you need to export after the bracket

const sendMail = (name, email, subject, text, cb) => {
const mailOptions = {
sender: name,
from: email,
to: 'recipient@email.com',
subject: subject,
text: text
};

transporter.sendMail(mailOptions, function(err, data) {
    if (err) {
        cb(err, null);
    } else {
        cb(null, data);
    }
});
Enter fullscreen mode Exit fullscreen mode

}
// Exporting the sendmail
module.exports = sendMail;

Collapse
 
umekalu profile image
Ume Abraham Kalu

You're right but I already have those included inside the mailOptions.

Collapse
 
otumianempire profile image
Michael Otu

In the full code for server.js, you did res.status({ message: 'Email sent!!!' }); in the else block of the sendMail function

Collapse
 
umekalu profile image
Ume Abraham Kalu

Yes, that's when no error occurred.

Collapse
 
ayush071998 profile image
ayush jain

not getting mail

Collapse
 
umekalu profile image
Ume Abraham Kalu • Edited

Check if your mailGun domain and private key are correct. You also might be missing something. I don't know if you've fixed it by now.

Collapse
 
abhinavisrockstar profile image
Abhinav Tiwari

I get one error that is RangeError: Maximum call stack size exceeded error

Collapse
 
umekalu profile image
Ume Abraham Kalu

check your callback functions, something is wrong there. Just the instruction you'll get it working.