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

Matt Browne
  • 153
  • 6

6 Answers6

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
nedm
  • 5,630
  • 5
  • 32
  • 52
  • It says missing postfix/main.cf file , how can i download that file –  Apr 27 '10 at 06: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 '10 at 06:21
  • it didn't ask me anything, how can uninstall it and do that again –  Apr 27 '10 at 06: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 '10 at 06: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 '10 at 06:31
  • What version of Ubuntu are you using? You could also create a python script to send the email, and call it from your cron job. See the following for some email examples in python: http://docs.python.org/library/email-examples.html – nedm Apr 27 '10 at 07:11
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.

Kyle Brandt
  • 83,619
  • 74
  • 305
  • 448
  • And far easier/quicker to get up and running than postfix. – hometoast Apr 29 '10 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 :) – Steen Schütt Jan 25 '17 at 10:35
  • 1
    Link is broken, Ubuntu has a [guide](https://help.ubuntu.com/community/EmailAlerts) for setting up ssmtp. – Nattgew Nov 30 '17 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 09 '22 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

David Harrison
  • 441
  • 2
  • 5
2

Install postfix and mailx

apt-get install postfix mailx

Niko Gunadi
  • 123
  • 4
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.

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.

Rqomey
  • 1,055
  • 9
  • 25