Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
881 views
in Technique[技术] by (71.8m points)

apache - PHP xampp mail() function not working

I am trying to send emails from my gmail account using php (xampp). I search over stackoverflow and as I got the things I changed my xampp's files sendmail.ini and php.ini as,

In C:xamppphpphp.ini I removed the semicolon from the beginning of the line extension=php_openssl.dll to make SSL working for gmail for localhost.

in php.ini file under [mail function] I changed

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

Then in C:xamppsendmailsendmail.ini. I replaced all the existing code in sendmail.ini with following code

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

then I wrote a simple mail function

<?php
$to = '[email protected]';
$subject = 'Test email'; 
$message = "Hello World!

This is my first mail."; 
$headers = "From: [email protected]
Reply-To: [email protected]";
$mail_sent = @mail( $to, $subject, $message, $headers );
echo $mail_sent ? "Mail sent" : "Mail failed";
?>

then went on the this page by my browser, got echo as Mail Failed.

I saw my debug file and I got

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.

I am not getting whats the problem.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Ok, I got this working. Unfortunately not with TLS, because Port 587 is blocked in our network so I had to use Port 465. But I expect that TLS will also work.

I used XAMPP 5.6.3.

Sendmail config:

[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 config:

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

Code:

<?php
$to = '[email protected]';
$subject = 'Test email'; 
$message = "Hello World!

This is my first mail."; 
$headers = "From: *****@gmail.com
Reply-To: *****@gmail.com";
$mail_sent = @mail( $to, $subject, $message, $headers );
echo $mail_sent ? "Mail sent" : "Mail failed";
?>

Important:

You will need to enable SMTP access in GMAIL, also you need to allow unsecure clients:

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

Be aware that the smtp port you want to use is open in your firewall! I suggest to try a connection check from your dev machine with thunderbird. Then you know if the client is able to connect.

This is my result:

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.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...