Why can't I use simple mail() function in PHP to send emails to my Gmail account from my PC?
I have an XAMPP installed on my PC, and I wanted to send an email to my Gmail account, however, it's not working. Here is the code:
<?php
$to = "yjhjerry621@gmail.com";
$subject = "Test mail";
$message = "Hello! This is a simple email message.";
$from = "yjhjerry621@qq.com";
$headers = "From: $from";
mail($to,$subject,$message,$headers);
echo "Mail Sent.";
?>
Here is my setting in php.ini:
[mail function]
; For Win32 only.
; http://php.net/smtp
SMTP = smtp.qq.com
; http://php.net/smtp-port
smtp_port = 25
sendmail_from = yjhjerry621@qq.com
Here is what I've got from the 开发者_如何学运维browser:
Warning: mail() [function.mail]: Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set() in C:\xampp\htdocs\phpemail.php on line 7
Is your php.ini pointing to a valid SMTP server? Check for this section in php.ini:
[mail function]
; For Win32 only.
SMTP = localhost
smtp_port = 25
and confirm that valid values are set. Does your PHP error log contain any useful errors after attempting to run the script?
XAMPP should of installed some applications for you. One of them is the Mercury Mail. In there you can go and configure your SMTP server.
Basically what happens is that when you are trying to send an email using PHP's mail function, in order to do it, it needs to send it using SMTP, so if you have not set the up correctly, the function will not be able to establish a connection and therefore will not be able to send the mail.
精彩评论