Sending mail from the command line on Ubuntu

First install exim4 and exim4-config using the package manager
then install mailx

sudo apt-get install mailx

then type

sudo dpkg-reconfigure exim4-config

and follow the prompts

once this is set up use the command “mail” to check your mail.

if you would like to send mail type “mail” followed by the email address to mail it to eg


enter the subject and press <enter>

enter the body and keep pressing enter as many times as you need to, when your body is finished, press “Ctrl” and “d” together this will prompt you for a cc etc and finally the mail will be sent.

Remember that if your domain name is not qualified eg (machine is not a web site with a domain name) this will not work. I can only get this to work on servers that are hosting content and have a qualified domain name, if anyone knows how to get this to work on a home pc without a qualified DNS name please let me know.


9 thoughts on “Sending mail from the command line on Ubuntu

  1. OK got it working on a machine without a qualified domain name (not a web server, with its own hostname and external DNS name etc)
    when you sudo dpkg-reconfigure exim4-config put your isp’s name in as the system mail name/mail name (domain name used to qualify mail addresses without a domain name.
    for example

  2. Pingback: Setting up a cron job to update Ubuntu machine « RUBRIC Tech Team

  3. I found that I needed to send email from this machine to email domains that were off campus. The sending of emails was possible with the above config for internal emails only (behind gateway, LAN etc)
    To get external emails working I ran the sudo dpkg-reconfigure exim4-config again but this time chose “mail sent by smarthost; received via SMTP or fetchmail” from the Mail Server configuration menu. I then put in the SMTP Gateway Server address when prompted for the address of the smart host. I pretty much left everything else as default or blank and it worked.

  4. Hello Tech Team,

    I found this thread and, although I know it is a bit old, I hope someone is listening!

    I am having a bit of trouble understanding what to do when “following the prompts” in the “sudo dpkg-reconfigure exim4-config” step.

    Firstly, here is my objective: I want to be able to cron a script which sends an email message automatically. I am not setting up a mail server, I just want to send a short email message from via a bash script. I want to use my ISP’s SMTP server to actually send the mail.

    I am a relative Linux newbie so please bear with me. I am also not a Linux developer – I am a mere user and some terminology and jargon might be over my head so please be gentle. Here are my questions, in order of the screens that display in dpkg-reconfigure exim4-config:

    1. One of the opening menu items is “mail sent by smarthost; no local mail”. What does that mean? What is a “smarthost”? I want to deliver mail to a remote SMTP server provided by my ISP. Is that the “smarthost”? At first glance from a dummy, this seems to be the right item so I select it.

    2.Now its asking me for the “mail name”. What’s that? The explanation isn’t very clear. The default name that comes up is my computer’s name. Really? THIS is an address? Should I replace the text with my email address, the ISP’s SMTP server, or what? Do I just put “” or whatever there? And what “other programs” will be using this value?

    3. What are the I don’t care about having my computer “listen for connections”. I’m not serving anything but my own commands. So, do I put here? Can I write “localhost” or must I put in the numbers?

    4. What is meant by “recipient domains”? Should I just leave this blank?

    5. What is meant by the “Visible domain name for local users”? Do I put in here the name of the ISP? Do I just leave it blank?

    6. Here’s a question about the host name of the “smarthost”. Is that the SMTP server; e.g. “”?

    7. Since I only ever send and don’t expect to be receiving, should I just leave “Root and postmaster mail recipient” blank?

    Thanks for your patience.

    Allan M.

  5. Hi Allan
    Yes I am listening, I hope I can be of service.
    I will do my best to explain how to do what you are trying to accomplish. I am going to explain as much as I can please excuse me if I am covering ground that you already know.
    Creating a Cron job…
    To create a Cron job I type “sudo crontab -e” (e is for edit)
    If I add the following line when the cron editor loads, it will send an email with the output from what ever script is programmed to run, based on the scheduling (minute “m” , hour “h”… etc). The important part is that you have the as the first line.
    #m h dom mon dow command
    01 20 * * * /home/myuser/
    More to come…

  6. When running the sudo dpkg-reconfigure exim4-config command I choose the following options
    Mail sent by smart host; …..
    For the mail name I put the base URL of my internet service provider eg “”
    For the “addresses to listen on for smtp connections” I just put
    For “other destinations for which mail is accepted I just put the hostname of my machine”
    ***NOTE: check that firstly the hostname is set by typing “hostname” at the command prompt. If it is not correct the type “hostname thehostname” eg “hostname homecomputer”. You will also have to make sure that the /etc/hosts/ file is correct, eg homecomputer localhost
    For the “Machines to relay mail for: ” I leave this blank
    For the “IP address or host name of the outgoing smarthost” I put in the credentials of my ISP in there. Eg Just google “outgoing mail server” and add the name of your ISP (internet service provider) eg Google this “tpg outgoing mail server” You should get a result like “”
    For the rest of the questions I just hit enter (accept default values).
    More to come…

  7. Hallo, I hope you are listening here as well ;)

    Is this configuration supposed to work in an LAN scenario, with 1 mail server running i.e. postfix?

  8. hi i am not able to install mailx in ubuntu i tried sudo apt-get install mailx and sudo dpkg-reconfigure exim4-config its not working can anyone help me on it please.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s