Why PHP cannot send mail using the Mail function

Source: Internet
Author: User
Reading guidance PHP requires an installed and running messaging system so that mail function mail () is available. The mail system program used is set up in the php.ini file.

Sending a message with the following code can always send a failure.


[php] View plaincopyprint?
!--? php . $to = "xxxxxxxx@qq.com";
$subject = "Test mail";
$message = "hello! A simple email message. ";
05.mail ($to, $subject, $message);
06.?>
, PHP needs an installed and running messaging system to make mail function mail () available. The mail system program used is set up in the php.ini file. The workaround is as follows:
1. Install sendmail
sudo apt-get install sendmail
sudo apt-get install sendmail-cf
sudo apt-get install Mailutils
2. Configure SendMail
To open/ETC/MAIL/SENDMAIL.MC, locate the following line:
Daemon_options (' Family=inet, NAME=MTA-V4, port= SMTP, addr=127.0.0.1 ') dnl
Change 127.0.0.1 to 0.0.0.0
perform the following command to back up the old configuration file and generate a new profile:
Cd/etc/mail
MV SENDMAIL.CF sendmail.cf~
M4 SENDMAIL.MC > SENDMAIL.CF
3. Test
Echo "This is a test mail." | mail-s "Test" xxxxxxxx@qq.com
Note: This message will be accepted as spam, and if you use 163 mailboxes you may be filtered out of the box to receive it.
4. Configure php.ini file
to open/etc/php5/apache2/php.ini, locate the following line:
; sendmail_path =
Change the line to: Sendmail_path =/usr/sbin/ Sendmail-t

In particular, it is important to note that the "-T" parameter is necessary, and if it is missing, the mail () function fails when the original PHP file is executed through the browser, but there is no problem if the original PHP file is executed directly on the server side using the PHP command line.

5. Restart Apache
Sudo/etc/init.d/apache2 restart


The original PHP code is executed again, and the message is sent successfully.


The above describes the reason that PHP cannot send mail with the email function, including the content of the contents, I hope that the PHP tutorial interested in a friend helpful.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.