Mailtrap: A Fake SMTP Server for Pre-Production Email Testing

Emails are an important part of any website. We used to send emails about user registration, newsletters, new deals, coupons, etc. Before these emails start sending to real users, we should do email testing in the development phase. In this article, we study how to do sandbox testing for your emails.

Mailtrap is a fake SMTP testing server that is useful for pre-production email testing. Using Mailtrap, you will get your own inbox where you can check whether your emails are displaying properly or not.

Mailtrap allows 500 emails per month in your inbox for free. For a small application, 500 emails are sufficient with a free plan. Please check their Pricing page if you require an additional quota.

Get Mailtrap SMTP Credentials

Before writing the actual code for emails, you need to first sign up on Mailtrap and get the credentials. Although Mailtrap provides a paid plan users don’t need to enter credit card details for registration.

Upon registration, grab your SMTP server credentials. You can change these credentials at any time. Refer to the screenshot below.


Now, let’s test one email using PHPMailer and Mailtrap SMTP server.

Mailtrap Integration for Email Testing

For email testing with Mailtrap, we will send one dummy email. When using the Mailtrap SMTP server, the email will not be sent to the real user’s inbox. Instead, it will be sent to your Mailtrap inbox.

We use the PHPMailer library for sending an email. You should have Composer installed on your system to install the library. Open the terminal in your project root directory and run the command below:

composer require phpmailer/phpmailer

In the below code, replace the placeholders with your credentials for the Mailtrap SMTP server. Also, set the emails as per your requirement. For now, I am using my emails.

//Import PHPMailer classes into the global namespace
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require_once 'vendor/autoload.php';
$mail = new PHPMailer(true);
try {
    $mail->Host = '';    //mailtrap SMTP server
    $mail->SMTPAuth = true;
    $mail->Username = 'YOUR_USERNAME';   //username
    $mail->Password = 'YOUR_PASSWORD';   //password
    $mail->Port = 465;                   //smtp port
    $mail->setFrom('', 'Artisans Web');
    $mail->addAddress('', 'Sajid');
    $mail->Subject = 'Mailtrap Email';
    $mail->Body    = 'Hello User, <p>This is a test mail sent through Mailtrap SMTP</p><br>Thanks';
    if (!$mail->send()) {
        echo 'Message could not be sent.';
        echo 'Mailer Error: ' . $mail->ErrorInfo;
    } else {
        echo 'Message has been sent';
} catch (Exception $e) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;

Upon running the above code, your email should be sent to your Mailtrap inbox as follows:


Under the Spam Analysis tab, you can check the spam score and blacklisting of your message and server. ‘Check HTML’ tab will list out HTML validation against your email.

I also recommend checking out the article How to Send Emails in PHP written on the Mailtrap blog.

Related Articles

If you liked this article, then please subscribe to our YouTube Channel for video tutorials.

1 thought on “Mailtrap: A Fake SMTP Server for Pre-Production Email Testing

Leave a Reply

Your email address will not be published. Required fields are marked *