WPForms Blog

WordPress Tutorials, Tips, and Resources to Help Grow Your Business

How to Send WordPress Emails With Mailgun Using WP Mail SMTP

by Shahzad Saeed on Apr 2, 2018

Have you noticed that you’re not receiving WordPress contact form notifications? You might be wondering why WPForms isn’t sending you emails when someone fills out your forms, or why you aren’t seeing any WordPress notifications.

In this tutorial, we’ll walk you through how to fix email delivery issues by sending WordPress emails with Mailgun using WP Mail SMTP.

Note: If you are using G Suite, we recommend using WP Mail SMTP’s Google mailer option in place of Mailgun to avoid technical challenges.

Why Email Notifications Don’t Work

Maybe you’ve noticed that WordPress isn’t sending you email notifications because form entries are piling up in your WordPress dashboard, or frustrated visitors start to reach out to you using social media or other methods.

The most common reason for missing these emails is due to your WordPress hosting server. Usually it isn’t configured to use the PHP mail() function, which WordPress uses by default to send emails generated by WordPress or any contact plugin like WPForms.

Thankfully, you can work around this issue by using SMTP.

SMTP (Simple Mail Transfer Protocol) is the industry standard for sending emails. Unlike PHP mail function, SMTP uses proper authentication which increases email deliverability. That’s why we recommend using SMTP to send emails in WordPress.

In this article we’ll show one of the SMTP services we recommend: Mailgun.

How to Fix Your Form Notifications

Mailgun is a popular email service provider that allows website owners to send large quantities of emails. You can send your first 10,000 emails for free every month (or 100 emails per hour), though beyond that it’s a paid service. But that’s more than enough for most small websites.

Let’s get started setting up your site to send WordPress emails with Mailgun using WP Mail SMTP.

1. Sign up for a Mailgun account

First you need to sign up for an account on the Mailgun website.

Sign up for a Mailgun account

Note: As an added verification step Mailgun will require that you enter credit card information to set up an account, even if using a free account. For more information, please see Mailgun’s sign up page.

After completing the short signup form, you’ll have a couple of additional verification steps to complete. Multiple levels of verification is what allows Mailgun to better ensure email deliverability for all of their users.

First, you should receive an email to verify your Mailgun account. After clicking the verification link, you’ll then need to enter a mobile number to receive a text verification code.

Verify Mailgun account

Once you’ve completed these steps, you’ll be taken to the login page. Go ahead and log into your new Mailgun account.

After logging in, you’ll be taken to your Mailgun dashboard. If you didn’t enter credit card details earlier, you’ll be prompted for that information again here. Otherwise, you’ll see a graph that can later show some statistics about the emails you send.

When you’re ready, click on Domains in the main menu.

Go to Domains in Mailgun dashboard

On the Domains page, you’ll need to click the Add New Domain button.

Add new domain to Mailgun

Next, you’ll need to enter a subdomain of your website’s domain name.

What does that mean, exactly? A subdomain replaces the “www” part of your website’s address; for example: “subdomain.yourwebsite.com.” Subdomains can be used to add separate sections or directories to your website (like how some sites have their blogs on a subdomain such as blog.example.com).

In this case, the subdomain we’ll create will be used by Mailgun to send emails.

We’ll create the actual subdomain in the next step. For now, you can just enter a subdomain such as mail.yourwebsite.com or mg.yourwebsite.com. (It doesn’t really matter what you name it, but using “mail” or “mg” makes it more clear what its purpose is.)

Add a domain to Mailgun

As we continue into the next step, be sure to keep this tab or window open as we’ll return to this page later.

2. Verify Your Domain

As a way of both protecting site owners and their visitors, some SMTP services like Mailgun require that you verify your domain by adding records to your site’s DNS. You can find more information on the benefits of verifying your domain in Mailgun’s guide.

First, you’ll need to open your site’s DNS settings. If you’re not sure how to find these, Mailgun will provide you with some helpful links shown on their setup page when you’re adding a new domain.

Mailgun Domains page

Note: If you’re not sure where to find your site’s DNS settings or feel unsure about making changes, please contact your site’s hosting provider.

Or for more details on modifying a site’s DNS, we recommend WPBeginner’s tutorial on DNS settings.

Once your DNS settings are open, you should see an option to add a new record.

TXT Records

To make it possible for Mailgun to verify your domain, you’ll need to add two TXT records to your site’s DNS. Be sure to copy them carefully, as you’ll need the exact values from your Mailgun account for the verification to work.

In your site’s DNS settings you may also see a TTL field, or Time To Live, field for the record. You can leave this blank to allow the default TTL value to be used.

MX Records

The next step is to add MX, or Mail Exchanger, records.

Note: If you are already using MX records to point to an email provider, such as G Suite, modifying MX records can lead to additional email issues.

If unsure of any changes, be sure to reach out to your site’s hosting provider or Mailgun support.

Mailgun’s MX records are the same for everyone:

First MX record:

  • Type: MX
  • Priority: 10
  • Value: “mxa.mailgun.org”

Second MX record:

  • Type: MX
  • Priority: 10
  • Value: “mxb.mailgun.org”

Once you’ve completed these steps, your DNS settings are all set.

3. Check your Mailgun Verification Status

Now Mailgun will need to verify that your domain has proper DNS records. This verification can take a while (sometimes 24-48 hours) because DNS records may take some time to update.

You can check the status of your domain verification any time by visiting the Domains page in your Mailgun account. Once Mailgun can verify the DNS changes you made, it will show active status next to your domain name.

View active domain on Mailgun

However, you can skip to the next step while you wait for this verification to complete. Just be sure to keep this tab or window open, as we’ll need it a little later.

4. Configure WordPress to Send Emails

Now that you’ve set up your DNS, you can prepare your WordPress site to start using Mailgun with WP Mail SMTP for sending emails.

The first thing you need to do is install and activate the WP Mail SMTP plugin. For more details, see our step by step guide on how to install a WordPress plugin.

After installing and activating this plugin, you need to visit Settings » WP Mail SMTP to configure plugin settings.

On the WP Mail SMTP Settings page, you’ll first need to enter the From Email and From Name. These will fill with your site’s admin email and site name by default, but we recommend setting the From Email to the same email address you used to create your Mailgun account for the most reliable delivery.

WP Mail SMTP settings

Next, in the Mailer field you’ll need to select the Mailgun option.

We also recommend checking the optional Return Path checkbox to Set the return-path to match the From Email. With this enabled, you’ll be emailed if any messages bounce as a result of issues with the recipient’s email.

WP Mail SMTP Mailer settings for Mailgun

After selecting Mailgun as your mailer, a new Mailgun section will appear. Here, you’ll need to enter API and domain information.

Even if your domain is still waiting to be verified, Mailgun will provide the information you need here. To access this, you’ll need to return to the tab or window with your Mailgun domain details.

If you look at the bottom the section named Domain Information, you should see the API Key.

Copy Mailgun API key

Once you’ve copied the full text for the key, return to the WP Mail SMTP settings and paste it into the API Key field.

Then for the Domain Name, enter the subdomain you created in Mailgun. If needed, you can find this at the top of the Mailgun page with your API key.

Domain name for Mailgun

Once this information is complete, go ahead and save your settings.

Enter Mailgun settings in WP Mail SMTP

5. Send a Test Email

Before proceeding with this step, make sure that the verification process from Step 3 is complete. This may take a while, so it’s possible you’ll need to come back to this later.

Once your domain is verified and your settings have been added to WP Mail SMTP, it’s important to send a test email to ensure that everything is working properly. To do this, make sure you’re on the Settings » WP Mail SMTP page and open the Email Test tab.

Send test email in WP Mail SMTP

On this tab, you can enter any email address that you have access to and click the Send Email button.

After sending the email, you should see a success message.

WP Mail SMTP test email was sent successfully

If you check the inbox for that email address, you should receive the email for this test.

Note: Be sure to also submit a couple of test submissions for your forms after completing this setup to check that emails deliver successfully.

If you see any issues, we recommend setting the From Email in your form’s notification settings to the same email you set up Mailgun with.

That’s it! You have successfully setup your site to send WordPress emails with Mailgun. Now your form notifications will reliably be delivered.

Looking for more email notification tips? You might also want to check out our guide on how to send users confirmation emails from WordPress forms. This is a great way to reassure visitors that their message has been received, and prevent them from sending duplicates.

Don’t have WPForms yet? What are you waiting for? Get started with the most powerful WordPress forms plugin today.

If you like this article, then please follow us on Facebook and Twitter for more free WordPress tutorials.

Comments

  1. Hi!

    I have a problem with my WP-Form, the checkboxers does’t work. The you press on a checkbox it does’t check, it stays empty.

    You can try it at [link removed]

    How can I fix it?

    1. Hi Linus,

      I see what you mean, and it looks like your theme is removing the normal checkbox styling. We can add this back, though, with a little custom CSS:

      .wpforms-container input[type=radio] {
      	-webkit-appearance: radio !important;
      }
      
      .wpforms-container input[type=checkbox] {
      	-webkit-appearance: checkbox !important;
      }
      
      .wpforms-container input[type=radio]:checked:before,
      .wpforms-container input[type=radio]:before,
      .wpforms-container input[type=checkbox]:checked:before,
      .wpforms-container input[type=checkbox]:before,
      .wpforms-container input[type=radio]:checked:after,
      .wpforms-container input[type=radio]:after,
      .wpforms-container input[type=checkbox]:checked:after,
      .wpforms-container input[type=checkbox]:after {
      	display: none !important;
      }
      

      The above CSS should fix any issues with Multiple Choice fields as well (these issues tend to happen at the same time). And in case it helps, here’s WPBeginner’s tutorial on how to add custom CSS to your site.

      I hope that helps! If you have any other questions, please get in touch 🙂

  2. i use mailgun
    and mailgun verify my domain already

    i test send mail and this error

    what shuld i do?
    ………………………………………………………………

    There was a problem while sending a test email.

    The related debugging output is shown below:

    Versions
    WordPress: 4.9.1
    PHP: 5.6.32
    WP Mail SMTP: 1.0.2
    PHPMailer
    ErrorInfo:
    Mailer: mailgun
    Host: localhost
    Port: 25
    SMTPSecure: NULL
    SMTPAutoTLS: bool(true)
    SMTPAuth: bool(false)

    1. Hi Korly,

      Sorry for the trouble! The best next step is to run back through the tutorial to double check that everything’s in place. With so many parts to Mailgun’s setup, this is the most common cause of errors that we see.

      I took a look at WP Mail SMTP’s support forum, and here’s a thread with this same issue and a couple suggestions to try, as well.

      If you still see this issue after giving both of those a try, please let us know! If you have a paid license with WPForms, you can contact our support here. Otherwise, please post to WP Mail SMTP’s support forum.

      Thanks! 🙂

  3. hi…
    i have error notification if test email

    Versions:
    WordPress: 4.9.1
    WordPress MS: No
    PHP: 5.4.45
    WP Mail SMTP: 1.2.2

    Params:
    Mailer: mailgun
    Constants: No
    Api Key / Domain: Yes

    Debug:
    Business Verification

    how to fix it?

    thanks

    1. Check your other plugins for conflicts. I had a conflict issue with an old Sendgrid plugin and some configurations in mail poet.

      1. Thanks, Elise! Having more than one SMTP plugin active on your site is very likely to cause issues/break SMTP functionality, so that’s a great suggestion to check.

  4. Thanks for the documentation. If I may suggest one part to add and highlight because it gave me issues:

    The API key includes “key-.” I know it’s dumb that I missed it, but I didn’t copy the “key-” bit, and that obviously caused an error. I double-clicked on the second part of the API key to select it, and that doesn’t grab the whole thing because of the dash.

    Thanks!

    1. Thanks for mentioning that, Stanford — we revised the text for that instruction slightly, hopefully that helps! 🙂

  5. Why would anyone want to use WP Mail SMTP in addition to Mailgun? The whole point of Mailgun is so that you don’t need WP Mail SMTP – or SMTP mail at all, for that matter!

    1. Hi Peter,

      Great question. To be used on a WordPress site, a Mailgun account must be connected via a plugin such as WP Mail SMTP. This is where the API details get added so that Mailgun can be applied to your site — without that, your Mailgun account can’t connect to your WordPress site and so would have no influence over emails sent there.

      I hope that helps! 🙂

  6. Hi

    I do not receive emails from my WPForms.
    I have added Mailgun, and WP Mail SMTP, and have received email from both, that they are ok.

    I have the same email-address in Mailgun, WPForm and in WP Mail SMTP.
    What am I doing wrong?

    Stina

      1. Hi 🙂

        I have only the WPForms lite, so I do not have the login information i need, to make a support request. What do I do then?

      2. No problem, Stina — please use the right-side option on that page (button labeled ‘Complete a Form’). Thanks! 🙂

    1. Hi Ricky,

      Once WP Mail SMTP is set up, it will apply to all emails sent out from your WordPress site. For form emails, just be sure that all of the emails match (so the email used to set up Mailgun, the From Email in the SMTP settings, and the From Email in your form email settings must all be the same).

      I hope that helps! 🙂

  7. I just set up WP Mail SMTP using my MailGun account credentials, and the emails are sent properly, but without line breaks. The code we use to generate the emails is using \n\n to add line breaks in the body of the email, but the emails are sent out as a single line, ignoring the line breaks. Is there any way to force the emails to be sent as plain text, instead of HTML?

    Thanks!

    1. Hi Carlos,

      The answer to your question depends on what plugin is sending those emails out. If you need to do this for WPForms notification emails, you can switch to plain text emails under WPForms > Settings > Email tab (more details on that in this tutorial). For any other plugins, please check with their documentation or support.

      I hope this helps! 🙂

      1. Hi Jess,

        I’m having the same issue as Carlos. It seems that the plugin is converting emails that should be plain text into HTML formatting automatically. This doesn’t only mean that line breaks are ignored, but also some links. For example, the default WordPress Forgot Password email gets sent out without line breaks and (because of how it’s formatted) without the link to set a new password.

        According to all my tests, this is happening with all plugins and default WordPress emails that send mail out in plain text.

        Thanks for all your help!

      2. Hi Luis,

        Thanks for the details! Our team has tracked down the source of this issue, and is working on a fix. They’ll include this in an update as soon as it’s ready.

        Sorry for the trouble!

  8. Test email error:

    Versions:
    WordPress: 4.9.4
    WordPress MS: No
    PHP: 5.6.30
    WP Mail SMTP: 1.2.5

    Params:
    Mailer: mailgun
    Constants: No
    Api Key / Domain: Yes

    Debug:
    Forbidden

    1. Hi Jami,

      If you’re seeing ‘Forbidden’ it’s likely that there’s a permissions issue occurring within your site’s configuration. When you get a chance, please get in touch with your site’s hosting provider so that they can help to address this (likely this issue stems from a configuration issue on the server).

      I hope this helps!

    2. Actually, you are using the wrong API key. Just fix the API key and it will work 🙂
      The Forbidden message is from MailGun, not from your host (as the SMTP plugin doesn’t need any permissions from the host machine).

  9. So I have a couple of websites for real estate agents. In the general settings box they want to use the email their broker has set up for them. However, the broker won’t allow us to connect to their server for smtp. So would using MailGun work for this use case? The website domain is mydomain.com but all the emails from the forms need to go to person@mybrokerdomain.com? I am little confused.

    1. Hi Mark,

      When setting up any SMTP option, the most important thing is what you’d like to use as the From Email. You’ll be able to use any email you’d like as the recipient(s), however the From Email must match the email you use to set up the SMTP.

      It may be worth noting that both Mailgun and SendGrid options that require more access than the other two mailer options since you’ll need to modify DNS records for the site, however once this is in place it won’t need to be modified again later.

      I hope this helps! 🙂

  10. I have installed the plugin and also send the test email which I successfully received.
    I use the MailGun method. But After That I do not receive the email from my WP website So I again try to send the test email but it do not sending the test email also. In mailgun my domain is also active. Also it do not show any error

    1. Hi Ali,

      Generally when we see this issue, where the test email successfully arrives but form emails do not, there is an issue with the From Email. The From Email of your forms (set in the WPForms form builder under Settings > Notifications) must match the email used to set up Mailgun.

      So the From Email in WPForms, the From Email in WP Mail SMTP, and the email used to set up Mailgun should all be the same.

      I hope this helps!

  11. So if I’m already using G-Suite for my day-to-day e-mail I don’t need to enter MX records for MailGun? What allows the connection to be made? Thank you.

    1. Hi John,

      I apologize for the confusion with that! For Mailgun, MX records can present some complex challenges when using an email service like G Suite. We’ve added a note to the top of this doc to hopefully help address this at the start.

      Since you use G Suite I would recommend considering WP Mail SMTP’s Google mailer option instead. This uses Google’s own API, does not require modifying DNS in any way, and overall requires less setup steps if you already have a Gmail or G Suite email.

      I hope this helps! 🙂

  12. I did the whole setup and unfortunately the test mail works only for the email account I have verified in mailgun. For other emails I am getting an error message that for free account it is not allowed to send mails.

    There was a problem while sending a test email. Related debugging output is shown below:

    Versions:
    WordPress: 4.9.4
    WordPress MS: No
    PHP: 5.6.30
    WP Mail SMTP: 1.2.5

    Params:
    Mailer: mailgun
    Constants: No
    Api Key / Domain: Yes

    Debug:
    Free accounts are for test purposes only. Please upgrade or add the address to authorized recipients in Account Settings.
    Please copy only the content of the error debug message above, identified with an orange left border, into the support forum topic if you experience any issues.

    1. Hi Ashok,

      This error generally means that you haven’t yet verified your domain with Mailgun. Right now Mailgun is trying to work through your sandbox domain (their default), which can send to authorized recipients only. If you’re curious, you can find these details by clicking on the sandbox domain in your Mailgun app (here’s a screenshot of what you should see).

      In order to fix this, be sure to go back through the steps of this tutorial — and please know that domain verification can take some time, as it can take a while for DNS changes to take effect.

      I hope this helps! If you’d like further support for Mailgun setup, we offer email support for WP Mail SMTP to users with an active WPForms license. Additionally, as a valued WP Mail SMTP user, we offer 20% off your initial purchase 🙂

      Thanks!

Add a Comment

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our privacy policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.