phpmailerHow can I use PHPMailer with MailHog?
PHPMailer is an open source library for sending emails from PHP. It can be used with MailHog, a testing mail server for developers, by making a few simple configuration changes.
To use PHPMailer with MailHog, you need to set the SMTP
host to localhost
and the port
to 1025
. You can also set the SMTPSecure
option to tls
to enable encryption.
Below is an example of how to use PHPMailer with MailHog:
require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = 'localhost';
$mail->Port = 1025;
$mail->SMTPSecure = 'tls';
$mail->From = '[email protected]';
$mail->FromName = 'Example Sender';
$mail->addAddress('[email protected]', 'Example Recipient');
$mail->Subject = 'Test Email';
$mail->Body = 'This is a test email sent using PHPMailer with MailHog';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
Output example
Message has been sent
The above code does the following:
- Includes the PHPMailer library
- Sets the SMTP host to
localhost
and the port to1025
- Sets the
SMTPSecure
option totls
- Sets the sender and recipient addresses
- Sets the subject and body of the email
- Sends the email
- Outputs the result
Helpful links
More of Phpmailer
- How can I configure PHPMailer to support Polish characters?
- How can I configure PHPMailer to work with GoDaddy?
- How do I use PHPMailer to attach a ZIP file?
- How can I use PHPMailer in Yii 1?
- How can I set up PHPMailer to use Zimbra SMTP?
- How can I use PHPMailer without SMTP secure?
- How do I use PHPMailer to send a file?
- How can I send emails using PHPMailer without using SMTP?
- How do I use PHPMailer to encode emails in UTF-8?
- How to use PHPMailer with PHP 7.4?
See more codes...