You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Laravel发送邮件报错:无法连接smtp.gmail.com #0,疑XAMPP问题求解决

Hey there, let’s work through this SMTP connection issue with XAMPP and Gmail together. I’ve debugged this exact problem plenty of times, so here are the most likely fixes to try:

1. Fix Gmail’s Security Settings First

Gmail’s strict security rules are usually the culprit here. Depending on your account setup:

  • If you don’t have 2FA enabled: Head to your Google Account > Security, and temporarily enable "Less secure app access" (note: this isn’t ideal long-term, but it’s a quick test).
  • If you do have 2FA enabled (which you should!): Generate an App Password specifically for your XAMPP setup. Go to Google Account > Security > App passwords, select "Mail" as the app and "Other (custom name)" as the device, then use that 16-character generated password instead of your regular Gmail password.
2. Verify sendmail.ini and php.ini Configurations

XAMPP relies on sendmail for outgoing emails, so let’s make sure these config files are set correctly:

sendmail.ini (found in xampp\sendmail\)

Update these key lines:

smtp_server=smtp.gmail.com
smtp_port=587  # Use 465 if you prefer SSL instead of TLS
smtp_ssl=tls   # Swap to "ssl" if using port 465
auth_username=your-full-gmail@gmail.com
auth_password=your-app-password-or-less-secure-password
force_sender=your-full-gmail@gmail.com

php.ini (found in xampp\php\)

Locate the [mail function] section and update it to:

sendmail_path = "\"C:\xampp\sendmail\sendmail.exe\" -t"
; Make sure to comment out any other lines like SMTP, smtp_port, or mail.add_x_header here

After editing both files, restart your XAMPP Apache server—changes won’t take effect until you do this.

3. Check for Firewall/Antivirus Blocking

Local firewalls or antivirus programs often block outgoing SMTP connections. Try temporarily disabling them (just for testing!) to see if the connection goes through. If it does, add an exception for sendmail.exe or allow traffic on ports 465/587 in your security software.

4. Enable PHP’s OpenSSL Extension

Gmail requires SSL/TLS, so ensure this extension is turned on in php.ini:

  • Open xampp\php\php.ini
  • Find the line ;extension=openssl
  • Remove the semicolon to uncomment it: extension=openssl
  • Restart Apache to apply this change.
5. Test with a Minimal PHP Script

Isolate the issue with a simple test file named test_mail.php in your XAMPP htdocs folder:

<?php
$to = "your-test-email@example.com";
$subject = "XAMPP SMTP Test";
$message = "If you get this, your SMTP setup is working!";
$headers = "From: your-gmail@gmail.com\r\n";
$headers .= "Content-type: text/plain; charset=utf-8\r\n";

if(mail($to, $subject, $message, $headers)){
    echo "Email sent successfully!";
} else {
    echo "Failed to send email—check logs for details.";
}
?>

Run it in your browser at http://localhost/test_mail.php. If it fails, check the error logs: xampp\apache\logs\error.log or xampp\sendmail\sendmail.log will give you more clues.

6. Rule Out ISP Port Blocking

Some ISPs block ports 465 or 587 to combat spam. Try switching to port 25, or contact your ISP to confirm if they restrict these ports. As a workaround, you could test with another SMTP server (like Outlook or Yahoo) to see if the issue is specific to Gmail.

内容的提问来源于stack exchange,提问作者user1470098

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 04:12:33