1

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 = "myGmailusername@gmail.com";
     $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: )]
jz3
  • 521
  • 3
  • 13
  • 21

2 Answers2

1

Try looking here first:

Send email using the GMail SMTP server from a PHP page

Community
  • 1
  • 1
nicholasklick
  • 1,212
  • 10
  • 14
  • 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 – jz3 Jul 30 '10 at 16:46
  • 1
    Nevermind, I tried another account and it worked, thanks for your help. – jz3 Jul 30 '10 at 16:56
1

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/

C. E.
  • 10,297
  • 10
  • 53
  • 77