Sunday 29 December 2013

Sending Mail using PHP

Recently, I was trying to send mails using php script
I found out a couple of alternatives to get the work done.
There are a lot of good sites sharing this information. I am just sharing the steps  I took to get the job done.
I have used "Mail:factory " to send the mail. Again there a lot of options here, I have just used the most convenient one for me. Others are free to try and post
any options they can find.

Here goes nothing,

<?php
require_once "Mail.php";

 $from = "from where you want";

 $to = "send to your friend";
 $subject = "Testing No Reply! Pl. Ignore";
 $body = "Hi,\n\nTesting No Reply Functioning?";

 $host = "smtp server address";
 $port = "25"; #commonly 25 please check the port you are using

 $username = "USERNAME";
 $password = "PASSWORD";

 $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("<p>" . $mail->getMessage() . "</p>");
  } else {
   echo("<p>Message successfully sent!</p>");
  }
?>



As you might have guessed, you will have to install "mail" for this to work For this we will install "pear" The simple command is (for linux : apt or yum)
sudo apt-get install php-pear
Once we are done with pear installation we will install the mail application
pear install mail
If you get a message saying "no such package" then please check the internet connection. If you need to set the proxy for pear package then please use
pear config-set http_proxy
 http://username:password@your_proxy_server:port
Note: The whole command is a to be used in a single line. Please do not press the return key

This should pretty much to the trick.

I received a lot of help from Nitin Dalvi with regards to the script
The code block highlighting method can be found at the following link http://mateenmoosa.wordpress.com/2011/08/09/blogger-code-block/

No comments:

Post a Comment