当前位置: 首页 > 工具软件 > msmtp > 使用案例 >

msmtp使用手册

燕俊明
2023-12-01

内容:
      1、msmtp工作模式
      2、命令行选项
      3、配置文件
      4、配置文件示例
      5、应用举例

       msmtp是一个SMTP客户端程序,利用它可以在命令行下发邮件。

一、工作模式:
   msmtp共有三种工作模式:
       Sendmail mode (default):
              msmtp [选项...] [--] 收件人信箱...
              msmtp [选项...] -t [--] [收件人信箱...]

       Server information mode:
              msmtp [option...] --serverinfo

       Remote Message Queue Starting mode:
              msmtp [option...] --rmqs=host|@domain|#queue

       在默认模式(sendmail mode)下, msmtp从标准输入读入邮件,然后送到SMTP服务器上转发。
       在server information mode下, msmtp显示SMTP服务器的信息(如支持的认证方式,邮件最大长度等)。
       In Remote Message Queue Starting mode, msmtp sends a Remote Message
       Queue Starting request for a host, domain, or queue to an SMTP server.

二、命令行选项:
      命令行选项可覆盖配置文件中的相同设置。
       They are compatible with sendmail where appropriate.

       General options
              --version   显示版本信息、配置文件位置、支持的认证方式等信息
              --help 显示帮助信息
              -P, --pretend 打印当前配置,口令将以*号显示
              -d, --debug   调试模式,会显示与SMTP服务器的交互过程

       Changing the mode of operation
              -S, --serverinfo 显示SMTP服务器的信息
                     This includes information about supported features (mail size
                     limit, authentication, TLS, DSN, ...) and about the TLS certificate (if TLS is active).

              --rmqs=(host|@domain|#queue)
                     Send a Remote Message Queue Starting request for the
                     given host, domain, or queue to the SMTP server and exit.

       Configuration options

              -C, --file=filename 用指定的配置文件代替 ~/.msmtprc作为用户配置文件
              -a, --account=account_name 指定帐号名称,不指定将用default(默认帐号名称)
                                         This option cannot be used together with the --host option.
              --host=hostname 指定SMTP服务器
   This option cannot be used together with the --account option.
              --port=number 指定服务器端口
              --timeout=(off|seconds) 设定超时时间
              --protocol=(smtp|lmtp)   设定邮件发送协议
              --auth[=(on|off|method)] 指定账号认证模式
              --user=[username] 指定邮箱用户名
              --tls[=(on|off)]
                     Enable or disable TLS/SSL encryption.
              --tls-starttls[=(on|off)]
                     Enable or disable STARTTLS for TLS encryption.
              --tls-trust-file=[file]
                     Set or unset a trust file for TLS encryption.
              --tls-key-file=[file]
                     Set or unset a key file for TLS encryption.
              --tls-cert-file=[file]
                     Set or unset a cert file for TLS encryption.
              --tls-certcheck[=(on|off)]
                     Enable or disable server certificate checks for TLS
                     encryption.
              --tls-force-sslv3[=(on|off)]
                     Force TLS/SSL version SSLv3.
              --domain=[string]
                     Set the argument of the SMTP EHLO (or LMTP LHLO) command.

       Options specific to sendmail mode

              --auto-from[=(on|off)] 自动生成from地址,默认为off
                     Enable or disable automatic envelope-from addresses. The
                     default is off.
              -f, --from=address 指定from地址,只有在auto-from为off时有效
              --maildomain=[domain]
                     Set the domain   part   for   generated   envelope-from
                     addresses. It is only used when auto_from is on. See the
                     maildomain command below.

              -N, --dsn-notify=(off|cond)
                     Set or unset DSN notification conditions.
              -R, --dsn-return=(off|ret)
                     Set or unset the DSN notification amount.
              --keepbcc[=(on|off)]
                     Enable or disable the preservation of the Bcc header.
              -X, --logfile=[file] 设置日志文件
              --syslog[=(on|off|facility)] 设置syslog
                     Enable or disable syslog logging.
              -t, --read-recipients 根据邮件内容(To:中的信息)生成收件人地址,这样就不必有命令行中指定收件人
              --     选项结束标志,此后的信息均被认为是收件人地址(即使它们含有-号)

       The following options are accepted but ignored for sendmail compatibil-
       ity:
       -Btype, -bm, -Fname, -G, -hN, -i, -L tag, -m, -n, -O option=value, -ox
       value, -v

三、配置文件:
       配置文件有两类,一类是系统配置,一类是用户配置,配置文件中的内容几乎都可在命令行中指定。

       关于SMTP服务器的信息是按账号组织的,每个账号下描述了SMTP服务器的名称(host)、认证方式(auth)、From信息、TLS 设置等信息. 配置文件中可设置多个账号。

       用户可用三种方式指定账号:
       --account=id 直接指定账号
       --host=hostname 根据服务器选择账号
       --from=address 根据from信息选择账号

       如果以上三种方式都不用,则使用 "default"账号

       系统配置文件为 SYSCONFDIR/msmtprc, 其中SYSCONFDIR由所使用的平台决定. 用 --version选项可显示具体位置.
       用户配置文件为~/.msmtprc. 用户配置文件中的内容会覆盖系统配置文件的相同设置。用户配置文件的读定权限必须为600)(chmod 600)
     
      可配置的命令有:
       defaults
              defaults下设置部分项目的默认值,若在账号下没有指定这些项的具体值,则采用这里指定的值
       account name [:account[,...]] 设置账号,:表示此账号的未指定值将从其他账号继承

       host hostname 指定SMTP服务器

       port number 指定端口

       timeout (off|seconds) 指定超时

       protocol (smtp|lmtp) 指定协议

       auto_from [(on|off)] 是否自动生成from信息

       from envelope_from 指定from信息

       maildomain [domain]
              Set a domain part for the generation of an envelope-from
              address. This is only used when auto_from is on. The domain may
              be empty.

       auth [(on|off|method)] 指定认证方式,
              Available methods are plain, cram-md5, digest-md5, gssapi,
              external, login, and ntlm. Note that one or more of these meth-
              ods may be unavailable due to lack of support in the underlying
              authentication library. Use the --version option to find out
              which methods are supported.
              The plain and login methods send your authentication data in
              cleartext over the net

       user [username] 指定用户名

       password [secret] 指定口令
              If no password is set but one is needed during authentication, msmtp will try to find
              it in ~/.netrc, and if that fails, msmtp will prompt you for it.

       domain argument
              Use this command to set the argument of the SMTP EHLO (or LMTP
              LHLO) command. The default is localhost (stupid, but working).

       keepbcc [(on|off)] 是否保留密送地址,默认为不保留
              This command controls whether to remove or keep the Bcc header
              when sending a mail. The default is to remove it.

       logfile [file] 指定日志文件

       syslog [(on|off|facility)] 指定SYSLOG设置
              Enable or disable syslog logging. The facility can be one of
              LOG_USER, LOG_MAIL, LOG_LOCAL0, ..., LOG_LOCAL7. The default is
              LOG_USER.

       ntlmdomain [domain]
              Set a domain for the ntlm authentication method.

       tls [(on|off)]

       tls_starttls [(on|off)]

       tls_trust_file [file]

       tls_key_file [file]

       tls_cert_file [file]

       tls_certcheck [(on|off)]

       tls_force_sslv3 [(on|off)]

       dsn_notify (off|condition)

       dsn_return (off|amount)

四、配置文件示例:
      Configuration file

       # Set default values for all following accounts.
       defaults
       tls on
       tls_trust_file /etc/ssl/certs/ca-certificates.crt
       logfile ~/.msmtp.log

       # A freemail service
       account freemail
       host smtp.freemail.example
       from joe_smith@freemail.example
       auth on
       user joe.smith
       password secret

       # A second mail address at the same freemail service
       account freemail2 : freemail
       from joey@freemail.example

       # The SMTP server of the provider.
       account provider
       host mail.provider.example
       from smithjoe@provider.example
       auth on
       user 123456789
       password my_password

       # Set a default account
       account default : provider

五、应用举例
       在设置好配置文件后,就可以在命令行下用msmtp发送邮件了,下面是一个例子:
       在shell下运行命令:msmtp -t        #这里用 -t 来指定收件人。由于msmtp要从标准输入中读取邮件,因此回车后,进入输入方式,下面接着输入:
to: xxxx@163.com      #设定收件人,以便让 -t 参数生效
subject: test                #设定主题

                                  #空行
test smtp
ctrl+D结束

      随后检查信箱,就会收到邮件。

 类似资料: