ubuntu安装使用sendmail

背景: ubuntu中安装sendmail发送邮件;

1.环境配置

sudo apt install sendmail
sudo apt install sendmail-cf
sudo apt-get install mailutils

2.检查是否安装成功:

ps aux | grep sendmail
root     1403610  0.0  0.2  22908  4504 ?        Ss   00:45   0:00 sendmail: MTA: accepting connections
root     1405103  0.0  0.0   9032   740 pts/0    S+   00:47   0:00 grep --color=auto sendmail

3.默认情况下,sendmail只会为本机发送邮件,如需发送到internet,需改动 /etc/mail/sendmail.mc:

sudo vim /etc/mail/sendmail.mc

修改smtp的127.0.0.1为0.0.0.0:

FEATURE(`no_default_msa')dnl
dnl DAEMON_OPTIONS(`Family=inet6, Name=MTA-v6, Port=smtp, Addr=::1')dnl
DAEMON_OPTIONS(`Family=inet,  Name=MTA-v4, Port=smtp, Addr=127.0.0.1')dnl
dnl DAEMON_OPTIONS(`Family=inet6, Name=MSP-v6, Port=submission, M=Ea, Addr=::1')dnl
DAEMON_OPTIONS(`Family=inet,  Name=MSP-v4, Port=submission, M=Ea, Addr=127.0.0.1')dnl

为:

FEATURE(`no_default_msa')dnl
dnl DAEMON_OPTIONS(`Family=inet6, Name=MTA-v6, Port=smtp, Addr=::1')dnl
DAEMON_OPTIONS(`Family=inet,  Name=MTA-v4, Port=smtp, Addr=0.0.0.0')dnl
dnl DAEMON_OPTIONS(`Family=inet6, Name=MSP-v6, Port=submission, M=Ea, Addr=::1')dnl
DAEMON_OPTIONS(`Family=inet,  Name=MSP-v4, Port=submission, M=Ea, Addr=127.0.0.1')dnl

 保存并使之生效:

m4 /etc/mail/sendmail.mc > /etc/mail/sendmail.cf

4.发送方式:

1.如何写一般的邮件: mail test@qq.com  Cc 编辑抄送对象,Subject:邮件主题,输入回车,邮件正文后,按Ctrl-D结束
2.快速发送方式: echo “邮件正文” | mail -s "邮件主题" test@126.com
3.以文件内容作为邮件正文来发送: mail -s test test@126.com < test.txt
4.发送带附件的邮件: uuencode 附件名称 附件显示名称 | mail -s 邮件主题 发送地址

5.日志位置:

tail -f /var/log/mail.log

6.sendmail使用的端口为25端口,如未开通防火墙,需打开防火墙和服务云的安全组规则

使用 telnet 外网ip port 查看是否可通;

放开防火墙(如: iptables)

iptables -A INPUT -p tcp -m state --state NEW -m tcp --dport 25 -j ACCEPT
iptables -L -n

7.可能出现的错误

发送邮件提示: "unable to qualify my own domain name (ecs-df00) -- using short name":

Oct 31 01:21:01 ecs-df00 sm-msp-queue[1406209]: unable to qualify my own domain name (ecs-df00) -- using short name

解决方法:  vim /etc/hosts 修改对应服务名为".com"结尾即可

127.0.0.1       localhost
  
# The following lines are desirable for IPv6 capable hosts
::1     localhost       ip6-localhost   ip6-loopback
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
127.0.1.1       localhost.vm    localhost
127.0.1.1       ecs-df00.com    ecs-df00

到自己邮箱中查看即可发现已收到邮件;

 同时可看到日志:

(base) root@ecs-df00:/var/log# tail -f mail.log
Nov  3 02:11:01 ecs-df00 sendmail[1454106]: 2A2IB1eT1454106: from=root@ecs-df00.com, size=92, class=0, nrcpts=1, msgid=<202211021811.2A2IB1eT1454106@ecs-df00.com>, relay=root@localhost
Nov  3 02:11:01 ecs-df00 sm-mta[1454108]: 2A2IB1Fc1454108: from=<root@ecs-df00.com>, size=340, class=0, nrcpts=1, msgid=<202211021811.2A2IB1eT1454106@ecs-df00.com>, proto=ESMTP, daemon=MTA-v4, relay=localhost [127.0.0.1]

 注意: 本环境使用的事华为云服务器,如是腾讯云或阿里云,25端口被禁用,除放开对应端口安全组规则和防火墙外,还需额外提单,故建议更改端口号如:

sudo vim /etc/mail/sendmail.mc

修改内部smtp(默认端口为25)为 自己设定的端口 如 8020 即可

FEATURE(`no_default_msa')dnl
dnl DAEMON_OPTIONS(`Family=inet6, Name=MTA-v6, Port=smtp, Addr=::1')dnl
DAEMON_OPTIONS(`Family=inet,  Name=MTA-v4, Port=8020, Addr=0.0.0.0')dnl
dnl DAEMON_OPTIONS(`Family=inet6, Name=MSP-v6, Port=submission, M=Ea, Addr=::1')dnl
DAEMON_OPTIONS(`Family=inet,  Name=MSP-v4, Port=submission, M=Ea, Addr=127.0.0.1')dnl

 

posted @   刀霸汉  阅读(923)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
点击右上角即可分享
微信分享提示