Saturday 4 July 2009

Sending mails using PHP through a GMAIL account...

Hi,
This post will explain how to send mails using your Gmail account by using PHP script.
I use XAMPP, so the tutorial will be based on that... Modify if you use some other service! It'd work...

* Download the phpmailer from http://phpmailer.sourceforge.net .
* Extract the folder in htdocs of xampp.
* Now navigate to \_acp-ml\phpmailer\ and create a php page with the name you desire.
* In the section, paste the following code...!

include("class.phpmailer.php");
include("class.smtp.php");
$send_mail=new PHPMailer();
$send_mail->IsSMTP();
$send_mail->SMTPAuth = true;
$send_mail->Username = "someone@gmail.com"; // your gmail username
$send_mail->Password = "passwd"; // your gmail password
$webmaster_email = "someone1@gmail.com"; //Reply to this email ID
$email="someone@aaa.com"; // Recipients email ID
$name="Cool Dude"; // Recipient's name
$send_mail->From = $webmaster_email;
$send_mail->FromName = "Webmaster";
$send_mail->AddAddress($email,$name);
$send_mail->AddReplyTo($webmaster_email,"Webmaster");
$send_mail->WordWrap = 50; // set word wrap
//$send_mail->AddAttachment("/var/tmp/file.tar.gz"); // attachment
//$send_mail->AddAttachment("/tmp/image.jpg", "new.jpg"); // attachment
$send_mail->IsHTML(true); // send as HTML
$send_mail->Subject = "Test E-mail";
$send_mail->Body = "Hello there,
This is a test message..."; // Write your email body here!!!
$send_mail->AltBody = "This is a plain test message..."; //Text to be visible in Plain text mode..!
if(!$send_mail->Send())
{
echo "Error : " . $send_mail->ErrorInfo;
}
else
{
echo "The Message sending was successful...";
}
?>

Once you do this,
* Try accessing the file from localhost thru your browser.
* If this doesn't work,
* Find out the php.ini file from xampp folder, open it and uncomment extension=php_openssl.dll" by removing semicolon.
* Now, find the "#connect to the smtp server" line in class.smtp.php in phpmailer folder and add $host = "ssl://smtp.gmail.com"; $port = 465; before it.
* Now restart XAMPP by stopping and starting Apache once!

Try executing the php script, and you'd find this working...

Comment if any changes are to be made or if you found this useful...