SMTP implementation on EC2 using Pear Mail package (PHP)

I am working on sending out SMTP relay email’s using Gmail as my server using Pear Mail package in PHP. I have installed Pear package using YUM. Also I have located the path for pear_dir and included the same in the php.ini file.
How ever when I try to run my .php file for the mail (code attached below) the file does not run..
Any pointers as to what I am doing wrong here?

<?php
require_once "Mail.php";

$from = "Sandra Sender <xyx@gmail.com>";
$to = "Ramona Recipient <xyz@gmail.com>";
$subject = "Hi!";
$body = "Hi,\n\nHow are you?";

$host = "ssl://smtp.gmail.com";
$username = "xyz@gmail.com";
$password = "XXXXXXXXXXXX";

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

Answer

not sure about the PEAR package thing but you seem to be missing the port which is 465

you could just try this below if the PEAR package does not explicitly define a port paramater.

change ssl://smtp.gmail.com to ssl://smtp.gmail.com:465

Attribution
Source : Link , Question Author : Anshuman , Answer Author : Abhishek Dujari

Leave a Comment