Saturday, 4 July 2009

Sending mails using PHP through a GMAIL account...

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 .
* 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...!

$send_mail=new PHPMailer();
$send_mail->SMTPAuth = true;
$send_mail->Username = ""; // your gmail username
$send_mail->Password = "passwd"; // your gmail password
$webmaster_email = ""; //Reply to this email ID
$email=""; // Recipients email ID
$name="Cool Dude"; // Recipient's name
$send_mail->From = $webmaster_email;
$send_mail->FromName = "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..!
echo "Error : " . $send_mail->ErrorInfo;
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://"; $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...