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

我尝试使用PHP邮件()函数发送一个电子邮件从本地主机到我的雅虎电子邮件帐户,回报说我成功地发送电子邮件,但我没有得到任何电子邮件。 我一直在阅读和尝试所谓的“简单的方式”发送电子邮件,但结果令人失望,没有一个为我工作。 以下是代码,configuration和错误消息。 有人可以用这个来启发我吗? 谢谢。

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为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的configuration

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 =inputmypassword
force_sender=myemail@gmail.com

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

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

这是给我答案的链接:

[安装]“ 虚假的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的目录中configuration“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 

要访问受双因子validation保护的Gmail帐户,您需要创build特定于应用程序的密码 。 ( 来源 )

在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 

configuration这一个..它会工作…它为我工作得很好。

谢谢。

 require_once "Mail.php"; $from = '<from.gmail.com>'; $to = '<to.yahoo.com>'; $subject = 'Hi!'; $body = "Hi,\n\nHow are you?"; $headers = array( 'From' => $from, 'To' => $to, 'Subject' => $subject ); $smtp = Mail::factory('smtp', array( 'host' => 'ssl://smtp.gmail.com', 'port' => '465', 'auth' => true, 'username' => 'johndoe@gmail.com', 'password' => 'passwordxxx' )); $mail = $smtp->send($to, $headers, $body); if (PEAR::isError($mail)) { echo('<p>' . $mail->getMessage() . '</p>'); } else { echo('<p>Message successfully sent!</p>'); } 
 [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 

需要validation邮件的用户名和密码,然后只能从本地主机成功发送邮件

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

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

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

最简单的方法是使用PHPMailer和Gmail SMTP。 configuration将如下所示。

 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; 

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

检查您的垃圾邮件。 mail()函数有时会将邮件发送给垃圾邮件。

尝试这个。 它总是为我工作。

  $config['protocol'] = 'smtp'; $config['smtp_host'] = 'ssl://smtp.gmail.com'; $config['smtp_port'] = '465'; //ssl $config['smtp_timeout'] = '7'; $config['smtp_user'] = 'tandelromal09@gmail.com'; $config['smtp_pass'] = 'pentium409'; $config['charset'] = 'utf-8'; $config['newline'] = "\r\n"; $config['mailtype'] = 'html'; $config['validation'] = TRUE;