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 subjectto(string $email, string $name = '')- Add recipientcc(string $email, string $name = '')- Add CC recipientbcc(string $email, string $name = '')- Add BCC recipientfrom(string $email, string $name = '')- Set sender informationline(string|callable $text)- Add a text line to the emailbutton(string $text, string $url)- Add a call-to-action buttonview(string $path, array $data = [])- Use a view templateplainText(string|callable $plainText)- Set the plain text version of the emailunsubscribeUrl(string $unsubscribeUrl)- Set the unsubscribe URLunsubscribeText(string $unsubscribeText)- Set the unsubscribe textsend()- 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!