Πώς να στείλει το ηλεκτρονικό ταχυδρομείο από τη γραμμή εντολής Linux

από Sukrit Dhandhania [[on]] 1 Δεκεμβρίου 2008

Ηλεκτρονικό ταχυδρομείο Linux

Η γραμμή εντολής Linux μπορεί να είναι πολύ ισχυρή μόλις ξέρετε πώς να την χρησιμοποιήσετε. Μπορείτε να αναλύσετε τα στοιχεία, να ελέγξετε τις διαδικασίες, και να κάνετε πολλά άλλα χρήσιμα και δροσερά πράγματα χρησιμοποιώντας το. Συχνά έρχεται μια ανάγκη να παραχθεί μια έκθεση και να ταχυδρομηθεί έξω. Θα μπορούσε να είναι μια τόσο απλή απαίτηση όσο μια ανακοίνωση ότι το στήριγμα της ημέρας επέρασε από το πρόστιμο, ή όχι. Θα σας βοηθήσω να πάρετε αρχισμένους με την αποστολή των ταχυδρομείων από τη γραμμή εντολής Linux και στα χειρόγραφα κοχυλιών. Θα καλύψουμε επίσης την αποστολή των συνδέσεων από τη γραμμή εντολής. Θα αρχίσουμε με την εντολή «ταχυδρομείου».

ΤΑΧΥΔΡΟΜΕΙΟ

Αρχικά τρέξτε μια γρήγορη δοκιμή για να σιγουρευτείτε ότι η εφαρμογή «sendmail» είναι εγκατεστημένη και εργαζόμενη σωστά. Εκτελέστε την ακόλουθη εντολή, που αντικαθιστά «you@youremailid.com» με τη διεύθυνση ηλεκτρονικού ταχυδρομείου σας.

# ταχυδρομείο - κόσμος» you@youremailid.com του s «γειά σου

Χτυπήστε το επιστροφής κλειδί και θα έρθετε σε μια νέα γραμμή. Εισάγετε το κείμενο «που αυτό είναι μια δοκιμή από τον κεντρικό υπολογιστή μου». Εξακολουθήστε το κείμενο με να χτυπήσει το επιστροφής κλειδί πάλι. Κατόπιν χτυπήστε το βασικό συνδυασμό Control+D για να συνεχιστεί. Η υπαγόρευση εντολής θα σας ρωτήσει εάν θέλετε να χαρακτηρίσετε ένα αντίγραφο του ταχυδρομείου σε οποιαδήποτε άλληδήποτε διεύθυνση, χτύπημα Control+D πάλι. Ελέγξτε την ταχυδρομική θυρίδα σας. Αυτή η εντολή θα στείλει ένα ταχυδρομείο στην ταυτότητα ηλεκτρονικού ταχυδρομείου που αναφέρεται με το θέμα, «γειά σου κόσμος».

Για να προσθέσετε το περιεχόμενο στο σώμα του ταχυδρομείου τρέχοντας την εντολή μπορείτε να χρησιμοποιήσετε τις ακόλουθες επιλογές. Εάν θέλετε να προσθέσετε το κείμενο από μόνος σας:

# αντηχήστε «αυτό θα πάει στο σώμα του ταχυδρομείου.» | ταχυδρομείο - κόσμος» you@youremailid.com του s «γειά σου

Και εάν θέλετε το ταχυδρομείο για να διαβάσετε το περιεχόμενο από ένα αρχείο:

# ταχυδρομείο - κόσμος» you@youremailid.com του s «γειά σου

Μερικές άλλες χρήσιμες επιλογές στην εντολή ταχυδρομείου είναι:

- θέμα του s (Το θέμα του ταχυδρομείου)
- ταχυδρομείο-διεύθυνση γ (Χαρακτηρίστε ένα αντίγραφο σε αυτήν την «ταχυδρομείο-διεύθυνση», ή τα CC)
- ταχυδρομείο-διεύθυνση β (Χαρακτηρίστε ένα τυφλό αντίγραφο άνθρακα σε αυτήν την «ταχυδρομείο-διεύθυνση», ή BCC)

Εδώ είναι πώς εσείς δύναμη χρησιμοποιεί αυτές τις επιλογές:

# ηχώ «υποδοχή στον κόσμο Calvin ν Hobbes» | ταχυδρομείο - κόσμος» calvin@cnh.com του s «γειά σου - γ hobbes@cnh.com - β susie.derkins@cnh.com

MUTT

Ένα από σημαντικά μειονεκτήματα της χρησιμοποίησης της εντολής ταχυδρομείου είναι ότι δεν υποστηρίζει την αποστολή των συνδέσεων. mutt, αφ' ενός, το υποστηρίζει. Έχω βρεί αυτό το χαρακτηριστικό γνώρισμα ιδιαίτερα χρήσιμο για τα χειρόγραφα που παράγουν τις μη-κειμενικά εκθέσεις ή τα στηρίγματα που είναι σχετικά μικρές στο μέγεθος που θα ήθελα στο στήριγμα αλλού. 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>