Ceci est une ancienne révision du document !
Every now and again, I trip myself up when I’m trying to send outbound email from the command-line via a script or an application. A seemingly simple task such as changing the sender’s email address can sometimes take lots of time to get working. What follows will hopefully get you closer to a solution a little quicker.
Even if you have an MTA (Mail Transfer Agent) like the excellent Postfix (http://www.postfix.org) installed, you might need to alter the sender email address (or the “from line”) to reflect that your email is coming from a specific script, on a specific machine and not just the all-too-familiar “root@localhost”.
Step forward the “mail” utility. There are multiple historical versions and throwbacks of the “mail” utility but that discussion is for another day. If you’re using a Red Hat derivative, then you might be better trying to install “mailx” or “nail” to achieve the following functionality, but beware that your results may differ. However, on Ubuntu and Debian (on most versions), you should be fine with the “heirloommailx” package. Install it simply, as follows:
aptget install heirloommailx
Consider for a moment this busy command-line example:
mailx r 'mycustomsender@chrisbinnie.tld' s 'Your Subject Line' a '/fullpath/myattachment' S 'smtp=localhost' 'destination@chrisbinnie.tld'
Let’s look a little closer. You can easily change the “r” option to configure which “from line” is presented to the mail client when the email is picked up at the other end. This is sometimes surprisingly difficult to get working with other command-line mail clients.
The other options hopefully make sense too. The “s” option allows you to edit your email’s subject whereas the “a” option is the filename of the attachment that you are adding from your local filesystem. One handy hint is to compress the file if the plain text arrives with extra, unwanted carriage returns. Incidentally you should also ensure that you’re using the full path to the attached file if you have issues.
Take careful note that the above command-line example isn’t strictly complete and can either be terminated successfully with “< /dev/null” appended to the end of it or alternatively by manually typing a dot and then hitting the “Enter” key. This is to populate the body of the email with some content, even if the content is nonexistent and effectively it’s an empty email.
Compare and contrast the command-line example above and the one below; here our empty email has no body at all thanks to “/dev/null” populating it:
mailx r 'mycustomscript@chrisbinnie.tld' s 'Another Subject Line' a '/fullpath/yourfile‘ S 'smtp=remotesmartMTAhost' 'destination@chrisbinnie.tld' < /dev/null
I’m pointing this out because you could also insert a text file into the body by replacing “< /dev/null” with something like this example “< /home/chrisbinnie/bodytext”. The file “bodytext” is the email’s body in this case.
Consider this scenario now for a second: you don’t want to insert a whole file but instead send some other text directly from your command-line. You can also follow this now familiar format by echoing text through a pipe:
echo e “Text content goes here.\n And here.” | mailx r 'script@binnie.tld' s 'Subject Line' a '/fullpath/.bashrc' S 'smtp=localhost' 'chris@binnie.tld'
I haven’t yet explained that the “S” option lets you choose a remote MTA, like “smtp=smtp.mail.com” as an example, or a local machine (as we have seen in our command-line examples above).
Note that it’s easy to add multiple email addresses for the recipients (simply separating them by spaces).
Before we stop looking at command-line email, let’s quickly think about an alternative to solving the problem of changing the “from line” or sender name. It’s not too tricky (I’m sure you’ll be glad to read).
Another command-line email client option is to install “mutt” in case you can’t get your hands on a “mailx” derivative. In order to use it, you have to edit the “.muttrc” file, found in the home directory of the user who is sending the email. Here’s an example:
set realname=“Alerting Script” set from=“script@productionmachine.com” set use_from=yes
In the “.muttrc” text above, the “realname” option would usually be a person’s name; clearly it can be useful to set it – to tidy up emails as they are received.
There’s lots more information on the superb Mutt (http://dev.mutt.org/trac/wiki/MuttFaq/Header), and it’s clever enough to achieve everything we’ve covered with the “mail” command.
Personally, I prefer the “mail“ command for this task, but, hopefully, you’re now armed sufficiently so that you can choose between the two alternatives above.