简体   繁体   English

使用GMAIL邮件服务器从PHP运行XAMMP的localhost发送电子邮件

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

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. 我尝试使用php mail()函数从localhost发送电子邮件到我的雅虎电子邮件帐户,返回说我成功发送了电子邮件,但我没有收到任何电子邮件。 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 code 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!');
}
?>

Configuration for php.ini (I'm using gmail mail server) 配置php.ini(我正在使用gmail邮件服务器)

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

Configuration for sendmail.ini sendmail.ini的配置

smtp_server=smtp.gmail.com smtp_server = smtp.gmail.com
smtp_port=587 SMTP_PORT = 587
smtp_ssl=tls smtp_ssl = TLS
error_logfile=error.log error_logfile = error.log中
debug_logfile=debug.log debug_logfile =的debug.log
auth_username=myemail@gmail.com auth_username=myemail@gmail.com
auth_password=mypassword AUTH_PASSWORD =输入mypassword
force_sender=myemail@gmail.com force_sender=myemail@gmail.com

error message in sendmail error log with port 587 sendmail错误日志中的错误消息,端口为587

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

Here's the link that gives me the answer: 是给我答案的链接:

[Install] the " fake sendmail for windows ". [安装]“ 假的sendmail for Windows ”。 If you are not using XAMPP you can download it here: http://glob.com.au/sendmail/sendmail.zip 如果您不使用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的目录中配置“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 . 要访问受双因素验证保护的Gmail帐户,您需要创建特定应用程序的密码 ( source ) 来源

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
auth_username=your@gmail.com
auth_password=yourpassword
force_sender=your@gmail.com
hostname=localhost

configure this one..it will works...it working fine for me. 配置这个...它会工作......它对我来说很好。

thanks. 谢谢。

[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

need authenticate username and password of mail then only once can successfully send mail from localhost 需要验证邮件的用户名和密码,然后才能成功从localhost发送邮件

Don't forget to generate a second password for your Gmail account. 不要忘记为您的Gmail帐户生成第二个密码。 You will use this new password in your code. 您将在代码中使用此新密码。 Read this: 读这个:

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. 您的第二个密码将打印在屏幕上。

Simplest way is to use PHPMailer and Gmail SMTP. 最简单的方法是使用PHPMailer和Gmail SMTP。 The configuration would be like the below. 配置如下所示。

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中的Localhost发送电子邮件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM