Best practice for sending email in WordPress

Best practice for sending email in WordPress

How to send emails from WordPress, ensuring proper formatting with both HTML and plaintext emails

Email comms is crucial when running a website, whether for sending notifications, password resets, or marketing messages. WordPress, being a versatile CMS, offers multiple ways to send emails. However, sending emails effectively requires ensuring deliverability, proper formatting, and handling both HTML and plaintext versions. This article jumps into the best practices for sending emails from WordPress, covering plugins, coding techniques, and essential tips for ensuring your emails reach their intended recipients.

Before we jump in it might be a good idea to take a read of our article How to send email from WordPress, which covers the actual sending of mail from code.

Why Email Deliverability Matters

Deliverability ensures that your emails reach the recipient's inbox rather than their spam folder. Factors influencing deliverability include:

  • Setting up SPF, DKIM, and DMARC records.

  • Avoiding spammy language and ensuring a good text-to-image ratio.

  • Using reputable SMTP servers and maintaining a clean email list.

Setting Up Your WordPress Site for Sending Emails

By default, WordPress uses the PHP mail() function to send emails. While functional, this method often faces deliverability issues due to misconfigured servers or lacking proper authentication. Therefore, using an SMTP server is recommended for reliable email delivery.

Using Plugins to Send Emails

WP Mail SMTP

One of the most popular plugins for configuring WordPress to send emails is WP Mail SMTP. It reconfigures the wp_mail() function to use a proper SMTP server.

Installation and Configuration:

  1. Install the Plugin: Navigate to Plugins > Add New and search for "WP Mail SMTP". Install and activate the plugin.

  2. Configure SMTP Settings: Go to WP Mail SMTP settings and enter the SMTP server details. Common settings include:

    • SMTP Host: e.g., smtp.gmail.com

    • SMTP Port: e.g., 587 (TLS) or 465 (SSL)

    • Authentication: Enable and enter your SMTP username and password.

  3. Send Test Email: Use the built-in functionality to send a test email and verify the configuration.

SMTP Plugins

Apart from WP Mail SMTP, there are other SMTP plugins like Easy WP SMTP and Post SMTP that offer similar functionality. These plugins also allow you to configure SMTP settings and ensure emails are sent through a reliable server.

Sending Emails via Code

For more control over email content and functionality, you can send emails directly via code using the wp_mail() function. This is the recommended approach.

Using wp_mail() Function

The wp_mail() function is a versatile tool in WordPress for sending emails. It accepts the following parameters:

wp_mail( $to, $subject, $message, $headers = '', $attachments = array() );
  • $to: Recipient email address.

  • $subject: Email subject.

  • $message: Email message body.

  • $headers: Additional headers, such as 'From' and 'Content-Type'.

  • $attachments: Files to attach.

HTML Email Example

To send an HTML email, set the Content-Type header to text/html.

function send_html_email() {
    $to = 'recipient@example.com';
    $subject = 'HTML Email Test';
    $message = '<h1>Hello, World!</h1><p>This is a test email in HTML format.</p>';
    $headers = array('Content-Type: text/html; charset=UTF-8');

    wp_mail( $to, $subject, $message, $headers );
}

Plaintext Email Example

Plaintext emails are simpler but lack formatting capabilities.

function send_plaintext_email() {
    $to = 'recipient@example.com';
    $subject = 'Plaintext Email Test';
    $message = "Hello, World!\nThis is a test email in plaintext format.";
    $headers = array('Content-Type: text/plain; charset=UTF-8');

    wp_mail( $to, $subject, $message, $headers );
}

Creating Multipart Emails

Multipart emails contain both HTML and plaintext versions, ensuring compatibility with various email clients.

function send_multipart_email() {
    $to = 'recipient@example.com';
    $subject = 'Multipart Email Test';

    $html_message = '<h1>Hello, World!</h1><p>This is a test email in HTML format.</p>';
    $plaintext_message = "Hello, World!\nThis is a test email in plaintext format.";

    $boundary = wp_generate_password( 24, false );

    $headers = array(
        'Content-Type: multipart/alternative; boundary=' . $boundary,
    );

    $message = "--{$boundary}\r\n";
    $message .= "Content-Type: text/plain; charset=UTF-8\r\n\r\n";
    $message .= $plaintext_message . "\r\n";
    $message .= "--{$boundary}\r\n";
    $message .= "Content-Type: text/html; charset=UTF-8\r\n\r\n";
    $message .= $html_message . "\r\n";
    $message .= "--{$boundary}--";

    wp_mail( $to, $subject, $message, $headers );
}

Best Practices for Email Deliverability

To ensure your emails are delivered effectively, follow these best practices:

  1. Set up SPF, DKIM, and DMARC records for your domain.

  2. Services like SendGrid, Mailgun, or Amazon SES offer high deliverability rates.

  3. Regularly remove inactive or bounced email addresses.

  4. Use a good balance of text and images, and avoid common spam triggers in your content.

  5. Use tools to track open rates, bounce rates, and other metrics to ensure your emails are being delivered and read.

  6. Don't send unsolicited email :)

Wrapping Up and Further Reading

Sending emails from WordPress the right way involves more than just using the built-in wp_mail() function. By configuring SMTP settings, using plugins, and following best practices for email deliverability, you can ensure your emails reach their recipients and look great in any email client. Whether you choose to send HTML, plaintext, or multipart emails, understanding the intricacies of email communication in WordPress will greatly enhance your site's functionality and user experience.

For more in-depth reading, consider these resources:

Did you find this article valuable?

Support Accreditly by becoming a sponsor. Any amount is appreciated!