Script to Log into an SMTP server – phpmailer
Monday, July 11, 2011 14:59Posted in category PHP
Best way to read and send mails is log into an SMTP server.
Download the latest phpmailer source code and copy it into your server.
And use the below script to send mails using SMTP server.
require_once("phpmailer/class.phpmailer.php");
require_once("phpmailer/class.smtp.php");
function smtp_auth_mail($to, $subject, $message, $headers, $auth = true)
{
$mail = new PHPMailer();
$body = $message;
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "localhost"; // SMTP server
$mail->Port = "465"; // SMTP port
$mail->SMTPSecure = "ssl"; // SMTP secure
$mail->From = "admin@xyz.com";
$mail->FromName = "XYZ Admin";
if ($auth)
{
$mail->SMTPAuth = true;
$mail->Username = "admin@xyz.com"; // in some servers @ will be replaced with +
$mail->Password = "xyz123";
}
$mail->Subject = $subject;
$body = str_ireplace("\n", "<br>", $body);
$mail->MsgHTML($body);
$mail->AddAddress($to);
if(!$mail->Send()) {
if (stripos($mail->ErrorInfo, 'Data not accepted') === false)
echo "Mailer Error: " . $mail->ErrorInfo;
return false;
} else {
return true;
}
}
smtp_auth_mail('xyz@gmail.com', 'Test mail', 'Test mail from XYZ', '');
Finally smtp_auth_mail function will return true after sending the mail successfully otherwise it will return false.
You can follow any responses to this entry through the RSS 2.0 feed.
You can leave a response, or trackback from your own site.
freesoft says:
July 11th, 2011 at 6:23 pm
Thank you..really informative!!
USMLE CRAM says:
July 12th, 2011 at 11:09 pm
{Informative|Interesting|Insightful}…
Thank you This was quite useful I will look into this further…
aranzacje wnetrz says:
July 13th, 2011 at 12:47 pm
My brother recommended I might like this web site. He was totally right. This post truly made my day. You cann’t imagine simply how much time I had spent for this info! Thanks!
Garage Doors Clifton says:
July 14th, 2011 at 6:54 pm
very nice post, i certainly love this website, keep on it