by Jeffrey van Rossum

Simplifying WordPress Emails with a Fluent API

As WordPress developers, we often need to send emails that look polished and professional. But out of the box, the wp_mail does not provide that. That’s where my latest package, WP Mail, comes in—a lightweight package designed to make sending good looking responsive emails in WordPress very easy.

With WP Mail, you can make emails using a clean, fluent API or leverage a view template. Whether it’s a simple notification or a styled email with buttons and unsubscribe links.

Here’s a quick example of how it works:

use Jeffreyvr\WPMail\Mail;

Mail::make()
    ->to('[email protected]')
    ->subject('Hello')
    ->line('Hello there')
    ->button('Click me', 'https://vanrossum.dev')
    ->line('Kind regards, Jeffrey')
    ->send();

You can also use a view template with $mail to access the mail object, or add features like CC, BCC, or unsubscribe links with simple methods.

These and the other methods available are listed below:

  • subject(string $subject) - Set the email subject
  • to(string $email, string $name = '') - Add recipient
  • cc(string $email, string $name = '') - Add CC recipient
  • bcc(string $email, string $name = '') - Add BCC recipient
  • from(string $email, string $name = '') - Set sender information
  • line(string|callable $text) - Add a text line to the email
  • button(string $text, string $url) - Add a call-to-action button
  • view(string $path, array $data = []) - Use a view template
  • plainText(string|callable $plainText) - Set the plain text version of the email
  • unsubscribeUrl(string $unsubscribeUrl) - Set the unsubscribe URL
  • unsubscribeText(string $unsubscribeText) - Set the unsubscribe text
  • send() - Send the email

To get started, install via Composer:

composer require jeffreyvanrossum/wp-mail

If you want to execute the sending of emails in a background job, try pairing it with the wp-job-scheduler package.

WP Mail is open-source and available on GitHub. If you’re looking to easily send good looking responsive emails in your WordPress projects, give it a try and let me know your feedback!

Comments
Talk about this article on X.
Did you like this post?
If you sign up for my newsletter, I can keep you up to date on more posts like this when they are published.