How to send Email in WordPress

Working with WordPress and development around plugins like MailPoet makes me wonder how much people actually know about how badly configured shared hosting are for your emails.

Don’t get me wrong, I’m not saying that they don’t know how to do it, but the way they need to do things to avoid spammers make they email server a crap one.

In the mailing world there are 2 main kind of emails, the Newsletter/Campaign email and the Transactional email.

### Campaign Emails

So to explain a little bit further on this subject we need to understand what categorizes the Newsletter email, it’s pretty much the one that is active, or where the sender actually fire an action of sending to a list, which also helps with the marketing or the site, while also using social media tools online, for example the agorapulse and hootsuite review where you can find great tools for this.

### Transactional Emails

This sort of emails are a little bit different because they are set to be sent to the user when an event happens or if a condition is matched by the system.

We should take in consideration that Transactional emails shouldn’t have a newsletter look and feel, or it could be considered a Newsletter.

### Email Services

So on my search through the web I’ve been able to find many email providers, some of them are better than the others, but in the end their goal is to delivery email faster.

In my search, I’ve been filtering out the ones that doesn’t provide an HTTP API, because since I’m talking about WordPress shared hosting I want to avoid any SMTP connection.

I didn’t want to include any services that didn’t had a pricing table or a public documentation, so that rules out many of the current players of this area.

My last rule was that I needed something that was easy to configure for a mortal WordPress user and could be used from of the gecko.

* [SendGrid](
* [MailGun](
* [Mandrill](
* [MailJet](
* [PostMark](

All of these have a starter plan, where you can send quite a good amount of emails for free, and this will help you deal with the beginning of your website, and you can test for yourself which one suites you best, while you can also improve your social media reach if you buy instagram followers for this purpose.

### About WordPress email

Sending a an Email with WordPress is an easy task, you will only need the code below for it to work:

It’s not because it’s simple that it’s not good, you can make more of it; Sending to multiple recipients using a single function call:

Changing the headers on the email, in the example we change the Reply-To email, which is by default the From value:

We can even change the Content Type of the email, then allowing us to use HTML on the email:

When we are using the wp_mail function we allow plugins to hook in to our mailing procedure and therefore allowing them to use other methods to send the email.

### Send emails via HTTP API in WordPress

First you need to register to one of the Email providers that I’ve talked above.

To ensure that your users will receive the emails from your WordPress site, you should take care of the DNS rules that they will ask you before start using their services.

The plugins below are the ones I’ve tested and they seemed to be working for a simple email sending flow, if you need a better control over your sending rate you might need to code something.

The only one I didn’t test is the Postmark, because it’s too old, but if you are already using their service you can test and give me the feedback in the comments below.

* [Postmark Approved WordPress Plugin](

### Wrapping Up

I’m using on this website the MailGun plugin because I really like their documentation, but I’ve been using this methods for some of my clients and It’s working very very good, because even if they pass the free mark it’s too cheap to worry, at least if you want to do a professional job.

19 Replies to “How to send Email in WordPress”

  1. I want to use your php code but I am not sure where to put it or how to trigger it
    Ideally, I would like to have a button using this sort of code:

    however onClick would send an email to me to tell me the user is interested or wants more information about something


  2. So Andrew what you want to do on this case is setup a page to do this the email.

    Create a [WordPress page template]( with a code that will parse some values, like below;

    $post = get_post( absint( $_GET[‘_post_id’ ) );
    $email = esc_attr( $_GET[‘_email’] );
    $name = esc_attr( $_GET[‘_name’] );

    $to = “”;
    $subject = “Interest on ” . $post->post_title . ” by ” . $email;
    $content =
    “Name: {$name}” .
    {$email}” .
    {$post->post_title}” .
    “Product link:
    ” . get_permalink( $post ) . ““;

    $headers = array(
    ‘Reply-To’ => $name . ‘<' . $email . '>‘,

    add_filter( ‘wp_mail_content_type’, ‘set_html_content_type’ );

    $status = wp_mail( $to, $subject, $content );

    // Reset content-type to avoid conflicts —
    remove_filter( ‘wp_mail_content_type’, ‘set_html_content_type’ );

    function set_html_content_type() {
    return ‘text/html’;

    // If status correct then redirect the user to the product page again
    if ( $status ){
    wp_redirect( get_permalink( $post ) );
    } else {
    // if the status of the email is false do something


    With that code in place you could send the user via a link to the page with the need params:

    Exemple: `{$ID}&_email={$user_email}&_name={$user_name}`

    Don’t forget to URL encode the user name and user email. Hope this will help you reach your goal.


  3. goodpost.. but just found this.. if you do not know how to add that code in wordpress you can do this..

    Here u are using your mail gun settings in and sending your mail through


    1. So, my only concern with using is that that will add up a price that in most cases people don’t want to have.

      And by suggesting another service you will not remove the need to know where to put the code to send the email.


  4. I forgot to add… u can add these services SendGrid, MailGun, Mandrill, in and send mail through it.


  5. Gustavo, I opened an account with sendgrid, installed their wordpress plugin and did all of the settings correctly. Sent an email via SMTP and got an error message. Then changed the email settingt to API and it said that the email was sent out. Also the SendGrid dashboard said that the email was sent out but the email was never received. Did check in spam folder and nothing. DOes using sendgrid bypass the issues of the shared server being blacklisted? I use Hostgator and the first test I did with email I got the email in spam folder. Subsequent tests that I did email was never received and it was after that that I started researching this issue and found this email. I thought this would solve my problems. Any ideas?


    1. Hi Roberto,
      In most cases it shouldn’t have a problem delivering a simple email, when you are using SendGrid you will not have problems with a server been blacklisted, although you could have your domain blacklisted.

      Try to check if your SPF and DKIM are configured on your SendGrid account, [this article about white labeling your domain]( should point you in the right direction:

      If you are still in trouble after trying that, do a simple test of using MailGun just to see if there is something on SendGrid (their API) that is not right on your case.


  6. Hi, this is working well for me. I’m using the mailgun wordpress plugin and the wp_mail() call. However, I cant seem to set the ‘From’ and ‘Reply-to’ headers manually.

    If I pass the headers array as the 4th argument, as in your example, it still sends from ‘WordPress’ and no reply-to is set in my message source either.

    Do you know if the WP mailgun plugin overrides the headers passed into wp_mail() ?


  7. Hi Chris,

    From what I understand the Mailgun should allow you to change the `reply-to` depending on how you configured their system (on their website).

    Can you post a code sample?

    Use the following template:


    // Code



  8. I’m getting an error :

    Call to undefined function wp_mail()..
    could u please tell me how to fix it


    1. Hi Vinay,

      Where are you getting this `wp_mail` undefined error? I mean `wp_mail` should always be defined within WordPress. Give me more information to help you here on this one. ?


  9. Hey,

    i am trying to send an email if a event in the “the-events-calendar-plugin” is about to happen in around one week.

    Is this possible without writing hundred lines of code?

    Many thanks!



  10. I want to implement on my site but it is bit difficult. Any help somebody?


    1. Hi Pragya, explain a little bit further how you want to apply this to your site?


  11. Hi
    I want to send an email with small condition like
    If n > 300
    Send email
    Any help ?


    1. Hi Nada, can you explain your use case a bit more? I can try to craft you an example that would work ?


  12. Hi, – I was hoping that you could help me – having a heck of a time changing the recipient email from an array . Try number 1001 is this, try number 1000 did not have the esc_attr on $recipient. I have tried it like green eggs and ham, in the class, through woo and through wp_mail directly and nothing seems to work on the $recipient – was able to get it to work in a class but only to send it to the predefined woo emails (customer and admin)

    function ecard_email_send(){
    $sitename = get_bloginfo(‘name’);
    $siteurl = site_url();
    $cardurl = ($siteurl . ‘/’ . $post_title);
    $recipient = esc_attr($lineItem[‘recipient_email’]);
    $headers = array(‘Content-Type: text/html; charset=UTF-8’);
    $subject = ($lineItem[‘sender_name’] . ‘ has sent you an Ecard from ‘ . $sitename);
    $sitename = get_bloginfo(‘name’);
    $siteurl = site_url();
    $cardurl = ($siteurl . ‘/’ . $post_title);

    $content = array(
    ‘name’ => $lineItem[‘recipient_name’],
    ‘verse’ => $lineItem[‘ecard_message’],
    ‘greeting’ => $lineItem[‘ecard_greeting’],
    ‘closing’ => $lineItem[‘ecard_closing’],
    ‘sender’ => $lineItem[‘sender_name’],
    ‘image’ => $ecardbackground[0],
    ‘link’ => $cardurl,
    ‘value’ => ‘View your card online’

    include(MY_PLUGIN_PATH . ‘/templates/ecard_email.php’);
    $message = ob_get_contents();

    $status = wp_mail( $recipient, $subject, $message, $headers );


  13. Dear,

    I am using php custom page in uploads folder in WordPress. Can you give full code that i can embed in php page to send an email.


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.