19

I have installed ubuntu on my server. I want the cronjobs to send me emails for output.

How can i send emails from terminal . which thing i have to install and how will i put smtp or pop setting in that program

6 Answers 6

13

I believe mailx is what you're looking for.

sudo apt-get install bsd-mailx 

This will also install postfix at a minimum, and will give you a few options to set postfix up. If you have an SMTP server on your LAN, choose 'satellite system', then enter the mail domain name, and lastly the IP address of your SMTP server.

To use:

echo $MESSAGE_BODY | /bin/mail -s "$SUBJECT" "$RECIPIENT_ADDRESS"

You can also use a file for the body:

/bin/mail -s "$SUBJECT" "$RECIPIENT_ADDRESS" < /tmp/message.txt
6
  • It says missing postfix/main.cf file , how can i download that file
    – John
    Apr 27, 2010 at 6:15
  • When you installed mailx it should have run through the install screens for postfix. Did it ask you which type of install, and for the mail name and relay host? And when does it give you the error, when you run /bin/mail or when you tried to install?
    – nedm
    Apr 27, 2010 at 6:21
  • it didn't ask me anything, how can uninstall it and do that again
    – John
    Apr 27, 2010 at 6:28
  • If you have postfix set up as part of another install it sounds like it may not be configured correctly. Try "sudo dpkg-reconfigure postfix" and then "sudo /etc/init.d/postfix reload"
    – nedm
    Apr 27, 2010 at 6:28
  • If postfix isn't installed, then "sudo apt-get remove mailx" and then "sudo apt-get install mailx" and it should install postfix with the mailx package.
    – nedm
    Apr 27, 2010 at 6:31
7

The ssmtp package is popular for an easily way to send email from an ubuntu box. Here is a tutorial to set it up to use gmail.

4
  • And far easier/quicker to get up and running than postfix.
    – hometoast
    Apr 29, 2010 at 16:13
  • ssmtp continues to be the best mail agent by far for use in web servers, because it's only configuration is how to connect to an SMTP server. It saves so much time when compared to configuring postfix, especially if you haven't tried it before. It depends on the use case, however, and mailx might be easier to use if you're sending e-mail from the command line :) Jan 25, 2017 at 10:35
  • 1
    Link is broken, Ubuntu has a guide for setting up ssmtp.
    – Nattgew
    Nov 30, 2017 at 15:24
  • Can be combined with bsd-mailx if a mail command is needed. Far easier to set up than configuring postfix.
    – JanKanis
    Nov 9, 2022 at 15:36
4

If you do not wish to mess around with configuring Postfix (which can be an awful pain) checkout the heirloom-mailx package (sudo apt-get install heirloom-mailx).

This is an alternative version of the mail command that lets you specify an external SMTP server. For simple cron scripts it is ideal.

Homepage: http://heirloom.sourceforge.net/mailx.html

Man page: http://heirloom.sourceforge.net/mailx/mailx.1.html

2

Install postfix and mailx

apt-get install postfix mailx

1

I use a standard setup in my shop, and I find mailutils package perfect, for doing things like sending auto emails from cronjobs etc. Very simple to set up through a ncurses config helper.

sudo aptitude install mailutils 
sudo dpkg-reconfigure exim4-config

The option internet site; mail is sent and received directly using SMTP works best for me (as I want to send for the most part). You can also set your root forward address through this config.

You can then send emails using the command line (someone above gave examples) but I use:

mail -s "AVScan completed on HOSTNAME" avnotifications@somedomain.com

Hope this helps.

1

Latest installation i did was on Ubuntu 11.10 with command:

sudo apt-get install postfix mailutils

And if you want to read more on postfix, how it works and how to test it try : PostfixBasicSetupHowto

You could then use the mail command in the cronjob. See the command's man pages for options and usage.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy