Sometimes we may need to send emails from localhost for the purpose of testing, and as you know that localhost doesn't support sending email function. Recently I came across a useful and effective PHP library called PHPMailer developed by PHPMailer team, the PHPMailer Library is very flexible and easy and you can simply send SMTP mail easily from your local server to the specified email
- in the beginning you will need to download the PHPMailer library from PHPMailer Library.
- in this example i chose to send STMP mail using Gmail Account, as you can configure other services such as Yahoo or Hotmail according to your desire.
- create a file named "test.php".
the PHP code:
<?php
require 'PHPMailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = 'yourgmailaccount@gmail.com';
$mail->Password = 'yourpassword';
$mail->SMTPSecure = 'tls';
$mail->From = 'yourgmailaccount@gmail.com';
$mail->FromName = 'the Recipient Name ';
$mail->addAddress('additionaladdress@gmail.com', 'add Title');
$mail->addReplyTo('yourgmailaccount@gmail.com', 'add title');
$mail->WordWrap = 50;
$mail->isHTML(true);
$mail->Subject = 'PHPMailer';
$mail->Body = 'Hi, this is a test to send a SMTP mail from localhost using PHPMailer library ';
if(!$mail->send()) {
echo 'Email could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
exit;
}
echo 'Email has been sent';
?>
No comments:
Post a Comment