Come trasmettere email dalla linea di ordine di Linux

da Sukrit Dhandhania su 1° dicembre 2008

Email Linux

La linea di ordine di Linux può essere molto potente una volta che sapete usarla. Potete analizzare i dati, processi del video e fate molto altre cose utili e fredde usando. Viene spesso una necessità di generare un rapporto e di spedirlo per posta fuori. Potrebbe essere semplice un requisito come notifica che il sostegno del giorno ha passato attraverso l'indennità, o non. Li aiuterò ad ottenere cominciati con la trasmissione delle poste dalla linea di ordine di Linux ed in scritti di coperture. Inoltre riguarderemo la trasmissione dei collegamenti dalla linea di ordine. Cominceremo con l'ordine “della posta„.

POSTA

Il primo funzionamento una prova rapida da assicurarsi l'applicazione “del sendmail„ è installato e funzionante correttamente. Esegua il seguente ordine, sostituente “you@youremailid.com„ con il vostro indirizzo di E-mail.

# posta - mondo„ you@youremailid.com di s “ciao

Colpisca la chiave RETURN e verrete ad una nuova linea. Digiti il testo “che questa è una prova dal mio assistente„. Aggiornamento il testo colpendo la chiave RETURN ancora. Allora colpisca la combinazione chiave di Control+D per continuare. Il richiamo di ordine gli chiederà se desiderate contrassegnare una copia della posta a qualunque altro indirizzo, colpo Control+D ancora. Controlli la vostra cassetta postale. Questo ordine spedirà una posta all'identificazione del email accennata con l'oggetto, “ciao mondo„.

Per aggiungere soddisfare al corpo della posta mentre fate funzionare l'ordine potete usare le seguenti opzioni. Se desiderate aggiungere il testo sui vostri propri:

# echeggi “questo entrerà nel corpo della posta.„ | posta - mondo„ you@youremailid.com di s “ciao

E se desiderate la posta leggere il soddisfare da una lima:

# posta - mondo„ you@youremailid.com di s “ciao

Alcune altre opzioni utili nell'ordine della posta sono:

- oggetto di s (L'argomento di posta)
- email address di c (Contrassegni una copia a questo “email address„, o il cc)
- email address di b (Contrassegni una copia con carta carbone cieca a questo “email address„, o BCC)

Qui è come potreste usare queste opzioni:

# echeggi “il benvenuto al mondo di Calvin la n Hobbes„ | posta - mondo„ calvin@cnh.com di s “ciao - c hobbes@cnh.com - b susie.derkins@cnh.com

MUTT

Uno degli svantaggi importanti di usando l'ordine della posta è che non sostiene la trasmissione dei collegamenti. il mutt, d'altra parte, lo sostiene. Ho trovato questa caratteristica particolarmente utile per gli scritti che generano i rapporti o i sostegni non-testuali che sono relativamente piccoli nel formato che gradirei a sostegno altrove. 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>