SMTP connect() failed PHPmailer - PHP
This issue arises when you encounter an error while attempting to send an email using PHPmailer, specifically "Mailer Error: SMTP connect() failed." The underlying cause is often related to authentication settings and compatibility with your email provider.
In this instance, the solution involves enabling less secure apps for your Google account. Google recently implemented XOAUTH2 authentication, which requires you to explicitly allow access to third-party applications.
To resolve this:
Additionally, ensure that you are using the correct SMTP settings:
Here's an updated code sample with these settings:
<code class="php">require_once 'C:\xampp\htdocs\email\vendor\autoload.php'; define ('GUSER','[email protected]'); define ('GPWD','your password'); function smtpmailer($to, $from, $from_name, $subject, $body) { global $error; $mail = new PHPMailer(); // create a new object $mail->IsSMTP(); // enable SMTP $mail->SMTPDebug = 2; // debugging: 1 = errors and messages, 2 = messages only $mail->SMTPAuth = true; // authentication enabled $mail->SMTPSecure = 'tls'; // secure transfer enabled REQUIRED for GMail $mail->SMTPAutoTLS = false; $mail->Host = 'smtp.gmail.com'; $mail->Port = 587; $mail->Username = GUSER; $mail->Password = GPWD; $mail->SetFrom($from, $from_name); $mail->Subject = $subject; $mail->Body = $body; $mail->AddAddress($to); if(!$mail->Send()) { $error = 'Mail error: '.$mail->ErrorInfo; return false; } else { $error = 'Message sent!'; return true; } }</code>
By enabling less secure apps and using the correct SMTP settings, you should be able to successfully send emails using PHPmailer with Gmail's SMTP server.
The above is the detailed content of Why is my PHPmailer SMTP connect() failing with Gmail and how can I fix it?. For more information, please follow other related articles on the PHP Chinese website!