Home  >  Article  >  Backend Development  >  [PHP] The mail() function fails when sending emails (sendmail command and postfix)

[PHP] The mail() function fails when sending emails (sendmail command and postfix)

little bottle
little bottleforward
2019-04-17 14:29:382656browse

1. PHP’s mail() function can send emails, but it has never been successfully tested. Now let’s summarize the reasons:

PHP’s mail function is based on the sendmail command, which needs to be installed. postfix software

2. Check the php configuration item sendmail_path. This is the underlying command called by the mail function. php -i|grep sendmail

3. Install Only postfix software can use the sendmail command, apt-get install postfix

After the software installation is completed, test whether it can be sent successfully on the command line

Create an eml text file, the content is the most The main email protocol headers


Subject: title-testing
From: shihan2@sopans.com
To: 630892807@qq.com
Sorry, I'm testing. ~_~


 /usr/sbin/sendmail -t -i -f shihan2@sopans.com < 2.eml

-f parameters are followed by the from value. If the correspondence does not match, it will If an error is reported, you can check the log file /var/log/mail.log to see the cause of the error and whether it was successful.

If the from value does not correspond, the information prompted in the log

The corresponding information after successful sending, 250ok

4. Modify the php configuration file and use the php function to send Letter

I modified this configuration file, /etc/php/7.0/cli/php.ini , and the from parameter was written to death. During the test, if I did not write this from email address, the sending kept failing, prompting 501 Syntax: MAIL FROM: 208700f394e4cf40a7aa505373e0130b (in reply to MAIL FROM command)) , the mail from and the letter from do not correspond to each other

Check the configuration

Create a php file


<?php
$to=&#39;630892807@qq.com&#39;;
$subject = &#39;测试一下&#39;;
$message = &#39;我来测试&#39;;
$headers[] = &#39;From: shihan2@sopans.com&#39;;
mail($to, $subject, $message, implode("\r\n", $headers));

Test successful

Related tutorials: A complete set of video tutorials on PHP programming from entry to master

The above is the detailed content of [PHP] The mail() function fails when sending emails (sendmail command and postfix). For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:cnblogs.com. If there is any infringement, please contact admin@php.cn delete