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)]
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 protected]>"; $to = "Ramona Recipient <[email protected]>"; $subject = "Hi!"; $body = "Hi,\n\nHow are you?"; $host = "ssl://smtp.gmail.com"; $port = "465"; $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>"); }
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!