Thursday, January 7, 2010

PHP with GMAIL SMTP


Test for WampServer Version 2.0 on Windows XP

This explains how to use gmail to send emails with attachments in php using PHPMailer using gmail example:

1. Download PHPMailer from http://phpmailer.sourceforge.net (5.0 tested)
2. Extract to folder phpmailer

3. Open your php.ini file, uncomment the extension=php_openssl.dll.2. Save the php.ini file

4. Restart your WAMP Server.

5. Create gmail.php file paster following gmail smtp code :

Note: you can find this example in folder (PHPMailer\examples\test_smtp_gmail_basic.php)

error_reporting(E_STRICT);
date_default_timezone_set('America/Toronto');
require_once('class.phpmailer.php');
include("class.smtp.php");
$mail = new PHPMailer();
$body = file_get_contents('contents.html');
$body = eregi_replace("[\]",'',$body);

IsSMTP(); // telling the class to use SMTP
$mail->SMTPDebug = 2; // enables SMTP debug information (for testing)

$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = "ssl"; // sets the prefix to the servier
$mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server
$mail->Port = 465; // set the SMTP port for the GMAIL server
$mail->Username = "gmailaccount@gmail.com"; // GMAIL username
$mail->Password = "gmailpassword"; // GMAIL password
$mail->SetFrom('fromemailaddress', 'First Last');
$mail->AddReplyTo("replyemailaddress","First Last");
$mail->Subject = "PHPMailer Test Subject via smtp (Gmail), basic";
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->MsgHTML($body);
$address = "toemailaddress";
$mail->AddAddress($address, "John Doe");
$mail->AddAttachment("images/phpmailer.gif"); // attachment
$mail->AddAttachment("images/phpmailer_mini.gif"); // attachment
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
?>

0 comments: