2014-12-03 151 views
1

我想从我的Gmail帐户使用PHP(xampp)发送电子邮件。 我搜索了stackoverflow和我得到的东西,我改变了我的xampp的文件sendmail.ini和php.ini作为,php xampp邮件()函数不工作

在C:\ xampp \ php \ php.ini我从行的开头删除了分号extension=php_openssl.dll使SSL适用于localhost的gmail。 \ XAMPP \ sendmail的\ sendmail.ini:

下[邮件功能] php.ini文件

我用C改变

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

然后。我替换sendmail.ini所有现有的代码与下面的代码

[sendmail] 

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

然后我写了一个简单的邮件功能

<?php 
$to = '[email protected]'; 
$subject = 'Test email'; 
$message = "Hello World!\n\nThis is my first mail."; 
$headers = "From: [email protected]\r\nReply-To: [email protected]"; 
$mail_sent = @mail($to, $subject, $message, $headers); 
echo $mail_sent ? "Mail sent" : "Mail failed"; 
?> 

然后通过浏览器中的这个页面上走了, 得到了呼应的邮件失败。

我看到我的调试文件和我

14/12/03 18:24:00 ** --- MESSAGE BEGIN --- 
14/12/03 18:24:00 ** To: [email protected] 
14/12/03 18:24:00 ** Subject: Test email 
14/12/03 18:24:00 ** From:[email protected] 
14/12/03 18:24:00 ** Reply-To:[email protected] 
14/12/03 18:24:00 ** 
14/12/03 18:24:00 ** Hello World! 
14/12/03 18:24:00 ** 
14/12/03 18:24:00 ** This is my first mail. 
14/12/03 18:24:00 ** --- MESSAGE END --- 
14/12/03 18:24:01 ** Connecting to smtp.gmail.com:25 
14/12/03 18:24:08 ** Connected. 
14/12/03 18:24:08 << 220 mx.google.com ESMTP o17sm23150442pdn.33 - gsmtp<EOL> 
14/12/03 18:24:08 >> EHLO Mukul-PC<EOL> 
14/12/03 18:24:10 << 250-mx.google.com at your service, [223.186.182.29]<EOL>250-SIZE 35882577<EOL>250-8BITMIME<EOL>250-STARTTLS<EOL>250-ENHANCEDSTATUSCODES<EOL>250-PIPELINING<EOL>250-CHUNKING<EOL>250 SMTPUTF8<EOL> 
14/12/03 18:24:10 ** Authenticating as [email protected] 
14/12/03 18:24:10 >> STARTTLS<EOL> 
14/12/03 18:24:12 << 220 2.0.0 Ready to start TLS<EOL> 
14/12/03 18:24:14 >> QUIT<EOL> 

lots of codes in unknown language. 

14/12/03 18:21:47 ** Disconnected. 
14/12/03 18:21:47 ** Disconnecting from smtp.gmail.com:25 
14/12/03 18:21:47 ** Disconnected. 
14/12/03 18:21:47 ** Disconnected. 
14/12/03 18:21:47 ** Connection Closed Gracefully. 

我没有得到什么问题。

+0

你重新启动所有服务? – 2014-12-03 13:42:08

+1

您配置端口587,但它连接到端口25.然后尝试通过端口25的TLS失败。 – 2014-12-03 13:43:26

+0

在发布问题时,我错误地在此代码中写了587。对不起。让我更新它..1秒.. – MukulAgr 2014-12-03 13:46:22

回答

3

好的,我得到了这个工作。不幸的是没有使用TLS,因为端口587在我们的网络中被阻塞,所以我不得不使用端口465.但是我希望TLS也能工作。

我用XAMPP 5.6.3。

Sendmail的配置:

[sendmail] 
smtp_server=smtp.gmail.com 
smtp_port=465 
smtp_ssl=auto 
error_logfile=error.log 
debug_logfile=debug.log 
auth_username==*****@gmail.com 
auth_password=********* 
force_sender=*****@gmail.com 

PHP配置:

[mail function] 
SMTP=smtp.gmail.com 
smtp_port=465 
sendmail_from = *****@gmail.com 
sendmail_path = "\"c:\xampp\sendmail\sendmail.exe\" -t" 

代码:

<?php 
$to = '[email protected]'; 
$subject = 'Test email'; 
$message = "Hello World!\n\nThis is my first mail."; 
$headers = "From: *****@gmail.com\r\nReply-To: *****@gmail.com"; 
$mail_sent = @mail($to, $subject, $message, $headers); 
echo $mail_sent ? "Mail sent" : "Mail failed"; 
?> 

重要:

您需要在Gmail中启用SMTP访问,也你需要允许不安全客户:

https://www.google.com/settings/security/lesssecureapps https://support.google.com/mail/answer/75726

要知道,你要使用的SMTP端口是打开防火墙!我建议尝试从您的开发机器与雷鸟连接检查。然后你知道客户端是否能够连接。

这是我的结果:

14/12/03 12:19:32 ** --- MESSAGE BEGIN --- 
14/12/03 12:19:32 ** To: *****@example.com 
14/12/03 12:19:32 ** Subject: Test email 
14/12/03 12:19:32 ** From: ******@gmail.com 
14/12/03 12:19:32 ** Reply-To: ******@gmail.com 
14/12/03 12:19:32 ** 
14/12/03 12:19:32 ** Hello World! 
14/12/03 12:19:32 ** 
14/12/03 12:19:32 ** This is my first mail. 
14/12/03 12:19:32 ** --- MESSAGE END --- 
14/12/03 12:19:34 ** Connecting to smtp.gmail.com:465 
14/12/03 12:19:34 ** Connected. 
14/12/03 12:19:34 << 220 mx.google.com ESMTP pl1sm22468617wic.16 - gsmtp<EOL> 
14/12/03 12:19:34 >> EHLO vm-test-03.devnet.local<EOL> 
14/12/03 12:19:35 << 250-mx.google.com at your service, [xxx.xxx.xxx.xxx]<EOL>250-SIZE 35882577<EOL>250-8BITMIME<EOL>250-AUTH LOGIN PLAIN XOAUTH XOAUTH2 PLAIN-CLIENTTOKEN OAUTHBEARER<EOL>250-ENHANCEDSTATUSCODES<EOL>250-PIPELINING<EOL>250-CHUNKING<EOL>250 SMTPUTF8<EOL> 
14/12/03 12:19:35 ** Authenticating as ****@gmail.com 
14/12/03 12:19:35 >> AUTH LOGIN<EOL> 
14/12/03 12:19:35 << 235 2.7.0 Accepted<EOL> 
14/12/03 12:19:35 >> MAIL FROM: <*****@gmail.com><EOL> 
14/12/03 12:19:35 << 250 2.1.0 OK pl1sm22468617wic.16 - gsmtp<EOL> 
14/12/03 12:19:35 >> RCPT TO: <[email protected]><EOL> 
14/12/03 12:19:35 << 250 2.1.5 OK pl1sm22468617wic.16 - gsmtp<EOL> 
14/12/03 12:19:35 >> DATA<EOL> 
14/12/03 12:19:35 << 354 Go ahead pl1sm22468617wic.16 - gsmtp<EOL> 
14/12/03 12:19:35 >> Date: Wed, 3 Dec 2014 12:19:35 -0800<EOL> 
14/12/03 12:19:35 >> To: [email protected]<EOL> 
14/12/03 12:19:35 >> Subject: Test email<EOL> 
14/12/03 12:19:35 >> From: *****@gmail.com<EOL> 
14/12/03 12:19:35 >> Reply-To: *****@gmail.com<EOL> 
14/12/03 12:19:35 >> <EOL> 
14/12/03 12:19:35 >> Hello World!<EOL> 
14/12/03 12:19:35 >> <EOL> 
14/12/03 12:19:35 >> This is my first mail.<EOL> 
14/12/03 12:19:35 >> .<EOL> 
14/12/03 12:19:36 << 250 2.0.0 OK 1417637977 pl1sm22468617wic.16 - gsmtp<EOL> 
14/12/03 12:19:36 >> QUIT<EOL> 
14/12/03 12:19:36 << 221 2.0.0 closing connection pl1sm22468617wic.16 - gsmtp<EOL> 
14/12/03 12:19:36 ** Disconnecting from smtp.gmail.com:465 
14/12/03 12:19:36 ** Disconnected. 
14/12/03 12:19:36 ** Disconnected. 
+0

我使用v3.2.1 – MukulAgr 2014-12-03 15:15:45

+1

而你的配置。不工作:( – MukulAgr 2014-12-03 15:17:21

+0

我更新了我的答案,我在XAMPP自己的测试环境中测试了这个...只是FYI V3.2.1是XAMPP控制台的版本,而不是XAMPP本身:) – LucasF 2014-12-03 20:39:46