Saturday, May 25, 2013

Send email using gmail’s smtp server using PHP



This tutorial  explains how to “Send Mail using SMTP and PHP” using SMTP credentials

You need to install two pear packages, Mail and SMTP. Code is quite self-explanatory.

<?php

require_once "Mail.php";
$from    = "test@gmail.com";
$to      = "test2@gmail.com";
$subject = "Email Subject";
$body    = "Content For the email";

$config=array(
    'host'      => 'ssl://smtp.googlemail.com',
    'port'      => 465,
    'auth'      => true,
    'username'  => 'write your gmail username here',
    'password'  => 'write your gmail password here'
);

$headers=array(
    'From'      => $from,
    'To'        => $to,
    'Subject'   => $subject
);

$smtp = Mail::factory('smtp',$config);

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

if(!(PEAR::isError($mail)))
{
    echo "Mail sent successfully!"; // On Success
}

else
{
    echo $mail->getMessage(); // On Failure
}

?>        

No comments:

Post a Comment