phpmailerHow can I use PHPMailer with XAMPP on a localhost?
PHPMailer is a library used to send emails using PHP. It can be used with XAMPP on a localhost by following these steps:
- Download and install XAMPP.
- Download and install PHPMailer from Github.
- Create a new file, for example
mailer.php
, and include the PHPMailer library:
<?php
require 'path/to/PHPMailer/src/PHPMailer.php';
require 'path/to/PHPMailer/src/SMTP.php';
?>
- Create a new instance of the PHPMailer class and set the necessary parameters:
<?php
//Create a new PHPMailer instance
$mail = new PHPMailer;
//Set who the message is to be sent from
$mail->setFrom('[email protected]', 'First Last');
//Set an alternative reply-to address
$mail->addReplyTo('[email protected]', 'First Last');
//Set who the message is to be sent to
$mail->addAddress('[email protected]', 'John Doe');
//Set the subject line
$mail->Subject = 'PHPMailer XAMPP test';
//Read an HTML message body from an external file, convert referenced images to embedded,
//convert HTML into a basic plain-text alternative body
$mail->msgHTML(file_get_contents('contents.html'), __DIR__);
//Replace the plain text body with one created manually
$mail->AltBody = 'This is a plain-text message body';
?>
- Finally, send the mail:
<?php
//send the message, check for errors
if (!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
?>
- Start the XAMPP server and open the
mailer.php
file in a browser to send the email. - Check the inbox of the recipient to see if the mail has been sent successfully.
Helpful links
More of Phpmailer
- How do I use PHPMailer to attach a ZIP file?
- How can I configure PHPMailer to work with GoDaddy?
- How can I configure PHPMailer to support Polish characters?
- How do I check which version of PHPMailer I'm using?
- How can I set up PHPMailer to use Zimbra SMTP?
- How do I use PHPMailer to send a file?
- How do I enable debug mode in PHPMailer?
- How can I use PHPMailer SMTPDebug to debug my email sending process?
- How do I disable STARTTLS in PHPMailer?
- How do I use PHPMailer with Yii2?
See more codes...