Home > Backend Development > PHP Tutorial > Why is my PHP email sending to Gmail failing authentication, and how can I fix it?

Why is my PHP email sending to Gmail failing authentication, and how can I fix it?

Mary-Kate Olsen
Release: 2025-01-04 10:51:33
Original
313 people have browsed it

Why is my PHP email sending to Gmail failing authentication, and how can I fix it?

Fix Authentication Failure When Sending Email via GMail SMTP Server from PHP

You may encounter the following authentication error while attempting to send an email using your PHP script:

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)]
Copy after login

This error generally occurs when the specified SMTP configuration is incorrect or incomplete. To resolve this issue, verify your configuration and adjust it to the following:

require_once "Mail.php";

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

$host = "ssl://smtp.gmail.com";
$port = "465";
$username = "[email&#160;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>");
 }
Copy after login

By specifying ssl:// in the host configuration, you establish a secure SSL connection to the GMail SMTP server. Additionally, you must specify the correct port for SSL, which is 465.

Make sure your username and password are correct. These should be your GMail credentials.

Once you have adjusted your configuration, your PHP script should be able to send emails through the GMail SMTP server without encountering the authentication failure error.

The above is the detailed content of Why is my PHP email sending to Gmail failing authentication, and how can I fix it?. For more information, please follow other related articles on the PHP Chinese website!

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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template