Search code examples
phpemailsmtppear

PHP sending email using PEAR through gmail smtp


After a battle getting the correct location for pear my code does not generate any errors. Yet it will not send the email. The email body is generated from a form which first enters the data into a mysql database then emails the same data to a person who then uses the data for whatever. I have read tons of questions and answers on this forum and other (this being the best) and most of the code I have written is been inspired by this forum. The gmail account is valid and the password is correct. Any help or suggestions would be very much appreciated.

pear and pear mail is installed on the server and seems to be working (at least not creating any errors) Here is my code:

   //Email process code


   ini_set('display_errors','on');
   error_reporting(E_ALL);
   /*
   require_once 'System.php';
  if(class_exists('System')===true) {
  echo 'PEAR is installed!';
  } else {
  echo 'PEAR is not installed :(';
   }
  */

  set_include_path('/home/surfcup/php/');
  require_once "Mail.php";


  $from = "<[email protected]>";
  $ToEmail = "<[email protected]>"; 


  $EmailSubject = 'Hotel Registration Form'; 

  $host ="ssl://smtp.gmail.com";
  $port ="465";
  $username = "[email protected]";
  $password =   "password";


  $headers = array ('From' => $from, 'To'=> $ToEmail, 'Subject'=> $EmailSubject);

  $smtp = Mail::factory('smtp',
    array ('host'=> $host,
           'port'=> $port,
           'auth'=> true,
           'username'=> $username,
           'password'=> $password));

  $MESSAGE_BODY = "Event: ".$Event."\n"; 
  $MESSAGE_BODY .= "Name: ".$realname."\n"; 
  $MESSAGE_BODY .= "Age Group: ".$Age."\n"; 
  $MESSAGE_BODY .= "Sex: ".$Sex."\n"; 
  $MESSAGE_BODY .= "Contact Name: ".$Contact_Name."\n"; 
  $MESSAGE_BODY .= "Contact Email: ".$Contact_Email."\n"; 
  $MESSAGE_BODY .= "Phone: ".$Phone."\n"; 
  $MESSAGE_BODY .= "Hotel Name: ".$Hotel_Name."\n"; 
  $MESSAGE_BODY .= "No Rooms: ".$No_Rooms."\n"; 
  $MESSAGE_BODY .= "Rate: ".$Rate."\n"; 
  $MESSAGE_BODY .= "Check In Date: ".$Check_In."\n"; 
  $MESSAGE_BODY .= "Check Out Date: ".$Check_Out."\n"; 
  $MESSAGE_BODY .= "Additional Info: ".$Add_Info."\n"; 



  $mail = $smtp->send($ToEmail, $headers, $MESSAGE_BODY);


        if (PEAR::isError($mail)){
            echo("<p>" . $mail->getMessage . "</p>");
            echo("\n");
            echo("<p>" . $mail->getDebugInfo . "</p>");

        }

Solution

  • Most errors I've seen regarding sending via GMAIL is that OpenSSL is not enabled on your own server. Check that extension is enabled in your php.ini

    (Also, can you error check "smtp" in case that fails.)