Email Delivery. Simplified.
SendGrid's cloud-based email infrastructure relieves businesses of the cost and complexity
of maintaining custom email systems. SendGrid provides reliable delivery, scalability and real-time
analytics along with flexible APIs that make custom integration a breeze.
Before to use this plugin, you'll need to create your very own SendGrid account.
Go ahead and do so at http://sendgrid.com/partner/wordpress
To have the SendGrid plugin running after you activated it, please go to plugin's
settings page and set the SendGrid credentials, and the way your email will be sent through SMTP or API.
You can also set default values for the 'Name', 'Sending Address' and the 'Reply Address'
in this page, so that you don\'t need to set these headers every time you want to send an email from your
application.
After you have done these configurations, all your emails sent from your WordPress installation will go through SendGrid.
Now let see how simple is to send a text email:
<?php wp_mail('to@address.com\', 'Email Subject', 'Email Body'); ?>
Where:
- $to - Array or comma-separated list of email addresses to send message.
- $subject - Email subject
- $message - Message contents>
- $headers - Array or SendGrid\Email() object. Optional.
- $attachments - Array or "\n"/"," separated list of files to attach. Optional.
The wp_mail function is sending text emails as default. If you want to send an email with HTML content you have
to set the content type to 'text/html' running
add_filter('wp_mail_content_type', 'set_html_content_type');
function before to wp_mail() one.
After wp_mail function you need to run the
remove_filter('wp_mail_content_type', 'set_html_content_type');
to remove the 'text/html' filter to avoid conflicts -- http://core.trac.wordpress.org/ticket/23578
Example about how to send an HTML email using different headers:
Using array for $headers:
$subject = 'test plugin'
$message = 'testing wordpress plugin'
$to = array('address1@sendgrid.com', 'Address2
', 'address3@sendgrid.com');
$headers = array()
$headers[] = 'From: Me Myself ';
$headers[] = 'Cc: address4@sendgrid.com';
$headers[] = 'Bcc: address5@sendgrid.com';
$headers[] = 'unique-args:customer=mycustomer;location=mylocation';
$headers[] = 'categories: category1, category2';
$headers[] = 'template: templateID';
$attachments = array('/tmp/img1.jpg', '/tmp/img2.jpg');
add_filter('wp_mail_content_type', 'set_html_content_type');
$mail = wp_mail($to, $subject, $message, $headers, $attachments);
remove_filter('wp_mail_content_type', 'set_html_content_type');
Using SendGrid\Email() for $headers:
$subject = 'Test SendGrid plugin';
$message = 'testing WordPress plugin';
$to = array('address1@sendgrid.com', 'Address2
', 'address3@sendgrid.com');
$headers = new SendGrid\Email();
$headers
->setFromName("Me Myself")
->setFrom("me@example.net")
->setCc("address4@sendgrid.com")
->setBcc("address5@sendgrid.com")
->setUniqueArgs(array('customer' => 'mycustomer', 'location' => 'mylocation'))
->addCategory('category1')
->addCategory('category2')
->setTemplateId('templateID');
$attachments = array('/tmp/img1.jpg', '/tmp/img2.jpg');
add_filter('wp_mail_content_type', 'set_html_content_type');
$mail = wp_mail($to, $subject, $message, $headers, $attachments);
remove_filter('wp_mail_content_type', 'set_html_content_type');
How to use Substitution and Sections
$subject = 'Hey %name%, you work at %place%';
$message = 'testing WordPress plugin';
$to = array('address1@sendgrid.com');
$headers = new SendGrid\Email();
$headers
->addSmtpapiTo("john@somewhere.com")
->addSmtpapiTo("harry@somewhere.com")
->addSmtpapiTo("Bob@somewhere.com")
->addSubstitution("%name%", array("John", "Harry", "Bob"))
->addSubstitution("%place%", array("%office%", "%office%", "%home%"))
->addSection("%office%", "an office")
->addSection("%home%", "your house");
$mail = wp_mail($to, $subject, $message, $headers);`
More examples for using SendGrid SMTPAPI header: https://github.com/sendgrid/smtpapi-php/
Categories used for emails can be set:
- globally, for all emails sent, by setting the 'Categories' field in the 'Mail settings' section
- per email by adding the category in the headers array: $headers[] = 'categories: category1, category2';
If you would like to configure categories for statistics, you can configure it by setting the 'Categories' field in the 'Statistics settings' section
Define SendGrid settings as global variables (wp-config.php):
- Set the API Key:
- API Key: define('SENDGRID_API_KEY', 'sendgrid_api_key');
- Set email related settings:
- Send method ('api' or 'smtp'): define('SENDGRID_SEND_METHOD', 'api');
- From name: define('SENDGRID_FROM_NAME', 'Example Name');
- From email: define('SENDGRID_FROM_EMAIL', 'from_email@example.com');
- Reply to email: define('SENDGRID_REPLY_TO', 'reply_to@example.com');
- Categories: define('SENDGRID_CATEGORIES', 'category_1,category_2');