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
Release: 2024-12-20 00:23:08
Original
365 people have browsed it

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>
<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">'From' => $from,
'To' => $to,
'Subject' => $subject
Copy after login

);

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

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

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

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

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

} else {

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

}

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template