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


rahul said...

i have done the same as you have given above but on opening the link it is showing the error as given below, so pls solve the problem.

Warning: fsockopen() [function.fsockopen]: unable to connect to localhost:25 (A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. ) in C:\xampp\htdocs\xampp\mailer\_acp-ml\phpmailer\class.smtp.php on line 122
Error : SMTP Error: Could not connect to SMTP host.

Sanjeev Gopinath said...

this doesn't work with Proxy! Do you use any proxy??