send mail from linux terminal in one line

LinuxEmail

Linux Problem Overview


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

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 java program. The idea is that I will use Runtime.getRuntime()… etc. to send the mail command from my java program.

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

Linux Solutions


Solution 1 - Linux

mail can represent quite a couple of programs on a linux system. What you want behind it is either sendmail or postfix. I recommend the latter.

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 user@gmail.com

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.

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

Solution 2 - Linux

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

echo "This is the body" | mail -s "This is the subject" user@gmail.com

Solution 3 - Linux

echo "Subject: test" | /usr/sbin/sendmail user@domain.com

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.

Solution 4 - Linux

For Ubuntu users: First You need to install 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.

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:

Turn on Access for less secure apps from here

Solution 5 - Linux

You can also use sendmail:

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

Solution 6 - Linux

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

For Ubuntu -:

$ sudo apt-get install -y mailutils

For CentOs-:

$ sudo yum install -y mailx

Test Mail command-:

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

Solution 7 - Linux

Sending Simple Mail:
$ mail -s "test message from centos" recipient@example.com
hello from centos linux command line

Ctrl+D to finish

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestioncHamView Question on Stackoverflow
Solution 1 - LinuxPSkocikView Answer on Stackoverflow
Solution 2 - LinuxrichardsonView Answer on Stackoverflow
Solution 3 - LinuxHengjieView Answer on Stackoverflow
Solution 4 - LinuxRohit GuptaView Answer on Stackoverflow
Solution 5 - Linuxmti2935View Answer on Stackoverflow
Solution 6 - LinuxArvind Kumar RawatView Answer on Stackoverflow
Solution 7 - LinuxabhilashView Answer on Stackoverflow