With WordPress, send email function could be tricky to master. Sure, there’s wp_mail, but it’s unreliable for several reasons.
For example, WordPress web hosting services have different PHP mail configurations. Or they even block sending entirely to prevent spamming. Then, the native wp_mail has very limited capabilities when it comes to the type of emails you can send.
There are WordPress hosting providers that include usable configuration for each WordPress site, but it’s an exception rather than a rule. And if you’re tech-savvy, it’s possible to configure WordPress manually to ensure your emails get delivered.
Either way, this is a developer-minded guide that will lead you through different scenarios and show you how to integrate Mailtrap SMTP (Simple Mail Transfer Protocol) with your WordPress site.
How does WP send emails?
In a nutshell, WP sends emails using PHP. And you have three options to send emails in WordPress – use the native wp_mail function, use a WordPress plugin, or manually set up an SMTP server using wp-config.php.
The following sections give you the steps for each method and tell you about potential downsides. Beyond that, you’ll learn how to test sending on WP, include CC and Bcc, format emails to HTML, and resolve common email service issues.
How to send emails from WordPress using wp_mail() function?
Wp_mail() represents a PHPMailer class wrapper that utilizes the hosting web server when sending emails. But there are 24 more WP functions that use wp_mail(); combine that and web server sending, and it’s not surprising that this method is unreliable.
Here are the steps wp_mail() function takes when attempting to send your emails.
- The code snippet to specify the recipient, subject, message, etc., and prepare an email.
wp_mail( $to, $subject, $message, $headers, $attachments );
- After preparation, wp_mail() calls the PHP mail function, which is in the PHPMailer lib, to give instructions to the server to send emails.
mail(to, subject, message, headers, parameters);
- Your WordPress hosting provider attempts to deliver the email. More specifically, the hosting side servers request to use PHP mail to send emails. Then, these servers typically use SendMail or Postfix to route the emails to their destination, and the receiving host rejects or accepts them.
Rejects are quite often, and it’s not uncommon that your email won’t be sent out at all. As mentioned, this depends on several factors, primarily your WP hosting provider and the limitations of wp_mail() function.
Note that WordPress native function makes it tricky to build HTML templates or add attachments and images without additional tweaks. More importantly, it struggles with email headers, which is one of the reasons emails sent using native wp_mail() may land in spam.
How to send emails using the WordPress SMTP plugin?
Installing a WordPress SMTP plugin resolves many of the native configuration issues.
How?
For starters, a plugin reconfigures wp_mail () function to use SMTP rather than relying on PHP. Then, a plugin routes the outgoing email through an email sending service.
This may mean that you need to set up an account and verify your WordPress site with a third-party sending service.
Of course, there are a few steps to the process, and you’ll need an additional tool to get a reliable SMTP. But a combination of an email delivery platform such as Mailtrap and an email plugin gives you the upper hand to streamline your email delivery and protect your sender and domain reputation.
The following sections guide you through Mailtrap and WP Mail SMTP plugin setup.
1.Set up Mailtrap and verify your domain
Go to Mailtrap and create an account. Then, access Mailtrap in-app homepage and click Setup Domain under Email API.