tags:

views:

98

answers:

2

Hey guys, I am trying to send email using my gmail account, and I'm getting an error for some reason. Would anyone know why it would give me this error? The script:

<?php

    set_include_path("/usr/share/php"); 

    include("Mail.php");

     $from = "email <email address>";
     $to = "<Emailaddress>";
     $subject = "Hi!";
     $body = "Hi,\n\nHow are you?";

     $host = "smtp.gmail.com";
     $username = "[email protected]";
     $password = "myPassword";

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

The Page resolves when I go to it, but gives the following error:

Failed to connect to smtp.gmail.com:25 [SMTP: Failed to connect socket: Connection refused (code: -1, response: )]
A: 

Try looking here first:

http://stackoverflow.com/questions/712392/send-email-using-gmail-smtp-server-from-php-page

nicholasklick
thanks, I altered my code according to the answer in that one, and it is mostly working now, but now I get an error saying "Validation Failed" for my Account, but I know for a fact I'm using the correct username/password
Joe
Nevermind, I tried another account and it worked, thanks for your help.
Joe
+1  A: 

Yes, it appears to connect to the wrong port. It connects to port 25 when it should connect to 465. Read the documentation and see how you can set the port with your class. It also needs to connect using ssl, here's an example of how to get it working:

http://neo22s.com/send-email-using-gmail-and-php/

Calle