170

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

cHam
  • 2,624
  • 7
  • 26
  • 28

7 Answers7

232

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.

Petr Skocik
  • 58,047
  • 6
  • 95
  • 142
  • 4
    Postfix is powerful but if you only need to send email try "ssmtp". This package is smaller and doesn't run a daemon like postfix. It supports secure protocols and works with gmail. – oᴉɹǝɥɔ Dec 16 '15 at 15:15
  • 8
    "The program 'mail' is currently not installed. You can install it by typing: apt-get install mailutils" – Tom Feb 01 '16 at 03:09
  • more light-weight: packages `nullmailer` plus `bsd-mailx`. Sufficient for most work-stations :) And as `nullmailer` includes a `sendmail` interface, you might even skip the `bsd-mailx` package and use [Hengjie's approach](https://stackoverflow.com/a/26865403/2533433). – Izzy Mar 16 '17 at 21:30
  • 2
    @oᴉɹǝɥɔ [`ssmtp` is deprecated.](https://wiki.archlinux.org/index.php/SSMTP) Use `msmtp` instead. – Mattwmaster58 May 19 '18 at 00:45
  • mail -s "Your subject" dunn@mycompany.com <<< "body message goes here." (from source: https://www.javatpoint.com/linux-mail-command#:~:text=Linux%20mail%20command%20is%20a,shell%20scripts%20or%20web%20applications.) – Dung Oct 06 '20 at 03:08
  • echo "Subject: sendmail test" | sendmail -f FROM_EMAIL_ADDRESS -v TO_EMAIL_ADDRESS – Mihir Bhatt Dec 23 '21 at 11:16
41
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.

Drew Dormann
  • 59,987
  • 13
  • 123
  • 180
Hengjie
  • 4,598
  • 2
  • 30
  • 35
  • 3
    Just to add to the answer for the subject and body: echo -e "Subject: test \n\n Body content here\n" | sendmail user@domain.com – emvidi Dec 14 '17 at 19:14
40

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
richardson
  • 456
  • 3
  • 4
15

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" username@domain.com

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

TheLethalCoder
  • 6,668
  • 6
  • 34
  • 69
Rohit Gupta
  • 443
  • 3
  • 17
14

You can also use sendmail:

/usr/sbin/sendmail user@domain.com < /file/to/send
mti2935
  • 11,465
  • 3
  • 29
  • 33
  • Note: I have Postfix+S-Nail . This is what worked for me. First I used "Rohit Gupta" answer and downloaded the mail utilities, then did yours and everything worked. – Mohammed Baashar Jun 12 '19 at 13:29
6

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" youremail@domain.com
-2

Sending Simple Mail:

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

Ctrl+D to finish

Leon Adler
  • 2,993
  • 1
  • 29
  • 42