Set up Mailtrap with WordPress

Spider web strung across grass leaves.
Charlotte, is that you?

Mailtrap is a service that captures all mail sent by your site and prevents it from arriving at the intended recipient. It’s really useful for testing and debugging mail without having to write manual checks to ensure your users don’t get spammed with tests.

Setting up Mailtrap with WordPress is pretty straightforward, because all we need to do is overwrite the default SMTP server in the PHPMailer, which is what WordPress uses to send mail. It’s important to note that it will also trap mail sent from plugins like Contact Form 7.

Add the following hook in your theme’s functions.php file or throw this in a plugin:

function mailtrap($phpmailer) {
  $phpmailer->isSMTP();
  $phpmailer->Host = 'mailtrap.io';
  $phpmailer->SMTPAuth = true;
  $phpmailer->Port = 25;
  $phpmailer->Username = 'username';
  $phpmailer->Password = 'password';
}

add_action('phpmailer_init', 'mailtrap');

Be sure to replace the username and password with your personal ones, which can be found by clicking on your inbox in Mailtrap. Look for the SMTP credentials.

Once the hook is in place, you’re done. Give it a go!

Featured image by Nick Fewings.


Comments (2)

Previously posted in WordPress and transferred to Ghost.

cone crusher
May 7, 2015 at 4:29 am

When I write Username = ‘username', has fault, but now has final, thank you share.

PRASENJIT GHOSH
March 6, 2018 at 6:48 am

This was very helpful and crisp article to clear doubts about the purpose of the Mailtrap utility. Thanks.