歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux綜合 >> 學習Linux

Linux簡單配置SendMail發送郵件,sendmail發送郵件

Linux簡單配置SendMail發送郵件,sendmail發送郵件


熱度4 評論 235 www.BkJia.Com  網友分享於:  2017-02-23 04:02:18     浏覽數5907次

Linux簡單配置SendMail發送郵件,sendmail發送郵件


本文簡單整理了一下如何在Linux服務器上安裝、配置SendMail發送郵件的步驟,此文不是配置郵件服務器,具體測試環境為CentOS Linux release 7.2.1511 (Core) ,如遇特殊平台有所差別,請以實際情況為准則。

Linux 系統版本查看

 

檢查、了解系統版本信息,主要是如果使用rpm安裝時,需要下載合適的版本。

[root@mylnx06 ~]# more /etc/redhat-release 
CentOS Linux release 7.2.1511 (Core)

 

檢查是否安裝sendmail組件

 

如下所示,該服務器上並沒有安裝sendmail的相關組件。那麼我們直接使用yum安裝sendmail組件即可。

[root@mylnx06 ~]# rpm -qa | grep sendmail
 
 
 
[root@mylnx06 ~]# yum list sendmail
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * epel: ftp.cuhk.edu.hk
Available Packages
sendmail.x86_64                                       8.14.7-4.el7                    base

 

使用yum安裝sendmail組件,當然你可以使用其它方式安裝,此處不做展開介紹。

[root@mylnx06 ~]# yum install sendmail.x86_64 
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * epel: ftp.cuhk.edu.hk
Resolving Dependencies
--> Running transaction check
---> Package sendmail.x86_64 0:8.14.7-4.el7 will be installed
--> Finished Dependency Resolution
 
Dependencies Resolved
 
========================================================================================================================
 Package                     Arch                      Version                            Repository               Size
========================================================================================================================
Installing:
 sendmail                    x86_64                    8.14.7-4.el7                       base                    722 k
 
Transaction Summary
========================================================================================================================
Install  1 Package
 
Total download size: 722 k
Installed size: 1.6 M
Is this ok [y/d/N]: y
Downloading packages:
sendmail-8.14.7-4.el7.x86_64.rpm                                                                 | 722 kB  00:00:00     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : sendmail-8.14.7-4.el7.x86_64                                                                         1/1 
  Verifying  : sendmail-8.14.7-4.el7.x86_64                                                                         1/1 
 
Installed:
  sendmail.x86_64 0:8.14.7-4.el7                                                                                        
 
Complete!
[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:  postmaster
postmaster:     root
 
# General redirections for pseudo accounts.
bin:            root
daemon:         root
adm:            root
lp:             root
sync:           root
shutdown:       root
halt:           root
mail:           root
news:           root
uucp:           root
operator:       root
games:          root
gopher:         root
ftp:            root
nobody:         root
radiusd:        root
nut:            root
dbus:           root
vcsa:           root
canna:          root
wnn:            root
rpm:            root
nscd:           root
pcap:           root
apache:         root
webalizer:      root
dovecot:        root
fax:            root
quagga:         root
radvd:          root
pvm:            root
amandabackup:           root
privoxy:        root
ident:          root
named:          root
xfs:            root
gdm:            root
mailnull:       root
postgres:       root
sshd:           root
smmsp:          root
postfix:        root
netdump:        root
ldap:           root
squid:          root
ntp:            root
mysql:          root
desktop:        root
rpcuser:        root
rpc:            root
nfsnobody:      root
 
ingres:         root
system:         root
toor:           root
manager:        root
dumper:         root
abuse:          root
 
newsadm:        news
newsadmin:      news
usenet:         news
ftpadm:         ftp
ftpadmin:       ftp
ftp-adm:        ftp
ftp-admin:      ftp
www:            webmaster
webmaster:      root
noc:            root
security:       root
hostmaster:     root
info:           postmaster
marketing:      postmaster
sales:          postmaster
support:        postmaster
 
 
# trap decode to catch security attacks
decode:         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 canonical
CP.
 
# "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 status
Redirecting to /bin/systemctl status  sendmail.service
● sendmail.service - Sendmail Mail Transport Agent
   Loaded: 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 start
Redirecting to /bin/systemctl start  sendmail.service
[root@mylnx06 ~]# service sendmail status
Redirecting to /bin/systemctl status  sendmail.service
● sendmail.service - Sendmail Mail Transport Agent
   Loaded: loaded (/usr/lib/systemd/system/sendmail.service; enabled; vendor preset: disabled)
   Active: active (running) since Wed 2017-02-22 10:42:02 HKT; 5s ago
  Process: 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 connections
 
Feb 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 root
Feb 22 10:42:02 azlnx06 sendmail[46124]: /etc/aliases: 77 aliases, longest 17 bytes, 792 bytes total
Feb 22 10:42:02 azlnx06 sendmail[46131]: starting daemon (8.14.7): SMTP+queueing@01:00:00
Feb 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.txt
it is only test
^Z
[1]+  Stopped                 cat > test.txt
[root@mylnx06 ~]# mail -s "test" [email protected] < test.txt 
[root@mylnx06 ~]# 
 
 
[azrlnx06azlnx06 ~]$ echo 'it is only a test' | mail -s "test eamil"  [email protected]
[azrlnx06@azlnx06 ~]$ 

http://www.bkjia.com/Linuxjc/1195057.htmlwww.bkjia.comtrue

Copyright © Linux教程網 All Rights Reserved