0
votes

Send email using the GMail SMTP server from a PHP page

I'm trying to get this to work. According to the answer in the given link "it's working code so use it". Except it doesn't work for me.

In particular:

<?php

       require_once "Mail.php";

        $from = "<xxxxx.gmail.com>";
        $to = "<xxxxx.gmail.com>";
        $subject = "Hi!";
        $body = "Hi,\n\nHow are you?";

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

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

        if (PEAR::isError($mail)) {
          echo("<p>" . $mail->getMessage() . "</p>");
         } else {
          echo("<p>Message successfully sent!</p>");
         }

    ?>  <!-- end of php tag-->

Produces:

Strict Standards: Non-static method Mail::factory() should not be called statically in F:\xampp\htdocs\test\index.php on line 23

Strict Standards: Non-static method PEAR::isError() should not be called statically, assuming $this from incompatible context in F:\xampp\php\PEAR\Mail\smtp.php on line 365

Strict Standards: Non-static method PEAR::isError() should not be called statically, assuming $this from incompatible context in F:\xampp\php\PEAR\Net\SMTP.php on line 450

Strict Standards: Non-static method PEAR::isError() should not be called statically, assuming $this from incompatible context in F:\xampp\php\PEAR\Net\SMTP.php on line 467

While phpinfo() gives registered sockets:

tcp, udp, ssl, sslv3, sslv2, tls

But most importantly, no email sent.

2

2 Answers

0
votes

Try setting correct email addresses in $from and $to

'[email protected]' instead of '<PeterMorgan.gmail.com>'

From PEAR Mail documentation:

include('Mail.php');

$recipients = '[email protected]';

$headers['From']    = '[email protected]';
$headers['To']      = '[email protected]';
$headers['Subject'] = 'Test message';

$body = 'Test message';

$params = ... // set smtp values here
              // http://pear.php.net/manual/en/package.mail.mail.factory.php

$mail_object =& Mail::factory('smtp', $params);

$mail_object->send($recipients, $headers, $body);
0
votes

Figured out the issue. For GMail they say on their website:

Port for TLS/STARTTLS: 587

Port for SSL: 465

If you're having trouble sending mail but you've confirmed that encryption is active for SMTP in your mail client, try to configure your SMTP server on a different port (465 or 587).

So I changed the port from 465 to 587, even though I am using SSL. I don't know why this works for me but it does.