我可以设置系统邮件以使用外部 SMTP 服务器吗?

cwd*_*cwd 94 linux smtp email

是否可以在 linux 机器上设置系统邮件以通过不同的 smtp 服务器发送 - 甚至可以进行身份​​验证?如果是这样,我该怎么做?

如果这还不清楚,让我们举个例子。如果我在命令行并输入:

cat body.txt | mail -s "just a test" myfriend@hisdomain.com
Run Code Online (Sandbox Code Playgroud)

是否可以通过外部 SMTP 服务器(例如 G-mail)发送?

我不是在寻找“一种从命令行从 gmail 发送邮件的方法”,而是一种将整个系统配置为使用特定 SMTP 服务器的选项,或者可能是 SMTP 服务器上的一个帐户(可能会覆盖发件人地址) .

Van*_*las 79

我发现 sSMTP 使用起来非常简单。

在基于 Debian 的系统中:

apt-get install ssmtp
Run Code Online (Sandbox Code Playgroud)

然后编辑/etc/ssmtp/ssmtp.conf中的配置文件

使用您的 gmail 发送电子邮件的示例配置:

# root is the person who gets all mail for userids < 1000
root=your@email.com

# Here is the gmail configuration (or change it to your private smtp server)
mailhub=smtp.gmail.com:587
AuthUser=your@gmail.com
AuthPass=yourGmailPass
UseTLS=YES
UseSTARTTLS=YES
Run Code Online (Sandbox Code Playgroud)

注意:确保您的系统中存在“mail”命令。mailutils 包应该在基于 Debian 的系统中提供这个。

更新:有人(以及针对不同 Linux 发行版的错误报告)报告说 sSMTP 不接受带有“空格”或“#”字符的密码。如果 sSMTP 对您不起作用,则可能是这种情况。

  • ssmtp 不验证当前 debian、ubuntu 和 redhat 版本上远程服务器的 SSL/TLS 证书,也不验证证书的主机名。这是一个主要问题,因为这有效地使加密变得无用,并且您的密码以纯文本形式传输,任何人都可以嗅探它。ssmtp 至少自 2009 年以来就没有积极的发展。因此,如果您关心用于服务器外发电子邮件的电子邮件帐户的安全性,请不要使用 ssmtp,而是使用 postfix(或其他东西):unix.stackexchange.com/ a/118101/72087 (13认同)
  • 作为对其他人的说明,您可能必须先安装 ssmtp,然后再安装 mailutils。我认为 mailutils 将邮件命令和符号链接设置为 ssmtp,因此您可以使用常规的`mail` 语法:) (3认同)

Nil*_*ils 26

对于后缀

  • 为您的外部邮件中继添加 IP 并为其/etc/hosts添加别名邮件中继。
  • 修改后缀配置:

    relayhost = [mailrelay]
    smtp_sasl_auth_enable = yes
    smtp_sasl_password_maps = hash:/etc/postfix/smtp_auth
    smtp_sasl_security_options = noanonymous
    
    Run Code Online (Sandbox Code Playgroud)
  • 编辑 /etc/postfix/smtp_auth

    mailrelay login:password
    
    Run Code Online (Sandbox Code Playgroud)
  • 转换成哈希格式

postmap /etc/postfix/smtp_auth

不用说只有root才能读这个…… chmod u=r,og=-


tek*_*aul 17

mailx 支持在 CLI 上设置 smtp 服务器...

echo "message" | mailx -S smtp=$smtphost:$smtpport -s "subject line" -v foo@baa.com
Run Code Online (Sandbox Code Playgroud)

无需安装任何东西,只要您的 smtp 服务器允许您发送未经身份验证的邮件。

没有一个答案可以为Linux 机器上可能拥有的所有软件位设置 smtp 服务器。每个电子邮件客户端都可以配置一个 SMTP 服务器。

  • 如果不允许未经验证的邮件怎么办?我收到客户端未通过身份验证以在邮件期间发送匿名邮件, (2认同)

Liu*_* 刘研 8

我最喜欢的是电子邮件( github ) 客户端。这真的很简单,不需要复杂的配置,不需要依赖。并且您可以通过命令行指定 smtp-server 参数,这意味着它更适合脚本编写。唯一可惜的是大部分linux发行版都没有这个工具,需要自己编译。

来自github 上电子邮件项目的引用

问:什么是“电子邮件”?

答:'email' 是我设计的一个程序,它将通过命令行将电子邮件发送到远程 smtp 服务器或在内部使用 'sendmail',并与 GNUPG 充分交互以加密和签署您的电子邮件,因此您决定这样做。 .. 您可以在以下网址获得 GNUPG:http ://www.gnupg.org

感谢cygwin让我知道这个有用的电子邮件客户端。

编译安装

./configure
make
./install.sh --version 3.1.3 --prefix /usr --mandir /usr/share/man --sysconfdir /etc
Run Code Online (Sandbox Code Playgroud)

电子邮件的命令行选项

$ email --help
Options information is as follows
email [options] recipient1,recipient2,...

    -h, -help module          Print this message or specify one of the below options
    -V, -verbose              Display mailing progress.
    -f, -from-addr            Senders mail address
    -n, -from-name            Senders name
    -b, -blank-mail           Allows you to send a blank email
    -e, -encrypt              Encrypt the e-mail for first recipient before sending
    -s, -subject subject      Subject of message
    -r, -smtp-server server   Specify a temporary SMTP server for sending
    -p, -smtp-port port       Specify the SMTP port to connect to
    -a, -attach file          Attach file and base64 encode
    -c, -conf-file file       Path to non-default configuration file
    -t, -check-config         Simply parse the email.conf file for errors
    -x, -timeout              Set socket timeout.
        -cc email,email,...   Copy recipients
        -bcc email,email,...  Blind Copy recipients
        -sign                 Sign the email with GPG
        -html                 Send message in HTML format ( Make your own HTML! )
        -tls                  Use TLS/SSL
    -m, -smtp-auth type       Set the SMTP AUTH type (plain or login)
    -u, -smtp-user username   Specify your username for SMTP AUTH
    -i, -smtp-pass password   Specify your password for SMTP AUTH
    -g, -gpg-pass             Specify your password for GPG
    -H, -header string        Add header (can be used multiple times)
        -high-priority        Send the email with high priority
        -no-encoding          Don't use UTF-8 encoding
Run Code Online (Sandbox Code Playgroud)

示例用法

指定了 SMTP 服务器的简单邮件

echo "mail body" | email -subject "unix.stackexchange.com Q36982" -from-name LiuYan?? -from-addr liuyan@domain.com -smtp-server smtp.domain.com -smtp-port 25 cwd@your-domain.com your-friends@his-domain.com
Run Code Online (Sandbox Code Playgroud)

HTML邮件

echo "<h1>header</h1><p>paragraph</p>" | email -html -subject "unix.stackexchange.com Q36982 HTML mail" cwd@your-domain.com
Run Code Online (Sandbox Code Playgroud)

附件

echo "see the attachment(s)" | email -subject "This is my email.conf file" -attach /etc/email/email.conf -attach cwd@your-domain.com
Run Code Online (Sandbox Code Playgroud)