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.
De temps en temps, je me trompe quand j'essaie d'envoyer un email par la ligne de commade via un script ou une application. Une tâche qui paraît si simple comme le changement de l'adresse de l'expéditeur peut parfois demander beaucoup de temps pour y arriver. Ce qui suit va vous rapprocher, avec un peu de chance, d'une solution un peu plus rapide.
Même si vous avez installé un MTA (Mail Transfert agent - Agent de transfert du courrier) comme l'excellent Postfix (http://www.postfix.org), vous devriez avoir besoin de modifier l'adresse mail de l'expéditeur (ou la ligne « De ») pour refléter que votre mail vient d'un script particulier, sur une machine spécifique et pas simplement du trop habituel « root@localhost ».
Passons à l'utilitaire « mail ». Il y a des multiples versions historiques et des retours en arrière de l'utilitaire « mail » mais ce sujet sera pour une autre fois. Si vous utilisez un dérivé de Red Hat, vous feriez mieux, dans ce cas, d'essayer d'installer « mailx » ou « nail » pour disposer de la pleine fonctionnalité suivante, mais sachez que les résultats peuvent diverger. Cependant, sur Ubuntu et Debian (sur la plupart des versions), ça devrait marcher avec la paquet « heirloommailx ». Installez-le simplement, comme ceci :
aptget install heirloommailx
Passons un moment dans l'analyse de cet exemple de ligne de commande chargée :
mailx r 'mycustomsender@chrisbinnie.tld' s 'Your Subject Line' a '/fullpath/myattachment' S 'smtp=localhost' 'destination@chrisbinnie.tld'
Regardons la d'un peu plus près. Vous pouvez facilement changer l'option «r » pour configurer quelle ligne « De » est présentée au client mail quand le mail est récupéré à l'autre bout. C'est parfois étonnamment difficile d'y arriver avec d'autres clients en ligne de commande.
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.
De même, les autres options sont compréhensibles, je l'espère. L'option « s » vous permet d'éditer le sujet de votre mail alors que l'option « a » est le nom du fichier que vous ajoutez en pièce jointe, à partir de votre système de fichiers local. Un conseil pratique est de compresser le fichier en si le texte brut arrive avec des retours chariot supplémentaires non-désirés. , Incidemment, vous pourriez aussi vous assurer que vous utilisez le chemin complet vers le fichier, si vous avez des problèmes.
Prenez soin de noter que l'exemple de ligne de commande ci-dessus n'est pas tout-à-fait complet et peut , soit être terminé avec succès par « < /dev/null » à la fin de celui-ci, ou autrement, en tapant un point puis en appuyant sur Entrée. Ceci sert à remplir le corps du mail avec du contenu, même si le contenu est inexistant et qu'effectivement, c'est un mail vide.
Comparez et opposez l'exemple en ligne de commande du dessus et celui ci-dessous ; ici, notre mail vide n'a pas du tout de corps de texte, à cause du « /dev/null » qu'il contient :
mailx r 'mycustomscript@chrisbinnie.tld' s 'Another Subject Line' a '/fullpath/yourfile‘ S 'smtp=remotesmartMTAhost' 'destination@chrisbinnie.tld' < /dev/null
Je vous précise ceci car vous pourriez aussi insérer un fichier texte dans le corps du message en remplaçant « /dev/null » par quelque chose comme cet exemple « /home/chrisbinnie/bodytext ». Le fichier « bodytext » est le corps du message dans ce cas.
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.