Outils pour utilisateurs

Outils du site


issue115:tuto1

Ceci est une ancienne révision du document !


Every now and again, I trip myself up when I’m trying to send outbound e­mail from the command-line via a script or an application. A seemingly simple task such as changing the sender’s e­mail 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 e­mail address (or the “from line”) to reflect that your e­mail 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 “heirloom­mailx” package. Install it simply, as follows:

apt­get install heirloom­mailx

Consider for a moment this busy command-line example:

mailx ­r 'my­custom­sender@chrisbinnie.tld' ­s 'Your Subject Line' ­a '/fullpath/my­attachment' ­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 e­mail 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 e­mail’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 e­mail with some content, even if the content is non­existent and effectively it’s an empty e­mail.

Compare and contrast the command-line example above and the one below; here our empty e­mail has no body at all thanks to “/dev/null” populating it:

mailx ­r 'my­custom­script@chrisbinnie.tld' ­s 'Another Subject Line' ­a '/fullpath/your­file‘ ­S 'smtp=remote­smart­MTA­host' '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 e­mail’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 e­mail addresses for the recipients (simply separating them by spaces).

Before we stop looking at command-line e­mail, 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 e­mail 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 e­mail. Here’s an example:

set realname=“Alerting Script” set from=“script@production­machine.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 e­mails 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.

issue115/tuto1.1480407141.txt.gz · Dernière modification : 2016/11/29 09:12 de d52fr