Sending emails using PHPMailer v5.0.0 and GMail

Requirements:

1 : PHPMailer v5.0.0 and above
2 : GMail account

Download the PHPMailer v5.0.0 and above from http://sourceforge.net/project/showfiles.php?group_id=26031&package_id=252700

Now create a file you can call mail.php on your system and open it with your favorite text editor.
Paste below code into it and save.

<?php
date_default_timezone_set(‘Asia/Calcutta’); // to set default timezone
require_once(‘../class.phpmailer.php’);
// optional, gets called from within class.phpmailer.php if not already loaded
//include(“class.smtp.php”);

$mail = new PHPMailer();
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "mail.yourdomain.com"; // SMTP server
// enables SMTP debug information (for testing) // 1 = errors and messages // 2 = messages only
$mail->SMTPDebug = 2;
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = "tls"; // sets the prefix to the servier
$mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server
$mail->Port = 587; // set the SMTP port for the GMAIL server
$mail->Username = "yourusername@gmail.com"; // GMAIL username
$mail->Password = "yourpassword"; // GMAIL password
$mail->SetFrom(‘name@yourdomain.com’, ‘First Last’);
$mail->AddReplyTo("name@yourdomain.com","First Last");
$mail->Subject = "PHPMailer Test Subject via smtp (Gmail), basic";
// optional, comment out and test
$mail->IsHTML(true); // send as HTML
$mail->Subject = "This is the subject";
$mail->Body = "Hi,This is the HTML BODY "; //HTML Body
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!";
$address = "towhom@towhomdomain.com";
$mail->AddAddress($address, "First Last");
$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!";
}
?>

For any ssl exceptions/ errors:

change fgets() to @fgets() to surpass the ssl error

———————–
At class.smtp.php

before line 104 (before #connect), use this lines:

$host = “ssl://smtp.gmail.com”;
$port = 465;

———————–
In the PHP.ini, you need to have this option:

extension=php_openssl.dl

Additional Resources:

http://phpmailer.worxware.com/index.php?pg=examples
http://www.askapache.com/php/phpfreaks-eric-rosebrocks-phpmailer-tutorial.html

Windows environments, this seems to work:
544-configuring-php-under-windows-use-gmail-external-smtp-server-ssl

Post to Twitter Post to Yahoo Buzz Post to Delicious Post to Digg Post to Reddit Post to StumbleUpon

Share or Bookmark This Post With :

   
Subscribe to RSS Feeds Follow me on Twitter Add to Favourite
 
Sponsors
 
Tags
 
Network [+]
 
© Copyright 2009 - 2011 TechiePark.com. All Rights Reserved | Powered by WordPress
This work is licensed under a Creative Commons Attribution 3.0 Unported License
Php5 powered    Mysql powered    Apache powered   Best viewed with  Spread Firefox Affiliate Button