how send email using gmail’s smtp server with PHP

Posted by midhun pottmmal On Friday, 28 December 2012 0 comments

         This post explains you how to “Sending Mail using SMTP and PHP” using SMTP credentials. In this post will show you how to send email using Gmail’s SMTP server.



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

example

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<?php
require_once "Mail.php";

$from    = "mail1@gmail.com";
$to      = "mail2@gmail.com";
$subject = "Test Message!";
$body    = "Its working!";

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

$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!";
}

else
{
    echo $mail->getMessage();
}
?>

0 comments:

Post a Comment