views:

25961

answers:

9

I am trying to send an email via GMail's SMTP server from a PHP page but I get this error:

authentication failure [SMTP: SMTP server does no support authentication (code: 250, response: mx.google.com at your service, [98.117.99.235] SIZE 35651584 8BITMIME STARTTLS ENHANCEDSTATUSCODES PIPELINING)]

Can anyone help? Here is my code:

<?php
require_once "Mail.php";

$from = "Sandra Sender <[email protected]>";
$to = "Ramona Recipient <[email protected]>";
$subject = "Hi!";
$body = "Hi,\n\nHow are you?";

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

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

Set

'auth' => false,

Also, see if port 25 works.

CookieOfFortune
crb
+17  A: 

Your code does not appear to be using TLS (SSL), which is necessary to deliver mail to Google (and using ports 465 or 587).

You can do this by setting

$host = "ssl://smtp.gmail.com";

Your code looks suspiciously like this example which refers to ssl:// in the hostname scheme.

crb
+1  A: 

You are trying to use GMAIl to send email from another account? I dont think gmail will support this also you have not mentioned about SSL. you can refer this tutorial.

Shoban
A: 

May I'm wrong, but gmail is not an open smtp relay (ok, I'm fairly sure about this part), so the sender or the recipient might have to be a gmail address.

Csaba Kétszeri
The sender provides their Gmail credentials in the script, as shown above.
crb
A: 

gmail require port 465 and also its the code from phpmailer :)

+2  A: 

The code as listed in the question needs two changes

$host = "ssl://smtp.gmail.com";
$port = "465";

Port 465 is required for an SSL connection.

s01ipsist
+10  A: 
<?php

       require_once "Mail.php";

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

        $host = "ssl://smtp.gmail.com";
        $port = "465";
        $username = "<myaccount.gmail.com>";
        $password = "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-->

This is working code so please use it.

pavan kumar
A: 

'auth' => false, <-- X

'auth' => 'login', <-- V

v1d32
+1  A: 

pavan kumar's answer is correct with one issue:

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

$host = "ssl://smtp.gmail.com";
$port = "465";
$username = "<myaccount.gmail.com>";
$password = "password";

This bit: $username = "<myaccount.gmail.com>"; doesn't need the <> like you do in the $from and $to fields. And won't work if they are there. Hopefully this helps someone.

Idiomatic