2013-10-02 23 views
19

我尝试使用php mail()函数发送本地主机到我的yahoo电子邮件帐户的电子邮件,返回说我成功发送电子邮件,但没有收到任何电子邮件。我一直在阅读和尝试许多所谓的“简单方式”来发送电子邮件,但结果令人失望,没有一个适合我。以下是代码,配置和错误消息。有人可以用这个来启发我吗?谢谢。使用GMAIL邮件服务器在PHP中运行使用XAMMP的本地主机发送电子邮件

PHP代码

<?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!'); 
} 
?> 

配置为php.ini文件(我使用Gmail的邮件服务器)

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

@ gmail.com
sendmail_path =“\”

配置为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
[email protected]

在sendmail的错误日志

错误消息端口587

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

+0

一个细节博客:http://goo.gl/O1zw89 –

回答

19

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.ini” 文件在已安装的sendmail:

[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帐户prote通过双因素验证,您需要创建一个application-specific password。 (source

+0

谨此确认,这个伟大工程(看到它没有被选中作为一个答案)。 – Foxhoundn

+3

我觉得gmail现在需要ssl – happyhardik

+0

我一直在寻找2天左右的配置信息,谢谢,这篇文章真的帮我解决了问题:)。 – DDeme

0
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' => '[email protected]', 
     'password' => 'passwordxxx' 
    )); 

$mail = $smtp->send($to, $headers, $body); 

if (PEAR::isError($mail)) { 
    echo('<p>' . $mail->getMessage() . '</p>'); 
} else { 
    echo('<p>Message successfully sent!</p>'); 
} 
+2

您可以从这里下载Mail.php http://pear.php.net/package/Mail/download/它的工作正常 – 2013-11-21 14:47:54

3

in 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 
[email protected] 
auth_password=yourpassword 
[email protected] 
hostname=localhost 

配置此one..it将作品......它的工作对我罚款。

谢谢。

+0

正确的一个..谢谢很多.. – Jana

+0

它为我工作。我只需将我的Gmail帐户配置为“允许安全性较低的应用程序:开启”即可。谢谢。 – Zanoldor

0
[sendmail] 

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

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

0

别忘了为您的Gmail帐户第二密码发送邮件。 您将在您的代码中使用此新密码。阅读:

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

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

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;     

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

0

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

+0

是的,您需要将服务器的IP添加到DNS – Skatox

0

请尝试此操作。它为我工作总是。

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