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 can I configure PHPMailer to support Polish characters?
- How can I use PHPMailer without SMTP secure?
- How can I use PHPMailer with XAMPP on a localhost?
- How do I set the X-Mailer header using PHPMailer?
- How can I send emails using PHPMailer without using SMTP?
- How do I use PHPMailer to attach a ZIP file?
- How do I use PHPMailer with OAuth2 authentication for Microsoft accounts?
- How do I configure PHPMailer to use TLS 1.2?
- How can I set up PHPMailer to use Zimbra SMTP?
- How can I configure PHPMailer to work with GoDaddy?
See more codes...