DEV Community

Cover image for Send email from WordPress the right way
Accreditly
Accreditly

Posted on

Send email from WordPress the right way

Sending emails is a critical feature for many WordPress websites, whether it’s for account confirmations, notifications, or customized alerts. While many developers might default to using PHP’s mail() function, WordPress offers more robust and reliable alternatives. Let's explore the best practices for sending both plaintext and HTML emails within WordPress.

Why Avoid PHP’s mail() Function?

While PHP’s mail() function is straightforward, it has several drawbacks, particularly in terms of flexibility and reliability. It lacks support for SMTP authentication, which can lead to emails being flagged as spam. WordPress's built-in functions are designed to overcome these limitations, offering better formatting options and more consistent delivery rates.

Sending Plaintext Emails with WordPress

WordPress makes it easy to send plaintext emails using the wp_mail() function, which is more versatile than PHP’s native mail() function.

Here’s a basic example of how you can use wp_mail():

$recipient = 'user@example.com';
$subject = 'Welcome to Our Platform!';
$body = 'Thank you for joining us.';
wp_mail($recipient, $subject, $body);
Enter fullscreen mode Exit fullscreen mode

If you need to add headers, such as a custom "From" address or "Reply-To," you can do so with an array:

$headers = array('From: Support Team <support@example.com>', 'Reply-To: support@example.com');
wp_mail($recipient, $subject, $body, $headers);
Enter fullscreen mode Exit fullscreen mode

This approach ensures that your emails are sent with the correct headers, improving deliverability and reducing the likelihood of your emails being marked as spam.

Crafting HTML Emails in WordPress

If you want to send emails that are more visually engaging, such as newsletters or account information emails, HTML is the way to go. To send HTML emails in WordPress, you’ll need to set the content type to text/html. This can be achieved by using the wp_mail_content_type filter.

Here’s an example:

function set_html_mail_content_type() {
    return 'text/html';
}
add_filter('wp_mail_content_type', 'set_html_mail_content_type');

$recipient = 'user@example.com';
$subject = 'Your Account Information';
$body = '<html><body>';
$body .= '<h1>Welcome to Our Platform!</h1>';
$body .= '<p>Here are your account details.</p>';
$body .= '</body></html>';

wp_mail($recipient, $subject, $body);

// Reset content type to avoid affecting other emails
remove_filter('wp_mail_content_type', 'set_html_mail_content_type');
Enter fullscreen mode Exit fullscreen mode

In this example, we first define a function to set the content type to HTML and then add it as a filter. After sending the email, the filter is removed to ensure that subsequent emails are not inadvertently sent as HTML.

For a more in-depth discussion on the best practices for sending emails from WordPress, including additional code examples and troubleshooting tips, check out our full article here.

Enhance Your WordPress Skills

If you're looking to deepen your understanding of WordPress, including how to handle email functionality more effectively, consider pursuing our WordPress Development Certification. It’s a comprehensive program designed to take your skills to the next level, covering everything from basic setup to advanced customization techniques.

Top comments (3)

Collapse
 
ravavyr profile image
Ravavyr

Yea, no, don't do that. That will get your emails to go straight into a spam box.

Why?

  • First, you are missing most of the headers you do need for real emails. I wrote this first in 2015 and later on my codepen codepen.io/rstamper/pen/RwbQQGq [a reply header, a from header, a boundary for text and html emails, etc etc etc]
  • Next, your server/host is most likely not meant for sending emails and will be an unknown IP so most email clients will flag your emails as spam, especially outlook, aol, yahoo [which still account for a large number of users]
  • Lastly, to do this right and not end up dealing with a bajillion email issues, use a 3rd party email service like mailgun, mailersend, or even outlook365's SMTP and without coding you can install WP SMTP for free and add one of those services to send the emails for you. Most of them are cheap, some are still free, every business has different needs, but this should solve it for most.

Not ripping on ya, just pointing out what you missed.

Collapse
 
aaronfc profile image
Aaron Fc

True! But depending on the requirements that could be good enough :)

Just make sure you let your users know that your emails might be in spam!

Collapse
 
nasoma profile image
nasoma

lol. 2 emails in total. One with your message and the other to inform users that the first email might go to spam.