Test sending emails in PHP with XAMPP and MailHog

Sending mail with PHP is easy. We can test it with a local SMTP server. MailHog is very useful for this purpose.

MailHog

MailHog is program thats creates a local SMTP mail sever for testing purposes. Let’s see how to configure MailHog.

Download MailHog

You can download MailHog from here . https://github.com/mailhog/MailHog/releases/latest . For 64 bit windows look for MailHog_windows_amd64.exe and for 32 bit MailHog_windows_386.exe

Configure MailHog

Rename the downloaded file to mailhog.exe and put it in C:\mailhog folder. So, the path for Mailhog executable will be C:\mailhog\mailhog.exe. Now add this to environment path variable.

1565861962130

Now open command prompt and type mailhog to run the SMTP server.

1565862665723

Open http://localhost:8025/ in your browser. You will see all the emails here you sent using PHP.

Update PHP configuration

Open php.ini and search for mail function. Set SMTP=127.0.0.1 and smtp_port=1025 in php.ini file.

[mail function]
; For Win32 only.
; http://php.net/smtp
SMTP=127.0.0.1
; http://php.net/smtp-port
smtp_port=1025

Update sendmail configuration

Open sendmail.ini from C:\xampp\sendmail\ directory and put smtp_server=127.0.0.1 and smtp_port=1025.

[sendmail]
smtp_server=127.0.0.1
smtp_port=1025

You can now use PHP’s mail() function to send email from your application.

Sending email

Now create a PHP file like the following

<?php

$to = 'recipients@email-address.com';
$subject = 'Hello from XAMPP!';
$message = 'This is a Mailhog test';
$headers = "From: your@email-address.com\r\n";

if (mail($to, $subject, $message, $headers)) {
echo "SUCCESS";
} else {
echo "ERROR";
}

When you run the file you will receive an email in the MailHog inbox.

1565863841973

This will also be helpful for local WordPress installation for resetting passwords.

Related posts

Md. Monirul Alom

Md. Monirul Alom

I am a Full Stack Web developer. I love to code, travel, do some volunteer work. Whenever I get time I write for this blog