What should I do if I can't receive emails when I send them using the mail function in php?

王林
Release: 2023-03-10 13:48:02
Original
2665 people have browsed it

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.

What should I do if I can't receive emails when I send them using the mail function in php?

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
Copy after login

Note:

To use the most commonly used mail functions under Ubuntu, you need to install mailutils,

Installation command:

sudo apt-get install mailutils
Copy after login

To use the function with attachments, you also need to install sharutils,

Installation command:

sudo apt-get install sharutils;(yum install sharutils )
Copy after login

Terminal input command:

ps aux |grep sendmail
Copy after login

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
Copy after login

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
Copy after login

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
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template