Sunday, September 6, 2009

Use GMail SMTP to send mail in PHP

An example to use phpmailer class to send email using a gmail account.



<?php
include("class.phpmailer.php");
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPAuth   = true; // enable SMTP authentication
$mail->SMTPSecure = "ssl"; // use ssl
$mail->Host = "smtp.gmail.com"; // GMAIL's SMTP server
$mail->Port  = 465; // SMTP port used by GMAIL server
$mail->Username   = "your_gmail_account@gmail.com"; // GMAIL username
$mail->Password   = "password"; // GMAIL password
$mail->AddReplyTo("other_email@hotmail.com","FirstName LastName"); // Reply email address
$mail->From = "your_gmail_account@gmail.com";
$mail->FromName = "FirstName LastName"; // Name to appear once the email is sent
$mail->Subject = "Subject of the email"; // Email's subject
//$mail->Body = "Hello World,<br />This is the HTML BODY<br />"; //HTML Body
$mail->AltBody = "This is a test email"; // optional, comment out and test
$mail->WordWrap = 50; // set word wrap
$mail->MsgHTML($body); // [optional] Send body email as HTML
$mail->AddAddress("abc@email.com", "Test email");  // email address of recipient
//$mail->AddAttachment("files/files.zip"); // [optional] attachment
$mail->IsHTML(true); // [optional] send as HTML
if(!$mail->Send())
echo "Mailer Error: " . $mail->ErrorInfo;
else
echo "Message sent!";
?>


No comments: