使用 GMAIL 邮件服务器从在 PHP 中运行 XAMMP 的本地主机发送电子邮件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19132171/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 18:53:18  来源:igfitidea点击:

Send email from localhost running XAMMP in PHP using GMAIL mail server

phpemailgmailsendmail.exe

提问by ani

I try to send an email from localhost to my yahoo email account using php mail() function, the return says I successfully send the email but I did not get any email. I've been reading and trying many so called 'simple way' to send email but the result are disappointing, none of them work for me. Below are the code, the configurations and the error message. Can someone enlighten me with this? Thanks.

我尝试使用 php mail() 函数从本地主机向我的雅虎电子邮件帐户发送电子邮件,返回说我成功发送了电子邮件,但我没有收到任何电子邮件。我一直在阅读并尝试许多所谓的“简单方式”发送电子邮件,但结果令人失望,没有一个对我有用。下面是代码、配置和错误信息。有人可以用这个启发我吗?谢谢。

php code

代码

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

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

Configuration for php.ini (I'm using gmail mail server)

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

SMTP =smtp.gmail.com
smtp_port =587
sendmail_from = [email protected]
sendmail_path = "\"C:\xampp\sendmail\sendmail.exe\" -t"

SMTP =smtp.gmail.com
smtp_port =587
sendmail_from = [email protected]
sendmail_path = "\"C:\xampp\sendmail\sendmail.exe\" -t"

Configuration for sendmail.ini

sendmail.ini 的配置

smtp_server=smtp.gmail.com
smtp_port=587
smtp_ssl=tls
error_logfile=error.log
debug_logfile=debug.log
[email protected]
auth_password=mypassword
[email protected]

smtp_server=smtp.gmail.com
smtp_port=587
smtp_ssl=tls
error_logfile=error.log
debug_logfile=debug.log
[email protected]
auth_password=mypassword
[email protected]

error message in sendmail error log with port 587

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

13/10/02 13:36:41 : Must issue a STARTTLS command first. k4sm129639pbd.11 - gsmtp

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

采纳答案by ani

Here'sthe link that gives me the answer:

是给我答案的链接:

[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"

[安装]“ 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"

(ignore the "Unix only" bit, since we actually are using sendmail)

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

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

然后,您必须在安装 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

To access a Gmail account protected by 2-factor verification, you will need to create an application-specific password. (source)

要访问受 2 因素验证保护的 Gmail 帐户,您需要创建应用程序专用密码。(来源

回答by Rambabu

in php.ini file,uncomment this one

在 php.ini 文件中,取消注释这个

sendmail_path = "\"C:\xampp\sendmail\sendmail.exe\" -t"
;sendmail_path="C:\xampp\mailtodisk\mailtodisk.exe"

and in sendmail.ini

并在 sendmail.ini

smtp_server=smtp.gmail.com
smtp_port=465
error_logfile=error.log
debug_logfile=debug.log
[email protected]
auth_password=yourpassword
[email protected]
hostname=localhost

configure this one..it will works...it working fine for me.

配置这个......它会起作用......它对我来说工作正常。

thanks.

谢谢。

回答by poonam

[sendmail]

smtp_server=smtp.gmail.com  
smtp_port=25  
error_logfile=error.log  
debug_logfile=debug.log  
[email protected] 
auth_password=gmailpassword  
[email protected]

need authenticate username and password of mail then only once can successfully send mail from localhost

需要验证邮件的用户名和密码,然后只有一次可以从本地主机成功发送邮件

回答by Vadzym

Don't forget to generate a second password for your Gmail account. You will use this new password in your code. Read this:

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

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

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

Under the section "How to generate an App password" click on "App passwords", then under "Select app" choose "Mail", select your device and click "Generate". Your second password will be printed on the screen.

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

回答by JoyGuru

Simplest way is to use PHPMailer and Gmail SMTP. The configuration would be like the below.

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

Example script and full source code can be found from here - How to Send Email from Localhost in PHP

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