Home >Backend Development >PHP Tutorial >Why Am I Getting an Authentication Failure When Sending Emails via Gmail's SMTP Server from PHP?

Why Am I Getting an Authentication Failure When Sending Emails via Gmail's SMTP Server from PHP?

Patricia Arquette
Patricia ArquetteOriginal
2024-12-20 00:23:08405browse

Why Am I Getting an Authentication Failure When Sending Emails via Gmail's SMTP Server from PHP?

Unable to Send Email via GMail's SMTP Server?

When attempting to send emails through GMail's SMTP server from a PHP page, you may encounter the 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)]

This error often arises when the PHP code lacks the correct SSL settings. Here's how to rectify the issue:

Corrected PHP Code:

<br>// Pear Mail Library<br>require_once "Mail.php";</p>
<p>$from = '<[email protected]>';<br>$to = '<[email protected]>';<br>$subject = 'Hi!';<br>$body = "Hi,nnHow are you?";</p>
<p>$headers = array(</p>
<pre class="brush:php;toolbar:false">'From' => $from,
'To' => $to,
'Subject' => $subject

);

$smtp = Mail::factory('smtp', array(

    'host' => 'ssl://smtp.gmail.com',
    'port' => '465',
    'auth' => true,
    'username' => '[email&#160;protected]',
    'password' => 'passwordxxx'
));

$mail = $smtp->send($to, $headers, $body);

if (PEAR::isError($mail)) {

echo('<p>' . $mail->getMessage() . '</p>');

} else {

echo('<p>Message successfully sent!</p>');

}

Key Differences:

  • The host now includes ssl:// indicating a secure connection.
  • The port is changed to 465, which is the standard SSL port.

By implementing these changes, you'll ensure proper SSL encryption and avoid the authentication failure error.

The above is the detailed content of Why Am I Getting an Authentication Failure When Sending Emails via Gmail's SMTP Server from PHP?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn