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
localhostand the port to1025 - Sets the
SMTPSecureoption 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 do I use PHPMailer to attach a ZIP file?
- How can I use PHPMailer with XAMPP on a localhost?
- How can I configure PHPMailer to work with GoDaddy?
- How do I use PHPMailer with OAuth 2.0?
- How to use PHPMailer with PHP 7.4?
- How can I configure PHPMailer to ignore TLS certificate errors?
- How do I configure PHPmailer to use Zoho SMTP?
- How can I use PHPMailer to send emails with a Yahoo account?
- How can I configure PHPMailer to support Polish characters?
- How can I use PHPMailer without SMTP secure?
See more codes...