Home > Article > Backend Development > What should I do if I can't receive emails when I send them using the mail function in php?
The solution to the problem of not receiving emails when sending emails using the mail function in PHP: first install the sendmail and sendmail-cf packages; then edit the sendmail.mc configuration file and modify the relevant configuration; finally generate a new configuration file , and just test it in the terminal.
The operating environment of this article: ubuntu 17.04 system, php 7.3, thinkpad t480 computer.
Cause analysis:
The reason why we cannot receive emails may be due to the lack of some relevant configurations.
Let’s take a look at the specific steps.
First of all, we need to install some software:
The sendmail function in ubuntu can easily send emails. Ubuntu sendmail must first install two packages
sudo apt-get install sendmail sudo apt-get install sendmail-cf
Note:
To use the most commonly used mail functions under Ubuntu, you need to install mailutils,
Installation command:
sudo apt-get install mailutils
To use the function with attachments, you also need to install sharutils,
Installation command:
sudo apt-get install sharutils;(yum install sharutils )
Terminal input command:
ps aux |grep sendmail
The output is as follows:
root 20978 0.0 0.3 8300 1940 ? Ss 06:34 0:00 sendmail: MTA: accepting connections root 21711 0.0 0.1 3008 776 pts/0 S+ 06:51 0:00 grep sendmail
Description sendmail has been successfully installed and started
Installed After that, we need to make some related configurations
sendmail will only send emails to local users by default. Only by extending it to the entire Internet can it become a real mail server.
Open the sendmail configuration macro file: /etc/mail/sendmail.mc
vi /etc/mail/sendmail.mc
Find the following line:
DAEMON_OPTIONS(`Family=inet, Name=MTA-v4 , Port=smtp, Addr=127.0.0.1′)dnl
Modify Addr=0.0.0.0 to indicate that you can connect to any server.
Generate a new configuration file:
#cd /etc/mail #mv sendmail.cf sendmail.cf~ //做一个备份 #m4 sendmail.mc > sendmail.cf //>的左右有空格,提示错误没有安装sendmail-cf $ sudo vim /etc/php5/apache2/php.ini
Find the sentence ";sendmail_path", remove the comment ";", and then add the sendmail startup directory
sendmail_path = /usr/ sbin/sendmail -t -i
Theoretically, you can send emails here.
Finally, you can test whether the configuration is successful in the terminal:
The common ways to send emails in the terminal are as follows:
How to write a general email: mail test@126.com Cc Edited copy Send object, Subject: email subject, enter Enter, after the email body, press Ctrl-D to end
Quick sending method: echo "Email body" | mail -s Email subject test@126.com
Send the file content as the email body: mail -s test test@126.com < test.txt
Send an email with an attachment: uuencode attachment name attachment display name | mail - s Email subject sending address
Related recommendations:php video tutorial
The above is the detailed content of What should I do if I can't receive emails when I send them using the mail function in php?. For more information, please follow other related articles on the PHP Chinese website!