Sunday 13 November 2011

How can send email in php from your localhost.

This is code which can be used to send email from your local host.


<?php
 require_once "Mail.php";

 $from = "Sender <puskun.pericent@gmail.com>";
 $to = "Recipient <kuntal.pushpendra@gmail.com>";
 $subject = "Hi!";
 $body = "Hi,\n\nHey Kuntal, you done it...";

 //$host = "mail.example.com";
 $host = "smtp.gmail.com";
 $username = "your usename for smtp server";
 $password = "your password for smtp server";

 $headers = array ('From' => $from,
   'To' => $to,
   'Subject' => $subject);
 $smtp = Mail::factory('smtp',
   array ('host' => $host,
     'auth' => true,
     'username' => $username,
     'password' => $password));

 $mail = $smtp->send($to, $headers, $body);

 if (PEAR::isError($mail)) {
   echo("<p>" . $mail->getMessage() . "</p>");
  } else {
   echo("<p>Message successfully sent!</p>");
  }

 ?>