Tuesday, November 8, 2011

Program to send Email in Php with Authentication

PEAR Installation

PEAR is an extension for PHP. These days PEAR is included with PHP, but you need to install it yourself. I use PEAR on all my PHP developments.

Installing PEAR:

Open a command line window (i.e. Start->Run->cmd)
Go to the PHP directory, in my case C:\php. Type go-pear.bat. Follow the instructions.
The PEAR extension get installed in C:\php\PEAR directory.
Once PEAR is installed, go to the php.ini file in your Apache2 directory. Find the

Code:
;include_path = ".;c:\php\includes"

Remove the semi-colon (to un-comment it), and then add C:\php\PEAR to it

Code:
include_path = ".;c:\php\includes;C:\php\PEAR"
;include_path = ".;C:\wamp\php\includes;C:\wamp\php\PEAR"

---------------------------------------------------------------------------------------------
** The 'pear' command is not currently in your PATH, so you need to
** use 'c:\wamp\php\pear.bat' until you have added
** 'C:\wamp\php' to your PATH environment variable.

* WINDOWS ENVIRONMENT VARIABLES *
For convenience, a REG file is available under C:\wamp\php\PEAR_ENV.reg .
This file creates ENV variables for the current user.

Double-click this file to add it to the current user registry.

-----------------------------------------------------------------------------------------------
#########Pear mail package Installation################

go to http://pear.php.net/package/Mail/download/ for supported Mail packages

pear install Mail-1.1.14
pear install --alldeps Mail-1.1.14

########Email with authentication#############

";
$to = "XYZ ";
$subject = "Hi!";
$body = "Hi,\n\nHow are you? This is test php mail.";

$host = "mail.domain.com";
$username = "user@myserver.com";
$password = "sdfdsf";

$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("

" . $mail->getMessage() . "

");
} else {
echo("

Message successfully sent!

");
}
?>

No comments: