How to send email from the Linux command line Kā sūtīt e-pastu no Linux komandrindas

by Sukrit Dhandhania on December 1, 2008 ar Sukrit Dhandhania 2008 December 1,

E-pastsLinux

The Linux command line can be very powerful once you know how to use it. Linux komandrindas var būt ļoti iedarbīgs, kad jūs zināt, kā to izmantot. You can parse data, monitor processes, and do a lot of other useful and cool things using it. Jūs varat analizējot datus, uzraudzītu procesus un darīt daudz citas noderīgas un labas lietas izmanto. There often comes a need to generate a report and mail it out. Vai bieži nāk vajadzība veidot ziņojumu un nosūtiet to out. It could be as simple a requirement as a notification that the day's backup went through fine, or did not. Tas varētu būt tikpat vienkārša prasība par paziņošanu, ka dienas backup pārdzīvoja sodu, vai nav. I'll help you get started with sending mails from the Linux command line and in shell scripts. Es jums palīdzēt sākt ar sūtīšanu pastus no Linux komandrindas un čaulas skriptus. We will also cover sending attachments from the command line. Mēs arī uz nosūtot pielikumus no komandrindas. We will begin with the “mail” command. Mēs sāksim ar "pasts" komandu.

MAIL PASTS

First run a quick test to make sure the “sendmail” application is installed and working correctly. Pirmizrāde ātri testu, lai pārliecinātos, "sendmail" pieteikums tiek uzstādīta un darbojas pareizi. Execute the following command, replacing “you@youremailid.com” with your e-mail address. Izpildīt komandu, aizstājot vārdu "you@youremailid.com" ar savu e-pasta adresi.

# mail -s “Hello world” you@youremailid.com # Mail-s "Hello world" you@youremailid.com

Hit the return key and you will come to a new line. Hit atpakaļ atslēgu un jūs ierodas jaunā rindā. Enter the text “This is a test from my server”. Ievadiet tekstu "Šis ir tests no mana servera. Follow up the text by hitting the return key again. Sekot tekstu hitting atgriešanās taustiņu vēlreiz. Then hit the key combination of Control+D to continue. Tad hit taustiņu kombināciju Ctrl + D turpināt. The command prompt will ask you if you want to mark a copy of the mail to any other address, hit Control+D again. Komandu uzvedne jautāt jums, ja jūs vēlaties, lai atzīmētu kopiju pastu uz citu adresi, hit Ctrl + D vēlreiz. Check your mailbox. Pārbaudiet savu pastkastīti. This command will send out a mail to the email id mentioned with the subject, “Hello world”. Šī komanda izsūtīt pastu uz e-pastu id minētas ar tematu "Hello world".

To add content to the body of the mail while running the command you can use the following options. Lai pievienotu saturu iestādei pasta darbināšanas laikā komandas var izmantot šādas iespējas. If you want to add text on your own: Ja jūs vēlaties pievienot tekstu par savu:

# echo “This will go into the body of the mail.” | mail -s “Hello world” you@youremailid.com # Echo "Tas stāsies iestāde pastu." | Mail-s "Hello world" you@youremailid.com

And if you want mail to read the content from a file: Un, ja jūs vēlaties pasta lasīt saturu no failu:

# mail -s “Hello world” you@youremailid.com < /home/calvin/application.log # Mail-s "Hello world" you@youremailid.com </ home / Calvin / application.log

Some other useful options in the mail command are: Dažas citas noderīgas opcijas pasts komanda ir:

-s subject (The subject of the mail) -s tēmu (priekšmets mail)
-c email-address (Mark a copy to this “email-address”, or CC) -c e-pasta adresi (Mark kopiju ar šo "e-pasta adresi", vai CC)
-b email-address (Mark a blind carbon copy to this “email-address”, or BCC) -b e-pasta adresi (Mark aklā oglekļa kopiju ar šo "e-pasta adresi", vai BCC)

Here's how you might use these options: Lūk, kā jūs varētu izmantot šo iespēju:

# echo “Welcome to the world of Calvin n Hobbes” | mail -s “Hello world” calvin@cnh.com -c hobbes@cnh.com -b susie.derkins@cnh.com # Echo "Welcome to pasaulē Calvin n Hobbes" | mail-s "Hello world" calvin@cnh.com-c hobbes@cnh.com-b susie.derkins @ cnh.com

MUTT Stulbenis

One of major drawbacks of using the mail command is that it does not support the sending of attachments. Viens no galvenajiem trūkumiem, izmantojot pasta komanda ir, ka tā neatbalsta nosūtīšanu pielikumus. mutt, on the other hand, does support it. stulbenis, no otras puses, tas atbalsta. I've found this feature particularly useful for scripts that generate non-textual reports or backups which are relatively small in size which I'd like to backup elsewhere. Es atradu šo funkciju, īpaši noderīgi skriptus, kas ģenerē bez teksta ziņojumu vai backups, kas ir salīdzinoši mazs izmērs, ko es gribētu, lai rezerves citur. Of course, mutt allows you to do a lot more than just send attachments. Protams, stulbenis ļauj jums darīt daudz vairāk nekā tikai nosūtīt pielikumus. It is a much more complete command line mail client than the “mail” command. Tas ir daudz pilnīgāks komandrindas pasta klientu nekā "pasts" komandu. Right now we'll just explore the basic stuff we might need often. Šobrīd mēs vienkārši izpētīt pamata stuff mums var būt nepieciešams biežāk. Here's how you would attach a file to a mail: Lūk, kā jūs varētu pievienot failu pasts:

# echo “Sending an attachment.” | mutt -a backup.zip -s “attachment” calvin@cnh.com # Echo "Sending pielikumu." | Stulbenis-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. Šī komanda nosūta pastu calvin@cnh.com ar kuriem (-s) "attachment", pamattekstu "nosūtītājvalsts pielikumu." Satur pielikumu (-a) backup.zip. Like with the mail command you can use the “-c” option to mark a copy to another mail id. Tāpat kā ar pasta komandu varat izmantot "-c" opciju, lai zīmes kopiju uz citu pasta id.

SENDING MAIL FROM A SHELL SCRIPT Sūtīt e-pastu NO SHELL SCRIPT

Now, with the basics covered you can send mails from your shell scripts. Tagad, pamati, uz kurām jūs varat sūtīt vēstules no savas čaulas skriptus. 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. Šeit ir vienkārši shell skriptu, kas nodrošina iespēju izmantošanas vieta lasījumā Jūsu nodalījumi un pastu jums datiem.

#!/bin/bash #! / bin / bash
df -h | mail -s “disk space report” calvin@cnh.com df-h | mail-s "diska vietas ziņojums" calvin@cnh.com

Save these lines in a file on your Linux server and run it. Saglabāt šo failu Linux servera līnijas un palaist to. You should receive a mail containing the results of the command. Jums vajadzētu saņemt pastu, kas satur ar komandu rezultātus. 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. Tomēr, ja jums nosūtīt vairāk datu, nekā tikai tas jums būs nepieciešams rakstīt datus teksta failā un ievadīt to pasta iestādei, kamēr komponēšanu pastu. 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: Here's un piemērs shell skriptu, kas izpaužas diska izmantošana, kā arī ielādes, atmiņas, raksta datus pagaidu failu, un pēc tam ievada to, viss kopums pastu tiek nosūtīts:

#!/bin/bash #! / bin / bash
df -h > /tmp/mail_report.log df-h> / tmp / mail_report.log
free -m >> /tmp/mail_report.log free-m>> / tmp / mail_report.log
mail -s “disk and RAM report” calvin@cnh.com < /tmp/mail_report.log mail-s "disku un RAM ziņojumu" calvin@cnh.com </ tmp / mail_report.log

Now here's a more complicated problem. Tagad šeit ir daudz sarežģītāka problēma. You have to take a backup of a few files and mail then out. Jums ir jāveic backup dažus failus un pastu pēc tam out. First the directory to be mailed out is archived. Pirmo direktoriju, kas nosūtīts no arhivēšanai. Then it is sent as an email attachment using mutt. Tad tas ir nosūtīts kā e-pasta pielikumu, izmantojot stulbenis. Here's a script to do just that: Lūk skripts to darīt tikai, ka:

#!/bin/bash #! / bin / bash
tar -zcf /tmp/backup.tar.gz /home/calvin/files tar-zcf / tmp / backup.tar.gz / home / Calvin / faili
echo | mutt -a /tmp/backup.tar.gz -s “daily backup of data” calvin@cnh.com ECHO | stulbenis-/ tmp / backup.tar.gz-s "dienas 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. Uz sākumu pēdējā rindā echo piebilst tukšu vērā iestāde pasts ir izklāstīti.

This should get you started with sending mails form the Linux command line and from shell scripts. Tas būtu tu sāktu ar sūtīšanu pasta forma Linux komandrindas un no čaulas skriptus. Read up the “man page” for both mail and mutt for more options. Lasīt up "man lapu", lai gan pasta un stulbenis for more options.

Related Posts: Related Posts:
  • How to set up Evolution for email Kā izveidot Evolution e-pasta
  • How to post to Twitter from the Linux command line Kā izlikt čivināt no Linux komandrindas
  • Privacy Policy Privātuma politika
  • How to enable the root user account in Ubuntu Linux Kā lai root lietotāja kontu Ubuntu Linux
  • Some useful Linux bash tricks Daži noderīgi Linux bash tricks
  • Get Simple Help tutorials just like this one in your email inbox every day - for free! Get Simple Palīdzība konsultācijas tāpat kā šo vienu savā e-pastā ik dienas - bez maksas! Just enter your email address below: Vienkārši ievadiet savu e-pasta adresi:

    You can always opt out of this email subscription at any time. Jūs vienmēr varat atteikties no šīs e-pasta abonementu jebkurā laikā.

    { 9 comments… read them below or (9 komentāri ... lasīt tos zem vai add one pievienot vienu } )

    1 1 Michal Michal 12.08.08 at 3:20 am 12.08.08 at 3:20

    You may want to have a look at Jūs varat būt apskatīt smtp-client.pl SMTP-client.pl . . Check out the usage examples. Iepazīstieties ar lietošanas piemēriem. I'd say it's the ultimate command line smtp client, but I'ma little biased Es teiktu, ka tas ir galvenais komandrindas SMTP klients, bet esmu mazliet neobjektīvs ;-)

    2 2 Matt 01.21.09 at 11:24 pm At 11:24 01.21.09 Matt

    echo | mutt -a -s /tmp/backup.tar.gz “daily backup of data” ECHO | stulbenis--s / tmp / backup.tar.gz "dienas backup of data" calvin@cnh.com calvin@cnh.com

    should be: jābūt:

    echo | mutt -a /tmp/backup.tar.gz -s “daily backup of data” ECHO | stulbenis-/ tmp / backup.tar.gz-s "dienas backup of data" calvin@cnh.com calvin@cnh.com

    3 3 Ross McKillop Ross McKillop 01.22.09 at 12:47 am 01.22.09 at 12:47

    Matt – Matt --

    Thanks very much for catching that! Thanks very much ķeršanai that! I've made the correction. Esmu veiktas korekcijas. Cheers! Urravas!

    4 4 Jens Arnfelt 03.13.09 at 1:52 am Jens Arnfelt 03.13.09 at 1:52

    Hi Čau

    You can actually send binary attachment via “mail” the following way : Jūs faktiski var nosūtīt binārā pielikumu, izmantojot "pasts" šādā veidā:

    uuencode /etc/hosts /etc/hosts | mail -s “mail with binary attachment” uuencode / etc / hosts / etc / hosts | mail-s "pasta ar bināriem attachment" me@mydomain.org me@mydomain.org

    BR BR
    Jens Arnfelt Jens Arnfelt

    5 5 joviano 05.11.09 at 2:05 am at 2:05 05.11.09 joviano am

    Basically have a test script, whose output comes by mail to me with the use of the command taken from this article: Būtībā ir testa skriptu, kuru produkcija nonāk pa pastu man izmantojot komandu ņemti no šo rakstu:

    echo `my test script` | mail -s “Test Result” echo "savā pārbaudes skriptu" | mail-s "Pārbaudes rezultāti" my_mail@mail.com my_mail@mail.com

    the problem here is that when i do this.. Problēma šeit ir tāda, ka tad, kad man tas .. it works correctly on the mailing front, however the mail that i then get ignores the line breaks in the output of the scripts. tā darbojas pareizi pasta priekšā, tomēr pasta, ka man pēc tam saņemt ignorē līnijas pārtraukumi produkcijas skriptus. I really need the line breaks for readability of the output results.. Man tiešām ir nepieciešams rindiņu pārtraukumi, lai salasāmību produkcija rezultātiem ..

    any pointers on how i could achieve this ?? jebkurš par to, kā es varētu sasniegt šīs norādes? .. .. would really like some help here tiešām, mazliet palīdzēt šeit

    6 6 martin 07.10.09 at 10:51 am at 10:51 07.10.09 martin am

    I'm looking for a program that will run from a script and will send mail via gmail smtp server. Es meklēju programmu, kas ilgs no skriptu un sūtīt vēstules, izmantojot Gmail SMTP serveris. Any ideas? Any ideas?

    7 7 Michal Michal 07.10.09 at 4:54 pm 07.10.09 at 4:54

    martin: have a look at martin: ir apskatīt smtp-client.pl SMTP-client.pl – it is designed to run from a script or a cron job and send email through a specified server. - Ir paredzēts, vajadzēja ilgt no skriptu vai cron darbu un sūtīt e-pastu caur norādīto serveri. For example: For example:

    smtp-client.pl --server smtp.gmail.com --from someone@gmail.com --to some@body.else --subject Blah --attach /path/to/a/picture.png smtp-client.pl --server smtp.gmail.com --from someone@gmail.com --to some@body.else --subject Blah --attach /path/to/a/picture.png smtp-client.pl --server smtp.gmail.com --from someone@gmail.com --to some@body.else --subject Blah --attach /path/to/a/picture.png smtp-client.pl --server smtp.gmail.com --from someone@gmail.com --to some@body.else --subject Blah --attach /path/to/a/picture.png smtp-client.pl --server smtp.gmail.com --from someone@gmail.com --to some@body.else --subject Blah --attach /path/to/a/picture.png

    It can also authenticate with a Gmail username and password among other things. Tā arī var apliecināt ar Gmail lietotājvārdu un paroli starp citām lietām.

    8 8 martin 07.22.09 at 1:50 pm at 1:50 07.22.09 martin

    Thanks Michal. Thanks Michal. I'll check that out. I'll check ka no.

    9 9 Sujith John 08.01.09 at 7:33 am Sujith John 08.01.09 at 7:33

    An excellent tutorial !! Lieliska pamācība! Thanks Much Thanks Daudz

    Leave a Comment Leave 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> Jūs varat izmantot šos HTML tagus un atribūtus: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>