Hoe te om e-mail van de Linux bevellijn te verzenden

door Sukrit Dhandhania op 1 december, 2008

E-mail Linux

De Linux bevellijn kan zeer krachtig zijn zodra u het weet hoe te om het te gebruiken. U kunt gegevens ontleden, processen controleren, en heel wat andere nuttige en koele dingen doen gebruikend het. Er komt vaak een behoefte om een rapport te produceren en het uit te posten. Het zou een zo eenvoudig vereiste zoals een bericht dat kunnen zijn de steun van de dag door boete ging, of niet. Ik zal u helpen begonnen worden met het verzenden van post van de Linux bevellijn en in shell manuscripten. Wij zullen ook het verzenden van gehechtheid van de bevellijn behandelen. Wij zullen met het „post“ bevel beginnen.

POST

Stel eerst een snelle test in werking om ervoor te zorgen de „sendmail“ toepassing geïnstalleerdr correct en werkend is. Voer het volgende bevel uit, dat „you@youremailid.com“ vervangt met uw e-mailadres.

# post - s „Hello wereld“ you@youremailid.com

Raak de terugkeersleutel en u zult aan een nieuwe lijn komen. Ga de tekst in „dit een test van mijn server“ is. Volg de tekst door de terugkeersleutel opnieuw op te raken. Dan raak de belangrijkste combinatie van Control+D om verder te gaan. De bevelherinnering zal u vragen of wilt u een exemplaar van de post aan een ander adres, klap merken Control+D opnieuw. Controleer uw brievenbus. Dit bevel zal een post aan e-mailidentiteitskaart sturen die met het onderwerp wordt vermeld, „Hello wereld“.

Om inhoud aan het lichaam van de post toe te voegen terwijl het runnen van het bevel kunt u de volgende opties gebruiken. Als u tekst op uw wilt toevoegen:

# weergalm „dit zal gaan in het lichaam van de post.“ | post - s „Hello wereld“ you@youremailid.com

En als u post de inhoud van een dossier wilt lezen:

# post - s „Hello wereld“ you@youremailid.com

Een andere nuttige opties in het postbevel zijn:

- sonderwerp (Het onderwerp van de post)
- c e-mail-adres (Merk een exemplaar aan dit „e-mail-adres“, of CC)
- B e-mail-adres (Merk een blinde doorslag aan dit „e-mail-adres“, of BCC)

Hier is hoe u deze opties zou kunnen gebruiken:

# echo „Onthaal aan de wereld van Calvin n Hobbes“ | post - s „Hello wereld“ calvin@cnh.com - c hobbes@cnh.com - B susie.derkins@cnh.com

MUTT

Één van belangrijke nadelen van het gebruiken van het postbevel is dat het niet het verzenden van gehechtheid steunt. mutt, enerzijds, steunt het. Ik heb deze eigenschap bijzonder voor manuscripten nuttig gevonden die niettekstuele rapporten of steunen produceren die in grootte vrij klein zijn dievan ik aan steun elders zou houden. 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>