IT博客汇
  • 首页
  • 精华
  • 技术
  • 设计
  • 资讯
  • 扯淡
  • 权利声明
  • 登录 注册

    Linux:配置Sendmail,并发送一封邮件

    Adamhuan发表于 2017-03-16 08:41:32
    love 0

    首先,安装Sendmail:

    [root@ora12c-1 ~]# yum install sendmail sendmail-cf m4
    Loaded plugins: langpacks, product-id, search-disabled-repos, subscription-manager
    This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
    Resolving Dependencies
    --> Running transaction check
    ---> Package m4.x86_64 0:1.4.16-10.el7 will be installed
    ---> Package sendmail.x86_64 0:8.14.7-4.el7 will be installed
    --> Processing Dependency: procmail for package: sendmail-8.14.7-4.el7.x86_64
    --> Processing Dependency: libhesiod.so.0()(64bit) for package: sendmail-8.14.7-4.el7.x86_64
    ---> Package sendmail-cf.noarch 0:8.14.7-4.el7 will be installed
    --> Running transaction check
    ---> Package hesiod.x86_64 0:3.2.1-3.el7 will be installed
    ---> Package procmail.x86_64 0:3.22-35.el7 will be installed
    --> Finished Dependency Resolution
    
    Dependencies Resolved
    
    =================================================================================================================================================================================
     Package                                      Arch                                    Version                                        Repository                             Size
    =================================================================================================================================================================================
    Installing:
     m4                                           x86_64                                  1.4.16-10.el7                                  base                                  256 k
     sendmail                                     x86_64                                  8.14.7-4.el7                                   base                                  722 k
     sendmail-cf                                  noarch                                  8.14.7-4.el7                                   base                                  185 k
    Installing for dependencies:
     hesiod                                       x86_64                                  3.2.1-3.el7                                    base                                   30 k
     procmail                                     x86_64                                  3.22-35.el7                                    base                                  171 k
    
    Transaction Summary
    =================================================================================================================================================================================
    Install  3 Packages (+2 Dependent packages)
    
    Total download size: 1.3 M
    Installed size: 3.4 M
    Is this ok [y/d/N]: y
    Downloading packages:
    (1/5): hesiod-3.2.1-3.el7.x86_64.rpm                                                                                                                      |  30 kB  00:00:01     
    (2/5): m4-1.4.16-10.el7.x86_64.rpm                                                                                                                        | 256 kB  00:00:01     
    (3/5): procmail-3.22-35.el7.x86_64.rpm                                                                                                                    | 171 kB  00:00:01     
    (4/5): sendmail-8.14.7-4.el7.x86_64.rpm                                                                                                                   | 722 kB  00:00:02     
    (5/5): sendmail-cf-8.14.7-4.el7.noarch.rpm                                                                                                                | 185 kB  00:00:03     
    ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    Total                                                                                                                                            188 kB/s | 1.3 MB  00:00:07     
    Running transaction check
    Running transaction test
    Transaction test succeeded
    Running transaction
    Warning: RPMDB altered outside of yum.
      Installing : procmail-3.22-35.el7.x86_64                                                                                                                                   1/5 
      Installing : m4-1.4.16-10.el7.x86_64                                                                                                                                       2/5 
      Installing : hesiod-3.2.1-3.el7.x86_64                                                                                                                                     3/5 
      Installing : sendmail-8.14.7-4.el7.x86_64                                                                                                                                  4/5 
      Installing : sendmail-cf-8.14.7-4.el7.noarch                                                                                                                               5/5 
      Verifying  : hesiod-3.2.1-3.el7.x86_64                                                                                                                                     1/5 
      Verifying  : m4-1.4.16-10.el7.x86_64                                                                                                                                       2/5 
      Verifying  : sendmail-cf-8.14.7-4.el7.noarch                                                                                                                               3/5 
      Verifying  : sendmail-8.14.7-4.el7.x86_64                                                                                                                                  4/5 
      Verifying  : procmail-3.22-35.el7.x86_64                                                                                                                                   5/5 
    
    Installed:
      m4.x86_64 0:1.4.16-10.el7                             sendmail.x86_64 0:8.14.7-4.el7                             sendmail-cf.noarch 0:8.14.7-4.el7                            
    
    Dependency Installed:
      hesiod.x86_64 0:3.2.1-3.el7                                                            procmail.x86_64 0:3.22-35.el7                                                           
    
    Complete!
    [root@ora12c-1 ~]#

    sendmail的配置文件路径:

    [root@ora12c-1 ~]# cd /etc/mail
    [root@ora12c-1 mail]# ls
    access     aliasesdb-stamp  domaintable.db  local-host-names  mailertable.db  Makefile     sendmail.mc  submit.mc      virtusertable
    access.db  domaintable      helpfile        mailertable       make            sendmail.cf  submit.cf    trusted-users  virtusertable.db
    [root@ora12c-1 mail]# 
    [root@ora12c-1 mail]#

    编辑配置文件:sendmailmc

    备份:

    [root@ora12c-1 mail]# cp sendmail.mc sendmail.mc_orig
    [root@ora12c-1 mail]#

    编辑完成后,比较新旧文件,就可以发现哪里做出了变更:

    [root@ora12c-1 mail]# cat sendmail.mc | grep -E --color "127.0.0.1|relay_hosts_only"
    dnl # 127.0.0.1 and not on any other network devices. Remove the loopback
    dnl DAEMON_OPTIONS(`Port=smtp,Addr=127.0.0.1, Name=MTA')dnl
    FEATURE(relay_hosts_only)dnl
    [root@ora12c-1 mail]# 
    [root@ora12c-1 mail]# diff sendmail.mc sendmail.mc_orig 
    111d110
    < 
    119c118
    < dnl DAEMON_OPTIONS(`Port=smtp,Addr=127.0.0.1, Name=MTA')dnl
    ---
    > DAEMON_OPTIONS(`Port=smtp,Addr=127.0.0.1, Name=MTA')dnl
    177,178d175
    < 
    < FEATURE(relay_hosts_only)dnl
    [root@ora12c-1 mail]#

    该文件完整的内容如下:sendmail.mc

    [root@ora12c-1 mail]# cat sendmail.mc
    divert(-1)dnl
    dnl #
    dnl # This is the sendmail macro config file for m4. If you make changes to
    dnl # /etc/mail/sendmail.mc, you will need to regenerate the
    dnl # /etc/mail/sendmail.cf file by confirming that the sendmail-cf package is
    dnl # installed and then performing a
    dnl #
    dnl #     /etc/mail/make
    dnl #
    include(`/usr/share/sendmail-cf/m4/cf.m4')dnl
    VERSIONID(`setup for linux')dnl
    OSTYPE(`linux')dnl
    dnl #
    dnl # Do not advertize sendmail version.
    dnl #
    dnl define(`confSMTP_LOGIN_MSG', `$j Sendmail; $b')dnl
    dnl #
    dnl # default logging level is 9, you might want to set it higher to
    dnl # debug the configuration
    dnl #
    dnl define(`confLOG_LEVEL', `9')dnl
    dnl #
    dnl # Uncomment and edit the following line if your outgoing mail needs to
    dnl # be sent out through an external mail server:
    dnl #
    dnl define(`SMART_HOST', `smtp.your.provider')dnl
    dnl #
    define(`confDEF_USER_ID', ``8:12'')dnl
    dnl define(`confAUTO_REBUILD')dnl
    define(`confTO_CONNECT', `1m')dnl
    define(`confTRY_NULL_MX_LIST', `True')dnl
    define(`confDONT_PROBE_INTERFACES', `True')dnl
    define(`PROCMAIL_MAILER_PATH', `/usr/bin/procmail')dnl
    define(`ALIAS_FILE', `/etc/aliases')dnl
    define(`STATUS_FILE', `/var/log/mail/statistics')dnl
    define(`UUCP_MAILER_MAX', `2000000')dnl
    define(`confUSERDB_SPEC', `/etc/mail/userdb.db')dnl
    define(`confPRIVACY_FLAGS', `authwarnings,novrfy,noexpn,restrictqrun')dnl
    define(`confAUTH_OPTIONS', `A')dnl
    dnl #
    dnl # The following allows relaying if the user authenticates, and disallows
    dnl # plaintext authentication (PLAIN/LOGIN) on non-TLS links
    dnl #
    dnl define(`confAUTH_OPTIONS', `A p')dnl
    dnl # 
    dnl # PLAIN is the preferred plaintext authentication method and used by
    dnl # Mozilla Mail and Evolution, though Outlook Express and other MUAs do
    dnl # use LOGIN. Other mechanisms should be used if the connection is not
    dnl # guaranteed secure.
    dnl # Please remember that saslauthd needs to be running for AUTH. 
    dnl #
    dnl TRUST_AUTH_MECH(`EXTERNAL DIGEST-MD5 CRAM-MD5 LOGIN PLAIN')dnl
    dnl define(`confAUTH_MECHANISMS', `EXTERNAL GSSAPI DIGEST-MD5 CRAM-MD5 LOGIN PLAIN')dnl
    dnl #
    dnl # Rudimentary information on creating certificates for sendmail TLS:
    dnl #     cd /etc/pki/tls/certs; make sendmail.pem
    dnl # Complete usage:
    dnl #     make -C /etc/pki/tls/certs usage
    dnl #
    dnl define(`confCACERT_PATH', `/etc/pki/tls/certs')dnl
    dnl define(`confCACERT', `/etc/pki/tls/certs/ca-bundle.crt')dnl
    dnl define(`confSERVER_CERT', `/etc/pki/tls/certs/sendmail.pem')dnl
    dnl define(`confSERVER_KEY', `/etc/pki/tls/certs/sendmail.pem')dnl
    dnl #
    dnl # This allows sendmail to use a keyfile that is shared with OpenLDAP's
    dnl # slapd, which requires the file to be readble by group ldap
    dnl #
    dnl define(`confDONT_BLAME_SENDMAIL', `groupreadablekeyfile')dnl
    dnl #
    dnl define(`confTO_QUEUEWARN', `4h')dnl
    dnl define(`confTO_QUEUERETURN', `5d')dnl
    dnl define(`confQUEUE_LA', `12')dnl
    dnl define(`confREFUSE_LA', `18')dnl
    define(`confTO_IDENT', `0')dnl
    dnl # If you're operating in a DSCP/RFC-4594 environment with QoS
    dnl define(`confINET_QOS', `AF11')dnl
    dnl FEATURE(delay_checks)dnl
    FEATURE(`no_default_msa', `dnl')dnl
    FEATURE(`smrsh', `/usr/sbin/smrsh')dnl
    FEATURE(`mailertable', `hash -o /etc/mail/mailertable.db')dnl
    FEATURE(`virtusertable', `hash -o /etc/mail/virtusertable.db')dnl
    FEATURE(redirect)dnl
    FEATURE(always_add_domain)dnl
    FEATURE(use_cw_file)dnl
    FEATURE(use_ct_file)dnl
    dnl #
    dnl # The following limits the number of processes sendmail can fork to accept 
    dnl # incoming messages or process its message queues to 20.) sendmail refuses 
    dnl # to accept connections once it has reached its quota of child processes.
    dnl #
    dnl define(`confMAX_DAEMON_CHILDREN', `20')dnl
    dnl #
    dnl # Limits the number of new connections per second. This caps the overhead 
    dnl # incurred due to forking new sendmail processes. May be useful against 
    dnl # DoS attacks or barrages of spam. (As mentioned below, a per-IP address 
    dnl # limit would be useful but is not available as an option at this writing.)
    dnl #
    dnl define(`confCONNECTION_RATE_THROTTLE', `3')dnl
    dnl #
    dnl # The -t option will retry delivery if e.g. the user runs over his quota.
    dnl #
    FEATURE(local_procmail, `', `procmail -t -Y -a $h -d $u')dnl
    FEATURE(`access_db', `hash -T -o /etc/mail/access.db')dnl
    FEATURE(`blacklist_recipients')dnl
    EXPOSED_USER(`root')dnl
    dnl #
    dnl # For using Cyrus-IMAPd as POP3/IMAP server through LMTP delivery uncomment
    dnl # the following 2 definitions and activate below in the MAILER section the
    dnl # cyrusv2 mailer.
    dnl #
    
    dnl define(`confLOCAL_MAILER', `cyrusv2')dnl
    dnl define(`CYRUSV2_MAILER_ARGS', `FILE /var/lib/imap/socket/lmtp')dnl
    dnl #
    dnl # The following causes sendmail to only listen on the IPv4 loopback address
    dnl # 127.0.0.1 and not on any other network devices. Remove the loopback
    dnl # address restriction to accept email from the internet or intranet.
    dnl #
    dnl DAEMON_OPTIONS(`Port=smtp,Addr=127.0.0.1, Name=MTA')dnl
    dnl #
    dnl # The following causes sendmail to additionally listen to port 587 for
    dnl # mail from MUAs that authenticate. Roaming users who can't reach their
    dnl # preferred sendmail daemon due to port 25 being blocked or redirected find
    dnl # this useful.
    dnl #
    dnl DAEMON_OPTIONS(`Port=submission, Name=MSA, M=Ea')dnl
    dnl #
    dnl # The following causes sendmail to additionally listen to port 465, but
    dnl # starting immediately in TLS mode upon connecting. Port 25 or 587 followed
    dnl # by STARTTLS is preferred, but roaming clients using Outlook Express can't
    dnl # do STARTTLS on ports other than 25. Mozilla Mail can ONLY use STARTTLS
    dnl # and doesn't support the deprecated smtps; Evolution <1.1.1 uses smtps
    dnl # when SSL is enabled-- STARTTLS support is available in version 1.1.1.
    dnl #
    dnl # For this to work your OpenSSL certificates must be configured.
    dnl #
    dnl DAEMON_OPTIONS(`Port=smtps, Name=TLSMTA, M=s')dnl
    dnl #
    dnl # The following causes sendmail to additionally listen on the IPv6 loopback
    dnl # device. Remove the loopback address restriction listen to the network.
    dnl #
    dnl DAEMON_OPTIONS(`port=smtp,Addr=::1, Name=MTA-v6, Family=inet6')dnl
    dnl #
    dnl # enable both ipv6 and ipv4 in sendmail:
    dnl #
    dnl DAEMON_OPTIONS(`Name=MTA-v4, Family=inet, Name=MTA-v6, Family=inet6')
    dnl #
    dnl # We strongly recommend not accepting unresolvable domains if you want to
    dnl # protect yourself from spam. However, the laptop and users on computers
    dnl # that do not have 24x7 DNS do need this.
    dnl #
    FEATURE(`accept_unresolvable_domains')dnl
    dnl #
    dnl FEATURE(`relay_based_on_MX')dnl
    dnl # 
    dnl # Also accept email sent to "localhost.localdomain" as local email.
    dnl # 
    LOCAL_DOMAIN(`localhost.localdomain')dnl
    dnl #
    dnl # The following example makes mail from this host and any additional
    dnl # specified domains appear to be sent from mydomain.com
    dnl #
    dnl MASQUERADE_AS(`mydomain.com')dnl
    dnl #
    dnl # masquerade not just the headers, but the envelope as well
    dnl #
    dnl FEATURE(masquerade_envelope)dnl
    dnl #
    dnl # masquerade not just @mydomainalias.com, but @*.mydomainalias.com as well
    dnl #
    dnl FEATURE(masquerade_entire_domain)dnl
    dnl #
    dnl MASQUERADE_DOMAIN(localhost)dnl
    dnl MASQUERADE_DOMAIN(localhost.localdomain)dnl
    dnl MASQUERADE_DOMAIN(mydomainalias.com)dnl
    dnl MASQUERADE_DOMAIN(mydomain.lan)dnl
    
    FEATURE(relay_hosts_only)dnl
    MAILER(smtp)dnl
    MAILER(procmail)dnl
    dnl MAILER(cyrusv2)dnl
    [root@ora12c-1 mail]#

    对文件【relay_domain】的设定:

    [root@ora12c-1 mail]# hostname
    ora12c-1
    [root@ora12c-1 mail]# 
    [root@ora12c-1 mail]# hostname >> /etc/mail/relay_domain
    [root@ora12c-1 mail]# 
    [root@ora12c-1 mail]# pwd
    /etc/mail
    [root@ora12c-1 mail]# 
    [root@ora12c-1 mail]# ls -ltr | grep relay_domain
    -rw-r--r-- 1 root root     9 Mar 16 16:21 relay_domain
    [root@ora12c-1 mail]# 
    [root@ora12c-1 mail]# cat relay_domain 
    ora12c-1
    [root@ora12c-1 mail]#

    重新编译生成sendmail的配置文件【sendmail.cf】

    [root@ora12c-1 mail]# ls -ltr sendmail.cf 
    -rw-r--r-- 1 root root 58630 Mar 16 16:25 sendmail.cf
    [root@ora12c-1 mail]# 
    [root@ora12c-1 mail]# m4 sendmail.mc > sendmail.cf 
    [root@ora12c-1 mail]# 
    [root@ora12c-1 mail]# ls -ltr sendmail.cf 
    -rw-r--r-- 1 root root 58846 Mar 16 16:26 sendmail.cf
    [root@ora12c-1 mail]#

    启动并查看SENDMAIL服务状态:

    [root@ora12c-1 mail]# ps -ef | grep sendmail
    root      3436 32059  0 16:29 pts/2    00:00:00 grep --color=auto sendmail
    [root@ora12c-1 mail]# 
    [root@ora12c-1 mail]# 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)
    [root@ora12c-1 mail]# 
    [root@ora12c-1 mail]# service sendmail start
    Redirecting to /bin/systemctl start  sendmail.service
    [root@ora12c-1 mail]# 
    [root@ora12c-1 mail]# 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 Thu 2017-03-16 16:30:30 CST; 5s ago
      Process: 3531 ExecStart=/usr/sbin/sendmail -bd $SENDMAIL_OPTS $SENDMAIL_OPTARG (code=exited, status=0/SUCCESS)
      Process: 3524 ExecStartPre=/etc/mail/make aliases (code=exited, status=0/SUCCESS)
      Process: 3519 ExecStartPre=/etc/mail/make (code=exited, status=0/SUCCESS)
     Main PID: 3770 (sendmail)
       CGroup: /system.slice/sendmail.service
               └─3770 sendmail: accepting connections
    
    Mar 16 16:29:30 ora12c-1 systemd[1]: Starting Sendmail Mail Transport Agent...
    Mar 16 16:29:30 ora12c-1 sendmail[3531]: My unqualified host name (ora12c-1) unknown; sleeping for retry
    Mar 16 16:30:30 ora12c-1 sendmail[3531]: unable to qualify my own domain name (ora12c-1) -- using short name
    Mar 16 16:30:30 ora12c-1 sendmail[3770]: starting daemon (8.14.7): SMTP+queueing@01:00:00
    Mar 16 16:30:30 ora12c-1 systemd[1]: PID file /run/sendmail.pid not readable (yet?) after start.
    Mar 16 16:30:30 ora12c-1 systemd[1]: Started Sendmail Mail Transport Agent.
    [root@ora12c-1 mail]# 
    [root@ora12c-1 mail]# ps -ef | grep sendmail
    root      3770     1  0 16:30 ?        00:00:00 sendmail: accepting connections
    root      3787     1  0 16:30 ?        00:00:00 /usr/sbin/sendmail -L sm-msp-queue -Ac -q1h
    root      3824 32059  0 16:30 pts/2    00:00:00 grep --color=auto sendmail
    [root@ora12c-1 mail]# 
    [root@ora12c-1 mail]#

    SENDMAIL的日志:/var/log/maillog
    Linux中,用户的MAIL数据的存放位置:/var/mail

    看看MAIL数据的状态:

    [root@ora12c-1 ~]# cd /var/mail/
    [root@ora12c-1 mail]# ls -ltr
    total 80
    -rw-rw----. 1 rpc      mail     0 Mar  6 16:06 rpc
    -rw-rw----. 1 adamhuan mail     0 Mar  6 16:14 adamhuan
    -rw-rw----. 1 oracle   mail     0 Mar  7 17:31 oracle
    -rw-------. 1 root     mail 79340 Mar  8 10:49 root
    [root@ora12c-1 mail]# 
    [root@ora12c-1 mail]# cat adamhuan | wc -l
    0
    [root@ora12c-1 mail]# 
    [root@ora12c-1 mail]# cat adamhuan 
    [root@ora12c-1 mail]# 
    [root@ora12c-1 mail]#

    用【adamhuan】测试sendmail邮件的功能:

    准备邮件文件:

    [root@ora12c-1 ~]# echo "Hello world, Time is: "`date "+|%Y-%m-%d|%H:%M:%S|"` > say_me.txt
    [root@ora12c-1 ~]# 
    [root@ora12c-1 ~]# cat say_me.txt 
    Hello world, Time is: |2017-03-16|16:36:01|
    [root@ora12c-1 ~]# 
    [root@ora12c-1 ~]#

    发邮件前,先查看adamhuan的问题:

    [root@ora12c-1 mail]# cat adamhuan 
    [root@ora12c-1 mail]#

    发邮件:

    [root@ora12c-1 ~]# mail -s "Say you" adamhuan@ora12c-1 < say_me.txt 
    [root@ora12c-1 ~]#

    发送后,查看:

    [root@ora12c-1 mail]# cat adamhuan 
    From root@ora12c-1  Thu Mar 16 16:38:52 2017
    Return-Path: 
    Received: from ora12c-1 (localhost [127.0.0.1])
            by ora12c-1 (8.14.7/8.14.7) with ESMTP id v2G8cqAT005349
            for ; Thu, 16 Mar 2017 16:38:52 +0800
    Received: (from root@localhost)
            by ora12c-1 (8.14.7/8.14.7/Submit) id v2G8cqhC005181
            for adamhuan@localhost; Thu, 16 Mar 2017 16:38:52 +0800
    From: root 
    Message-Id: <201703160838.v2G8cqhC005181@ora12c-1>
    Date: Thu, 16 Mar 2017 16:37:52 +0800
    To: adamhuan@ora12c-1
    Subject: Say you
    User-Agent: Heirloom mailx 12.5 7/5/10
    MIME-Version: 1.0
    Content-Type: text/plain; charset=us-ascii
    Content-Transfer-Encoding: 7bit
    
    Hello world, Time is: |2017-03-16|16:36:01|
    
    [root@ora12c-1 mail]# 
    [root@ora12c-1 mail]#

    ——————————————————————
    Done。



沪ICP备19023445号-2号
友情链接