Tag Archives: PHPMailer

Setup Mailtrap with WordPress

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:

1
2
3
4
5
6
7
8
9
10
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');
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!