Pages

Monday, March 8, 2010

Sending Mail from PHP using WAMP

This a tutorial to send mail using PHP i.e., from localhost to the internet. We need to install pear Mail package inorder to send mail using PHP. Do the following steps to install pear mail package
  • Move to the directory where WAMP Server or PHP is installed using the command prompt. For example C:\wamp\bin\php\php5.3.0\>
  • Then run "go-pear.bat" file for complete installation of PEAR
  • Edit php.ini file and include the path to pear(include_path = “.;e:\wamp\bin\php\php5.3.0\includes;e:\wamp\bin\php\php5.3.0\PEAR;e:\wamp\bin\php\php5.3.0\PEAR\Mail") and also edit ini file extension and set extension=php_openssl.dll to enable openssl
  • ph5.3.0\>pear list (this command will list all the installed pear packages)
  • pear download Mail (A zip file will be downloaded)
  • pear install Mail
  • pear install -a Net_SMTP(-a switch for installing all dependencies)
  • Then go to the mail.php file in e:\wamp\bin\php\php5.3.0\PEAR\Mail\mail.php and add this line require_once "e:\wamp\bin\php\php5.3.0\PEAR\Mail.php";(If not added it shows error that class Mail cannot be found )
  • A note: If you are using php 5.3 then you might get a problem when installing pear. It will say that the command cannot be found... To overcome this you have to download go-pear.phar file and save in with that name in the php5.3.0 directory
Once done installing you can go straight away with the program. Here is the simple mail program that sends mail from php using SMTP

< ?php
include "e:\wamp\bin\php\php5.3.0\PEAR\Mail\mail.php";
$from = "xxx@gmail.com ";//Sender Mail Address
$to =  "yyy@yahoo.co.in ";// Recepient mail address
$subject = "Hi!";
$body = "Hi,\n\nHow are you?";
$host = "ssl://smtp.gmail.com";
$port = "465";
$username = "xxx@gmail.com";
$password = "";// Type your Password Here
$headers = array ('From' => $from,'To' => $to,'Subject' => $subject);
$smtp = Mail::factory('smtp',array ('host' => $host,'port' => $port,'auth' => true,'username' => $username,'password' => $password));
$mail = $smtp->send($to, $headers, $body);
if (PEAR::isError($mail)) {
               echo( $mail->getMessage());
 }
else {
               echo("Message successfully sent!");
 }
?>

0 comments:

Post a Comment