在本地运行XAMPP,使用GMAIL邮件服务器在PHP中发送电子邮件

24
我尝试使用php mail()函数从本地主机发送电子邮件到我的雅虎电子邮件帐户,返回结果显示我成功发送了电子邮件,但我没有收到任何电子邮件。我一直在阅读和尝试许多所谓的“简单方法”来发送电子邮件,但结果令人失望,它们都对我不起作用。以下是代码、配置和错误消息。有人能为我解决这个问题吗?谢谢。

PHP代码:

<?php
$to      = 'myemail@yahoo.com';
$subject = 'Fake sendmail test';
$message = 'If we can read this, it means that our fake Sendmail setup works!';
$headers = 'From: myemail@egmail.com' . "\r\n" .
           'Reply-To: myemail@gmail.com' . "\r\n" .
           'X-Mailer: PHP/' . phpversion();

if(mail($to, $subject, $message, $headers)) {
    echo 'Email sent successfully!';
} else {
    die('Failure: Email was not sent!');
}
?>

php.ini配置(我使用gmail邮件服务器)

SMTP =smtp.gmail.com
smtp_port =587
sendmail_from = myemail@gmail.com
sendmail_path = "\"C:\xampp\sendmail\sendmail.exe\" -t"

sendmail.ini配置

smtp_server=smtp.gmail.com
smtp_port=587
smtp_ssl=tls
error_logfile=error.log
debug_logfile=debug.log
auth_username=myemail@gmail.com
auth_password=mypassword
force_sender=myemail@gmail.com

sendmail错误日志中的错误消息,端口为587

13/10/02 13:36:41 : 必须先发出STARTTLS命令。k4sm129639pbd.11 - gsmtp


一个详细的博客:http://goo.gl/O1zw89 - Suresh Kamrushi
5个回答

22

这里是给我答案的链接:

[Install] the "fake sendmail for windows". If you are not using XAMPP you can download it here: http://glob.com.au/sendmail/sendmail.zip

[Modify] the php.ini file to use it (commented out the other lines):

[mail function]
; For Win32 only.
; SMTP = smtp.gmail.com
; smtp_port = 25

; For Win32 only.
; sendmail_from = <e-mail username>@gmail.com

; For Unix only. You may supply arguments as well (default: "sendmail -t -i").
sendmail_path = "C:\xampp\sendmail\sendmail.exe -t"

(由于我们实际上正在使用sendmail,因此忽略“仅适用于Unix”的部分)

You then have to configure the "sendmail.ini" file in the directory where sendmail was installed:

[sendmail]

smtp_server=smtp.gmail.com
smtp_port=25
error_logfile=error.log
debug_logfile=debug.log
auth_username=<username>
auth_password=<password>
force_sender=<e-mail username>@gmail.com

要访问启用了两步验证的Gmail帐户,您需要创建一个特定应用程序密码。(来源


3
我认为Gmail现在需要SSL。 - happyhardik
我已经搜索了两天关于这个配置细节的信息,谢谢你,这篇文章真的帮助我解决了问题 :)。 - David Demetradze
1
为了在本地主机上进行快速测试,auth_username中使用的gmail帐户应该设置允许从https://www.google.com/settings/security/lesssecureapps访问低安全应用程序。这对我有用。 - Roka

4
在php.ini文件中,取消注释这一行。
sendmail_path = "\"C:\xampp\sendmail\sendmail.exe\" -t"
;sendmail_path="C:\xampp\mailtodisk\mailtodisk.exe"

还有在sendmail.ini中

smtp_server=smtp.gmail.com
smtp_port=465
error_logfile=error.log
debug_logfile=debug.log
auth_username=your@gmail.com
auth_password=yourpassword
force_sender=your@gmail.com
hostname=localhost

配置这个..它会工作...对我来说很好用。

谢谢。


它对我起作用了。我只需要将我的Gmail帐户配置为“允许不安全的应用程序:开启”。谢谢。 - Zanoldor

0
最简单的方法是使用PHPMailer和Gmail SMTP。配置如下所示。
require 'PHPMailer/PHPMailerAutoload.php';
$mail = new PHPMailer;

$mail->isSMTP();                            
$mail->Host = 'smtp.gmail.com';            
$mail->SMTPAuth = true;                     
$mail->Username = 'Email Address';          
$mail->Password = 'Email Account Password'; 
$mail->SMTPSecure = 'tls';               
$mail->Port = 587;                  

示例脚本和完整源代码可以在此处找到 - 如何在PHP中从本地主机发送电子邮件


0
[sendmail]

smtp_server=smtp.gmail.com  
smtp_port=25  
error_logfile=error.log  
debug_logfile=debug.log  
auth_username=myemail@gmail.com 
auth_password=gmailpassword  
force_sender=myemail@gmail.com

需要验证邮件的用户名和密码,只有这样才能成功地从本地主机发送邮件。


0

不要忘记为您的Gmail帐户生成第二个密码。您将在代码中使用此新密码。请阅读:

https://support.google.com/accounts/answer/185833

在“如何生成应用程序密码”部分,单击“应用程序密码”,然后在“选择应用程序”下选择“邮件”,选择您的设备并单击“生成”。您的第二个密码将打印在屏幕上。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接