Thursday, April 04, 2013

php: send email through gmail via mail

1. setup php and include mail:
this is how i did it on my linux box
# yum install php-pear
# pear install --alldeps Mail-1.2.0

2. to use mail in php:
// email settings
require_once "Mail.php";
$username = "<gmail email address here>@gmail.com";
$password = "<gmail email password here>";
$from = "<gmail email address here>@gmail.com";
$host = "ssl://smtp.gmail.com";
$port = "465";

$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, $message);

if(PEAR::isError($mail))
{
$resultstring = "email sending error: " . $mail->getMessage();
}
else
{
$resultstring = "email sent to $to";
}

No comments:

Post a Comment

SSH : No matching host key type found. Their offer: ssh-rsa,ssh-dss

Got this while connecting to my mikrotik router via ssh   Unable to negotiate with <ip address> port <ssh port>: no matching hos...