Comment envoyer l'email de la ligne de commande de Linux

par Sukrit Dhandhania sur 1er décembre 2008

Email Linux

La ligne de commande de Linux peut être très puissante une fois que vous savez l'employer. Vous pouvez analyser des données, processus de moniteur, et faites beaucoup d'autres choses utiles et fraîches en utilisant le. Là vient souvent un besoin de produire d'un rapport et de l'expédier dehors. Il pourrait être aussi simple une condition qu'un avis que la protection du jour est passée par l'amende, ou pas. Je vous aiderai à obtenir commencé par envoyer des courrier de la ligne de commande de Linux et en manuscrits de coquille. Nous couvrirons également envoyer des attachements de la ligne de commande. Nous commencerons par la commande de « courrier ».

COURRIER

La première course un essai rapide à s'assurer l'application de « sendmail » est installée et travaillante correctement. Exécutez la commande suivante, remplaçant « you@youremailid.com » avec votre adresse d'E-mail.

# courrier - monde » you@youremailid.com de s « bonjour

Frappez la clef de retour et vous viendrez à un d'interligne. Écrivez le texte « que c'est un essai de mon serveur ». Suivi le texte en frappant la clef de retour encore. Frappez alors la combinaison principale de Control+D pour continuer. Le message de sollicitation de commande te demandera si vous voulez marquer une copie du courrier à n'importe quelle autre adresse, coup Control+D encore. Vérifiez votre boîte aux lettres. Cette commande enverra un courrier à l'identification d'email mentionnée avec le sujet, « bonjour monde ».

Pour ajouter le contenu au corps du courrier tout en courant la commande vous pouvez employer les options suivantes. Si vous voulez ajouter le texte sur vos propres :

# faites écho « ceci entrera dans le corps du courrier. » | courrier - monde » you@youremailid.com de s « bonjour

Et si vous voulez que le courrier lise le contenu à partir d'un dossier :

# courrier - monde » you@youremailid.com de s « bonjour

Quelques autres options utiles dans la commande de courrier sont :

- sujet de s (Le sujet du courrier)
- email address de c (Marquez une copie à cet « email address », ou le cc)
- email address de b (Marquez une copie au carbone sans visibilité à cet « email address », ou BCC)

Voici comment vous pourriez employer ces options :

# faites écho la « bienvenue au monde de Calvin n Hobbes » | courrier - monde » calvin@cnh.com de s « bonjour - c hobbes@cnh.com - b susie.derkins@cnh.com

CHIEN

Un d'inconvénients principaux d'employer la commande de courrier est qu'il ne soutient pas l'envoi des attachements. le chien, d'une part, le soutient. J'ai trouvé ce dispositif particulièrement utile pour les manuscrits qui produisent des rapports ou des protections non-textuels qui sont relativement petits dans la taille que je voudrais à la protection ailleurs. Of course, mutt allows you to do a lot more than just send attachments. It is a much more complete command line mail client than the “mail” command. Right now we’ll just explore the basic stuff we might need often. Here’s how you would attach a file to a mail:

# echo “Sending an attachment.” | mutt -a backup.zip -s “attachment” calvin@cnh.com

This command will send a mail to calvin@cnh.com with the subject (-s) “attachment”, the body text “Sending an attachment.”, containing the attachment (-a) backup.zip. Like with the mail command you can use the “-c” option to mark a copy to another mail id.

SENDING MAIL FROM A SHELL SCRIPT

Now, with the basics covered you can send mails from your shell scripts. Here’s a simple shell script that gives you a reading of the usage of space on your partitions and mails the data to you.

#!/bin/bash
df -h | mail -s “disk space report” calvin@cnh.com

Save these lines in a file on your Linux server and run it. You should receive a mail containing the results of the command. If, however, you need to send more data than just this you will need to write the data to a text file and enter it into the mail body while composing the mail. Here’s and example of a shell script that gets the disk usage as well as the memory usage, writes the data into a temporary file, and then enters it all into the body of the mail being sent out:

#!/bin/bash
df -h > /tmp/mail_report.log
free -m >> /tmp/mail_report.log
mail -s “disk and RAM report” calvin@cnh.com

Now here’s a more complicated problem. You have to take a backup of a few files and mail then out. First the directory to be mailed out is archived. Then it is sent as an email attachment using mutt. Here’s a script to do just that:

#!/bin/bash
tar -zcf /tmp/backup.tar.gz /home/calvin/files
echo | mutt -a /tmp/backup.tar.gz -s “daily backup of data” calvin@cnh.com

The echo at the start of the last line adds a blank into the body of the mail being set out.

This should get you started with sending mails form the Linux command line and from shell scripts. Read up the “man page” for both mail and mutt for more options.

Related Posts:
  • How to set up Evolution for email
  • Some useful Linux bash tricks
  • How to monitor your Linux machine with netstat
  • How to set the date on your Linux machine
  • Linux command line magic - find and replace
  • Get Simple Help tutorials just like this one in your email inbox every day - for free! Just enter your email address below:

    You can always opt out of this email subscription at any time.


    Bookmark and Share

    { 3 comments… read them below or add one }

    1 Michal 12.08.08 at 3:20 am

    You may want to have a look at smtp-client.pl. Check out the usage examples. I’d say it’s the ultimate command line smtp client, but I’m a little biased ;-)

    2 Matt 01.21.09 at 11:24 pm

    echo | mutt -a -s /tmp/backup.tar.gz ?daily backup of data? calvin@cnh.com

    should be:

    echo | mutt -a /tmp/backup.tar.gz -s ?daily backup of data? calvin@cnh.com

    3 Ross McKillop 01.22.09 at 12:47 am

    Matt -

    Thanks very much for catching that! I’ve made the correction. Cheers!

    Leave a Comment

    You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>