sSMTP
sSMTP 是一个程序,用于将电子邮件从本地计算机传递到配置的邮件主机(邮件枢纽)。它不是邮件服务器(不像功能丰富的邮件服务器 sendmail),并且不接收邮件、展开别名或管理队列。它的主要用途之一是将自动电子邮件(如系统警报)从您的机器转发到外部电子邮件地址。
安装
转发到 Gmail 邮件服务器
要配置 sSMTP,您需要编辑其配置文件(/etc/ssmtp/ssmtp.conf
)并输入您的帐户设置。
- 如果您的 Gmail 帐户使用双因素身份验证保护,您需要生成一个唯一的 应用密码 以在
ssmtp.conf
中使用。您可以在您的 应用密码 页面上执行此操作。在AuthUser
行中使用您的 Gmail 用户名(不是应用名称),并在AuthPass
行中使用生成的 16 个字符的密码,密码中的空格可以省略。 - 如果您不使用双因素身份验证,您需要允许访问安全性较低的应用。您可以在您的 安全性较低的应用 页面上执行此操作。
/etc/ssmtp/ssmtp.conf
# The user that gets all the mails (UID < 1000, usually the admin) root=username@gmail.com # The mail server (where the mail is sent to), both port 465 or 587 should be acceptable # See also https://support.google.com/mail/answer/78799 mailhub=smtp.gmail.com:465 # The address where the mail appears to come from for user authentication. rewriteDomain=gmail.com # The full hostname. Must be correctly formed, fully qualified domain name or GMail will reject connection. hostname=yourlocalhost.yourlocaldomain.tld # Use implicit TLS (port 465). When using port 587, change UseSTARTTLS=Yes TLS_CA_FILE=/etc/ssl/certs/ca-certificates.crt UseTLS=Yes UseSTARTTLS=No # Username/Password AuthUser=username AuthPass=password AuthMethod=LOGIN # Email 'From header's can override the default domain? FromLineOverride=yes
为本地用户名创建别名(可选)
/etc/ssmtp/revaliases
root:username@gmail.com:smtp.gmail.com:465 mainuser:username@gmail.com:smtp.gmail.com:465
要测试 Gmail 服务器是否会正确转发您的电子邮件
$ echo -e 'Subject: test\n\nTesting ssmtp' | sendmail -v tousername@example.com
通过编辑 /etc/passwd
更改“发件人”文本,以接收来自“root at myhost”而不是仅“root”的邮件。
# chfn -f 'root at myhost' root # chfn -f 'mainuser at myhost' mainuser
这将 /etc/passwd
更改为
$ grep myhost /etc/passwd
root:x:0:0:root at myhost,,,:/root:/bin/bash mainuser:x:1000:1000:mainuser at myhost,,,:/home/mainuser:/bin/bash
安全
由于您的电子邮件密码以明文形式存储在 /etc/ssmtp/ssmtp.conf
中,因此确保此文件的安全非常重要。默认情况下,整个 /etc/ssmtp
目录仅对 root 用户和 mail 组可访问。/usr/bin/ssmtp
二进制文件以 mail 组身份运行,并且可以读取此文件。没有理由将您自己或其他用户添加到 mail 组。
发送邮件
要从终端发送电子邮件,请执行以下操作
$ echo -e "Subject: this is the subject\n\nthis is the body" | mail user@example.com
或以交互方式
$ sendmail username@example.com Subject: this is my subject CC: optional@example.com Now I can type the body here
Ctrl
+d
以结束您的消息并自动发送出去。发送电子邮件的另一种方法是创建一个文本文件,并使用 ssmtp 或 mail 发送它
test-mail.txt
To:username@example.com From:youraccount@gmail.com Subject: Test This is a test mail.
发送 test-mail.txt
文件
$ sendmail -t < test-mail.txt
某些用户可能更喜欢来自 s-nail、mailutils 或其他 mailx 提供程序的 mail 语法。例如,mail 具有将主题作为参数提供的选项。mail 需要 sendmail 并且可以使用 ssmtpAUR 作为 sendmail。
附件
如果您需要能够添加附件,请安装和配置 Mutt 和 Msmtp,然后查看 nixcraft 上的提示。
或者,您可以使用来自 sharutils 的 uuencode 添加附件。要将 'file.txt' 作为 'myfile.txt' 附加
$ uuencode file.txt myfile.txt | sendmail user@example.com
发送邮件给本地用户
发送给本地用户(或任何其他不以 @fqdn 结尾的地址)的消息以两种方式之一处理
- 目标用户的 UID < 1000 - 地址被
/etc/ssmtp/ssmtp.conf
中root=user@fqdn
定义的地址替换 - 目标用户的 UID ≥ 1000 或用户未知 - 来自
/etc/ssmtp/ssmtp.conf
中rewriteDomain=
的值将附加到用户 ID 的末尾。
如果您的系统上的本地用户在您的 rewriteDomain
中也不是有效用户,但正在接收来自系统服务的邮件,尤其是在您的重写域是像 gmail.com
这样的公共服务时,这可能会导致问题。
为了解决这个问题,您可以使用来自 s-nail 的 mail。mail 命令可以读取在 /etc/mail.rc
中定义的别名。示例
$ grep alias /etc/mail.rc
alias git git<username@example.com> alias archuser 'My Name'<someone@example.com>
然后,您可以将消息管道传输到 mail 而不是 sendmail。
$ echo -e "Hey archuser." | mail archuser
/bin/mail
。不要这样做。sendmail 和 mail 在参数和标准输入方面具有不同的语法。最好找到直接使用 sendmail 的进程,并将它们配置为改用 mail。