【问题标题】:Send email from localhost running XAMMP in PHP using GMAIL mail server使用 GMAIL 邮件服务器从在 PHP 中运行 XAMPP 的本地主机发送电子邮件
【发布时间】:2013-10-08 13:21:20
【问题描述】:

我尝试使用 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=我的密码
force_sender=myemail@gmail.com

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

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

【问题讨论】:

标签: php email gmail sendmail.exe


【解决方案1】:

Here's给我答案的链接:

[安装]“fake sendmail for windows”。如果你没有使用 XAMPP,你可以在这里下载: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"

(忽略“仅 Unix”位,因为我们实际上使用的是 sendmail)

然后您必须在安装 sendmail 的目录中配置“sendmail.ini”文件:

[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 帐户,您需要创建一个 application-specific password。 (source)

【讨论】:

  • 我认为gmail现在需要ssl
  • 我一直在寻找这个配置细节大约 2 天,谢谢,这篇文章真的帮助我解决了问题:)。
  • 为了在 localhost 中进行快速测试,auth_username 中使用的 gmail 帐户应设置为允许从此处google.com/settings/security/lesssecureapps 访问低安全性应用程序。这对我有用。
【解决方案2】:

在 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 帐户配置为“允许不太安全的应用程序:打开”。谢谢。
【解决方案3】:
[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

需要验证邮件的用户名和密码,然后才能从本地成功发送邮件一次

【讨论】:

    【解决方案4】:

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

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

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

    【讨论】:

      【解决方案5】:

      最简单的方法是使用 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;                  
      

      示例脚本和完整源代码可以从这里找到 - How to Send Email from Localhost in PHP

      【讨论】:

        猜你喜欢
        • 2013-12-18
        • 2015-05-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-08-29
        • 2021-04-19
        • 2011-03-05
        相关资源
        最近更新 更多