本文简单整理了一下如何在Linux服务器上安装、配置SendMail发送邮件的步骤,此文不是配置邮件服务器,具体测试环境为CentOS Linux release 7.2.1511 (Core) ,如遇特殊平台有所差别,请以实际情况为准则。
Linux 系统版本查看
检查、了解系统版本信息,主要是如果使用rpm安装时,需要下载合适的版本。
[root@mylnx06 ~]# more /etc/redhat-releaseCentOS Linux release 7.2.1511 (Core)
检查是否安装sendmail组件
如下所示,该服务器上并没有安装sendmail的相关组件。那么我们直接使用yum安装sendmail组件即可。
[root@mylnx06 ~]# rpm -qa | grep sendmail[root@mylnx06 ~]# yum list sendmailLoaded plugins: fastestmirror, langpacksLoading mirror speeds from cached hostfile* epel: ftp.cuhk.edu.hkAvailable Packagessendmail.x86_64 8.14.7-4.el7 base
使用yum安装sendmail组件,当然你可以使用其它方式安装,此处不做展开介绍。
[root@mylnx06 ~]# yum install sendmail.x86_64Loaded plugins: fastestmirror, langpacksLoading mirror speeds from cached hostfile* epel: ftp.cuhk.edu.hkResolving Dependencies--> Running transaction check---> Package sendmail.x86_64 0:8.14.7-4.el7 will be installed--> Finished Dependency ResolutionDependencies Resolved========================================================================================================================Package Arch Version Repository Size========================================================================================================================Installing:sendmail x86_64 8.14.7-4.el7 base 722 kTransaction Summary========================================================================================================================Install 1 PackageTotal download size: 722 kInstalled size: 1.6 MIs this ok [y/d/N]: yDownloading packages:sendmail-8.14.7-4.el7.x86_64.rpm | 722 kB 00:00:00Running transaction checkRunning transaction testTransaction test succeededRunning transactionInstalling : sendmail-8.14.7-4.el7.x86_64 1/1Verifying : sendmail-8.14.7-4.el7.x86_64 1/1Installed:sendmail.x86_64 0:8.14.7-4.el7Complete![root@mylnx06 ~]#
编辑/etc/aliases文件
Sendmail有一个非常实用的功能,就是为每个账号取别名(Aliase) 会使用一个在 /etc/aliases 中的档案做使用者名称转换的动作。 所以这个配置文件是可配也可以不配,具体视你的需求而定。
[root@mylnx06 ~]# vi /etc/aliases## Aliases in this file will NOT be expanded in the header from# Mail, but WILL be visible over networks or from /bin/mail.## >>>>>>>>>> The program "newaliases" must be run after# >> NOTE >> this file is updated for any changes to# >>>>>>>>>> show through to sendmail.## Basic system aliases -- these MUST be present.mailer-daemon: postmasterpostmaster: root# General redirections for pseudo accounts.bin: rootdaemon: rootadm: rootlp: rootsync: rootshutdown: roothalt: rootmail: rootnews: rootuucp: rootoperator: rootgames: rootgopher: rootftp: rootnobody: rootradiusd: rootnut: rootdbus: rootvcsa: rootcanna: rootwnn: rootrpm: rootnscd: rootpcap: rootapache: rootwebalizer: rootdovecot: rootfax: rootquagga: rootradvd: rootpvm: rootamandabackup: rootprivoxy: rootident: rootnamed: rootxfs: rootgdm: rootmailnull: rootpostgres: rootsshd: rootsmmsp: rootpostfix: rootnetdump: rootldap: rootsquid: rootntp: rootmysql: rootdesktop: rootrpcuser: rootrpc: rootnfsnobody: rootingres: rootsystem: roottoor: rootmanager: rootdumper: rootabuse: rootnewsadm: newsnewsadmin: newsusenet: newsftpadm: ftpftpadmin: ftpftp-adm: ftpftp-admin: ftpwww: webmasterwebmaster: rootnoc: rootsecurity: roothostmaster: rootinfo: postmastermarketing: postmastersales: postmastersupport: postmaster# trap decode to catch security attacksdecode: root# Person who should get root's mail#root: marc
如下截图所示,如果我配置别名为root的,配置接收邮件(下面邮件仅仅是一个模板而已)
修改配置文件sendmail.cf
修改/etc/mail/sendmail.cf 添加对应的DS, DS后面为邮件服务器的IP地址。根据实际具体情况配置。其实这个配置文件,如果需要,还有蛮多需要配置、细化的地方,此处只讲述最简单的配置。
# host/domain names ending with a token in class P are canonicalCP.# "Smart" relay host (may be null)DS[xxx.xxx.xxx.xxx]# operators that cannot be in local usernames (i.e., network indicators)CO @ % !# a class with just dot (for identifying canonical names)
重启sendmail服务
[root@mylnx06 ~]# service sendmail statusRedirecting to /bin/systemctl status sendmail.service● sendmail.service - Sendmail Mail Transport AgentLoaded: loaded (/usr/lib/systemd/system/sendmail.service; enabled; vendor preset: disabled)Active: inactive (dead)Feb 22 10:29:45 azlnx06 systemd[1]: Stopped Sendmail Mail Transport Agent.[root@mylnx06 ~]# service sendmail startRedirecting to /bin/systemctl start sendmail.service[root@mylnx06 ~]# service sendmail statusRedirecting to /bin/systemctl status sendmail.service● sendmail.service - Sendmail Mail Transport AgentLoaded: loaded (/usr/lib/systemd/system/sendmail.service; enabled; vendor preset: disabled)Active: active (running) since Wed 2017-02-22 10:42:02 HKT; 5s agoProcess: 46129 ExecStart=/usr/sbin/sendmail -bd $SENDMAIL_OPTS $SENDMAIL_OPTARG (code=exited, status=0/SUCCESS)Process: 46116 ExecStartPre=/etc/mail/make aliases (code=exited, status=0/SUCCESS)Process: 46115 ExecStartPre=/etc/mail/make (code=exited, status=0/SUCCESS)Main PID: 46131 (sendmail)CGroup: /system.slice/sendmail.service└─46131 sendmail: accepting connectionsFeb 22 10:42:02 azlnx06 systemd[1]: Starting Sendmail Mail Transport Agent...Feb 22 10:42:02 azlnx06 sendmail[46124]: alias database /etc/aliases rebuilt by rootFeb 22 10:42:02 azlnx06 sendmail[46124]: /etc/aliases: 77 aliases, longest 17 bytes, 792 bytes totalFeb 22 10:42:02 azlnx06 sendmail[46131]: starting daemon (8.14.7): SMTP+queueing@01:00:00Feb 22 10:42:02 azlnx06 systemd[1]: PID file /run/sendmail.pid not readable (yet?) after start.Feb 22 10:42:02 azlnx06 systemd[1]: Started Sendmail Mail Transport Agent
测试邮件服务
如下所示,使用mail命令测试能否成功收到邮件。如果成功,表示前面配置OK, 如果没有收到邮件,则必须检查日志,然后根据具体错误信息诊断问题。
[root@mylnx06 ~]# cat > test.txtit is only test^Z[1]+ Stopped cat > test.txt[root@mylnx06 ~]# mail -s "test" konglb@xxxx.com < test.txt[root@mylnx06 ~]#[azrlnx06azlnx06 ~]$ echo 'it is only a test' | mail -s "test eamil" konglb@xxxx.com[azrlnx06@azlnx06 ~]$