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.

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.

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.


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.

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.

Discussions — 18 Responses

  • Andrew Leonard October 14, 2014 on 12:31 pm

    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

    Reply
  • Gustavo Bordoni October 14, 2014 on 1:46 pm

    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 = "your_email_here@yoursite.com";
    $subject = "Interest on " . $post->post_title . " by " . $email;
    $content =
        "Name: <b>{$name}<b>" .
        "Email: <b>{$email}<b>" .
        "Product: <b>{$post->post_title}<b>" .
        "Product link: <b>" . get_permalink( $post ) . "<b>";
    
    $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 -- http://core.trac.wordpress.org/ticket/23578
    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: http://yoursite.com/interest?_post_id={$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.

    Reply
  • acjexports October 15, 2014 on 6:55 am

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

    http://customer.io/docs/triggered-lifecycle-email-with-mailgun.html

    http://customer.io/docs/newsletters.html

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

    Reply
    • Gustavo Bordoni acjexports October 15, 2014 on 10:21 am

      So, my only concern with using Customer.io 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.

      Reply
  • acjexports October 15, 2014 on 7:02 am

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

    Reply
  • Roberto December 26, 2014 on 10:23 am

    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?

    Reply
    • Gustavo Bordoni Roberto December 27, 2014 on 1:02 pm

      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.

      Reply
  • Chris Allen April 29, 2015 on 6:22 pm

    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() ?

    Reply
  • Gustavo Bordoni April 29, 2015 on 8:00 pm

    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:

    ```php

    // Code

    ```

    Reply
  • vinaykumarnetha June 20, 2015 on 8:52 am

    I'm getting an error :

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

    Reply
    • Gustavo Bordoni vinaykumarnetha June 20, 2015 on 2:31 pm

      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. 😉

      Reply
  • Julian November 9, 2015 on 12:23 pm

    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!

    Julian

    Reply
  • Pragya March 30, 2016 on 4:27 am

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

    Reply
    • Gustavo Bordoni Pragya March 31, 2016 on 1:51 am

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

      Reply
  • Nada April 13, 2016 on 6:30 am

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

    Reply
    • Gustavo Bordoni Nada April 13, 2016 on 11:35 pm

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

      Reply
  • BabyBichu May 21, 2016 on 6:22 am

    Great gonna test it

    Reply
  • Kathy January 18, 2017 on 4:06 pm

    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' ) ; ob_start(); include(MY_PLUGIN_PATH . '/templates/ecard_email.php'); $message = ob_get_contents(); ob_end_clean();

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

    Reply

Leave a Reply