如何在ubuntu中安装配置sendmail,以实现发邮件发送(还可在php中发送,也可发匿名邮件)

一.安装sendmail

sudo apt install sendmail

二.修改/etc/hosts文件,添加localhost.localdomain项(全限定域名)

sudo vim /etc/hosts

修改后如下:

127.0.0.1 localhost.localdomain localhost lenix-Swift-SF314-512
127.0.1.1 lenix-Swift-SF314-512

三.修改sendmail.mc配置

cd /etc/mail
sudo vim sendmail.mc

找到以下2行:

DAEMON_OPTIONS(`Family=inet, Name=MTA-v4, Port=smtp, Addr=127.0.0.1')dnl
DAEMON_OPTIONS(`Family=inet, Name=MSP-v4, Port=submission, M=Ea, Addr=127.0.0.1')dnl

在前面加dnl代表注释掉这2行,以使能够进行外网访问 .

修改后如下:
dnl DAEMON_OPTIONS(`Family=inet, Name=MTA-v4, Port=smtp, Addr=127.0.0.1')dnl
dnl DAEMON_OPTIONS(`Family=inet, Name=MSP-v4, Port=submission, M=Ea, Addr=127.0.0.1')dnl

 

sendmail.mc最后添加3行:其中lenixhost,可以改为你想要的.
define(`SMART_HOST',`lenixhost')dnl
define(`RELAY_MAILER', `esmtp')dnl
define(`RELAY_MAILER_ARGS', `TCP $h 587')dnl

四.修改local-host-names配置.

vim local-host-names
加一行
lenixhost

此和上一步define(`SMART_HOST',`lenixhost')dnl中加的名称一定要一样.

 五.更新配置

cd /etc/mail
make

六.重启sendmail

sudo /etc/init.d/sendmail restart

七.修改PHP配置(可选:PHP发邮件需要配置)

sudo vim /etc/php/8.1/cli/php.ini

sudo vim /etc/php/8.1/fpm/php.ini

修改sendmail_path为:
sendmail_path= /usr/sbin/sendmail -t -i
重启PHP:
sudo service apache2 restart 或  sudo service php8.1-fpm restart

八.测试

终端发邮件测试 :需要安装mailutils.
echo helloworld | mail -s test xxxxxxxxx@qq.com

 

PHP发邮件测试:
php -a
mail('someonexxxxx@qq.com', 'My Subject', 'ssssssssss');

以上在ubuntu 18.04,ubuntu22.04中通过 .

参考:

How to Install and Setup Sendmail on Ubuntu

https://linuxconfig.org/sendmail-unqualified-hostname-unknown-sleeping-for-retry-unqualified-hostname

如何在ubuntu中安装配置sendmail,以实现发邮件发送(还可在php中发送,也可发匿名邮件)
标签: