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 do I configure PHPmailer to use Zoho SMTP?
- How can I configure PHPMailer to work with GoDaddy?
- How can I use PHPMailer to send emails with a Yahoo account?
- How do I check which version of PHPMailer I'm using?
- How can I resolve the issue of my PHPmailer username and password not being accepted?
- How can I send a UTF-8 encoded email using PHPMailer?
- How can I set a timeout for PHPMailer SMTP?
- How can I use PHPMailer SMTPDebug to debug my email sending process?
- How can I use PHPMailer with React?
See more codes...