一行从linux终端发送邮件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20318770/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-07 01:30:10  来源:igfitidea点击:

send mail from linux terminal in one line

linuxemail

提问by cHam

I know there is the command mailin linux to send emails via command line. How can I send an simple email with one line from the terminal though?

我知道maillinux 中有通过命令行发送电子邮件的命令。但是,如何从终端用一行发送简单的电子邮件?

For example:

例如:

mail [email protected] [subject] [body]

And have the email sent without any confirmation or prompts to the user?

是否在没有任何确认或提示的情况下向用户发送了电子邮件?

The reason is, I want to send a brief message via email to myself when a specific event happens in a javaprogram. The idea is that I will use Runtime.getRuntime()… etc. to send the mail commandfrom my javaprogram.

原因是,当java程序中发生特定事件时,我想通过电子邮件向自己发送一条简短的消息。这个想法是我将使用Runtime.getRuntime()...等mail command从我的java程序中发送。

I used cronto do something similar in the past, but the current implementation doesn't use cron, so I need to try this out instead.

我过去曾经cron做过类似的事情,但当前的实现不使用cron,所以我需要尝试一下。

采纳答案by PSkocik

mailcan represent quite a couple of programs on a linux system. What you want behind it is either sendmailor postfix. I recommend the latter.

mail在 Linux 系统上可以代表相当多的程序。您想要的是sendmailpostfix。我推荐后者。

You can install it via your favorite package manager. Then you have to configure it, and once you have done that, you can send email like this:

你可以通过你最喜欢的包管理器安装它。然后你必须配置它,一旦你完成了,你可以像这样发送电子邮件:

 echo "My message" | mail -s subject [email protected]

See the manual for more information.

有关更多信息,请参阅手册。

As far as configuring postfix goes, there's plenty of articles on the internet on how to do it. Unless you're on a public server with a registered domain, you generally want to forward the email to a SMTP server that you can send email from.

就配置 postfix 而言,互联网上有很多关于如何进行配置的文章。除非您在具有注册域的公共服务器上,否则您通常希望将电子邮件转发到可以从中发送电子邮件的 SMTP 服务器。

For gmail, for example, follow http://rtcamp.com/tutorials/linux/ubuntu-postfix-gmail-smtp/or any other similar tutorial.

例如,对于 gmail,请遵循 http://rtcamp.com/tutorials/linux/ubuntu-postfix-gmail-smtp/或任何其他类似教程。

回答by richardson

You can use an echo with a pipe to avoid prompts or confirmation.

您可以使用带有管道的 echo 来避免提示或确认。

echo "This is the body" | mail -s "This is the subject" [email protected]

回答by mti2935

You can also use sendmail:

你也可以使用sendmail:

/usr/sbin/sendmail [email protected] < /file/to/send

回答by Hengjie

echo "Subject: test" | /usr/sbin/sendmail [email protected]

This enables you to do it within one command line without having to echo a text file. This answer builds on top of @mti2935's answer. So credit goes there.

这使您可以在一个命令行中完成此操作,而无需回显文本文件。这个答案建立在@mti2935 的答案之上。所以信用就在那里。

回答by abhilash

Sending Simple Mail:

发送简单邮件:

$ mail -s "test message from centos" [email protected]
hello from centos linux command line

Ctrl+Dto finish

Ctrl+D结束

回答by Arvind Kumar Rawat

You can install the mail package in Ubuntu with below command.

您可以使用以下命令在 Ubuntu 中安装邮件包。

For Ubuntu -:

对于 Ubuntu -:

$ sudo apt-get install -y mailutils

For CentOs-:

对于 CentOs-:

$ sudo yum install -y mailx

Test Mail command-:

测试邮件命令-:

$ echo "Mail test" | mail -s "Subject" [email protected]

回答by Rohit Gupta

For Ubuntu users: First You need to install mailutils

对于 Ubuntu 用户:首先你需要安装 mailutils

sudo apt-get install mailutils

Setup an email server, if you are using gmail or smtp. follow this link. then use this command to send email.

如果您使用的是 gmail 或 smtp,请设置电子邮件服务器。按照这个链接。然后使用此命令发送电子邮件。

echo "this is a test mail" | mail -s "Subject of mail" [email protected]

In case you are using gmail and still you are getting some authentication error then you need to change setting of gmail:

如果您正在使用 gmail 并且仍然遇到一些身份验证错误,那么您需要更改 gmail 的设置:

Turn on Access for less secure apps from here

从这里打开安全性较低的应用程序的访问权限