mailx smtp_如何在Linux中使用Gmail的SMTP从mailx / s-nail命令发送电子邮件

mailx smtp

The heirloom mailx (or s-nail if you are using Ubuntu 18 or later or similar releases) command in Linux is still providing service for guys like me, especially when we need to send email automatically by script. Gmail is great. Now, how to use gmail’s smtp in mailx/mail? gmail is a little special since gmail’s smtp server requires tls authorization. The good news is that mailx supports it. Let’s look at how to use it.

传家宝的mailx (或S-钉子如果您正在使用Ubuntu 18或更高版本或类似版本)命令的Linux仍然是对球员和我一样提供服务,特别是当我们需要通过脚本自动发送电子邮件。 Gmail很棒。 现在,如何在mailx / mail中使用gmail的smtp? gmail有点特殊,因为gmail的smtp服务器需要tls授权。 好消息是mailx支持它。 让我们看看如何使用它。

There are two ways to do this: using all-in-one command or putting configurations into profile. The all-in-one-command way needs no other configurations except the command line itself, while the way using configuration has a clearer command.

有两种方法可以执行此操作:使用多合一命令或将配置放入配置文件中。 “多合一命令”方式除命令行本身外,无需其他配置,而使用配置的方式具有更清晰的命令。

多合一命令 (All-in-one command)

mailx accepts most configuration in the command line directly from the -S options in format -S variable[=value] (check more in mailx manual).

mailx直接从命令行中的-S选项以-S variable[=value]格式接受大多数配置(请在mailx手册中查看更多mailx )。

Sets the internal option variable and, in case of a string option, assigns value to it. Note, that when setting from variable, domain name of host is automatically added if value does not contain any. If you want to enter from address with owner’s name, you can use, for example, following format: -S "from=System User "

设置内部选项变量,如果是字符串选项,则为其赋值。 请注意,从变量进行设置时,如果值不包含主机域名,则会自动添加主机域名。 如果要使用所有者名称从地址输入,则可以使用例如以下格式:-S“ from = System User ”

This is an all-in-one command that sends email to $TO_EMAIL_ADDRESS

这是将电子邮件发送到$ TO_EMAIL_ADDRESS的多合一命令

(use `s-nail` instead in releases without the heirloom mailx)

(在没有传家宝mailx的版本中使用`s-nail`)

$ mailx -v -s "$EMAIL_SUBJECT" \
-S smtp-use-starttls \
-S ssl-verify=ignore \
-S smtp-auth=login \
-S smtp=smtp://smtp.gmail.com:587 \
-S from="$FROM_EMAIL_ADDRESS($FRIENDLY_NAME)" \
-S smtp-auth-user=$FROM_EMAIL_ADDRESS \
-S smtp-auth-password=$EMAIL_ACCOUNT_PASSWORD \
-S ssl-verify=ignore \
$TO_EMAIL_ADDRESS

Replace the `$…` above with the value that is actually used.

将上面的$$替换为实际使用的值。

This command will ask for the email content. Type in the mail content and after finishing the email, use “Ctrl+d” to tell mailx you have finished. Then this mail will be sent out through gmail’s smtp server. You can also use pipe like this:

该命令将询问电子邮件内容。 输入邮件内容,并在完成电子邮件后,使用“ Ctrl + d”告诉mailx您已完成。 然后,这封邮件将通过gmail的smtp服务器发送出去。 您也可以像这样使用管道:

echo "The mail content" | mailx -v -s ...


####Security notes on passwords####


####密码安全注意事项####

* you may consider not setting `-S smtp-auth-password` and input it each time you use this command. Passwords in the command line can be seen by other users on the system.
* Gmail now supports [“App Password”](https://support.google.com/accounts/answer/185833?hl=en) if you are using Google’s 2-Step verification and you will need to use it instead of your Gmail’s password. You can generate an “App Password” for `mailx` so it can be easily revoked in case it leaked out. Go to this URL: https://myaccount.google.com/apppasswords . Click on “Select App” → “Other”, type “mailx”, and hit “Generate”. You will get a password used by `mailx`. (thanks to [Ben](https://www.systutorials.com/1411/sending-email-from-mailx-command-in-linux-using-gmails-smtp/#comment-159161) for the notes).

*您可能会考虑不设置`-S smtp-auth-password`并在每次使用此命令时输入它。 系统上的其他用户可以看到命令行中的密码。
*如果您使用的是Google的两步验证,则Gmail现在支持[“应用密码”]( https://support.google.com/accounts/answer/185833?hl = zh_CN),并且您需要使用它代替您的Gmail的密码。 您可以为“ mailx”生成一个“应用密码”,以便在泄漏时可以轻松地将其撤消。 转到以下网址:https://myaccount.google.com/apppasswords。 单击“选择应用程序”→“其他”,键入“ mailx”,然后单击“生成”。 您将获得`mailx`使用的密码。 (感谢[Ben](https://www.systutorials.com/1411/sending-email-from-mailx-command-in-linux-using-gmails-smtp/#comment-159161))。

####About nssdb used by `mailx`####

关于 mail#使用的nssdb ####

Most Linux system’s default nssdb should be already updated so that gmail is good with it. But if your system does not have an updated nssdb, you may consider using Firefox’s instead by adding such option

大多数Linux系统的默认nssdb应该已经更新,以便gmail可以配合使用。 但是,如果您的系统没有更新的nssdb,则可以考虑使用Firefox,而不是添加此类选项

-S nss-config-dir=~/.mozilla/firefox/yyyyyyyy.default/

Find out Firefox’s profile directory in the home directory which commonly has a format like this:

在主目录中找到Firefox的配置文件目录,该目录通常具有以下格式:

~/.mozilla/firefox/yyyyyyyy.default

yyyyyyyy is a random string that’s different for different users. You can easily find it out by looking into the directory ~/.mozilla/firefox.

yyyyyyyy是一个随机字符串,对于不同的用户而言是不同的。 您可以通过查看目录〜/ .mozilla / firefox轻松找到它。

##Use mailx configuration file##

##使用mailx配置文件##

There are too many options in the above command? Yes… I must confess so. We can write most of them into mailx/mail’s configuration file ~/.mailrc

上面的命令中有太多选项? 是的……我必须承认。 我们可以将大多数文件写入mailx / mail的配置文件〜/ .mailrc中

set smtp-use-starttls
set ssl-verify=ignore
set smtp=smtp://smtp.gmail.com:587
set smtp-auth=login
set smtp-auth-user=$FROM_EMAIL_ADDRESS
set smtp-auth-password=$EMAIL_ACCOUNT_PASSWORD
set from="$FROM_EMAIL_ADDRESS($FRIENDLY_NAME)"

For better security, you may set the mode of `~/.mailrc` to 600 by `chmod 600 ~/.mailrc`.

为了获得更好的安全性,您可以通过“ chmod 600〜/ .mailrc”将“〜/ .mailrc”的模式设置为600。

The variables set are the same as those used in the “all in one command”.

设置的变量与“多合一命令”中使用的变量相同。

When sending mails, use this command:

发送邮件时,请使用以下命令:

$ mailx -v -s "$EMAIL_SUBJECT" $TO_EMAIL_ADDRESS

Or

要么

$ echo "The mail content" | mail -v -s "$EMAIL_SUBJECT" $TO_EMAIL_ADDRESS

Then, time to enjoy it!

然后,享受时间!

翻译自: https://www.systutorials.com/sending-email-from-mailx-command-in-linux-using-gmails-smtp/

mailx smtp

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值