Skip to content Gå til innhold


How to send email from the Linux command line Hvordan sende e-post fra Linux kommandolinjen

EpostLinux

The Linux command line can be very powerful once you know how to use it. Linux kommandolinjen kan være svært kraftig når du vet hvordan du skal bruke den. You can parse data, monitor processes, and do a lot of other useful and cool things using it. Du kan analysere data, overvåke prosesser, og gjøre en masse andre nyttige og kule ting å bruke det. There often comes a need to generate a report and mail it out. Det kommer ofte et behov for å generere en rapport og sende den ut. It could be as simple a requirement as a notification that the day's backup went through fine, or did not. Det kan være så enkelt et krav som et varsel om at dagens sikkerhetskopien gikk gjennom fin eller ikke. I'll help you get started with sending mails from the Linux command line and in shell scripts. Jeg skal hjelpe deg å komme i gang med å sende post fra Linux kommandolinjen og shell scripts. We will also cover sending attachments from the command line. Vi vil også dekke sende vedlegg fra kommandolinjen. We will begin with the “mail” command. Vi skal begynne med "post"-kommandoen.

MAIL MAIL

First run a quick test to make sure the “sendmail” application is installed and working correctly. Først kjøre en rask test for å kontrollere at "sendmail" programmet er installert og fungerer. Execute the following command, replacing “you@youremailid.com” with your e-mail address. Utfør følgende kommando, og erstattet "you@youremailid.com" med din e-postadresse.

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

Hit the return key and you will come to a new line. Hit retur-tasten, og du vil komme til en ny linje. Enter the text “This is a test from my server”. Skriv inn teksten "Dette er en test fra serveren min. Follow up the text by hitting the return key again. Følge opp teksten ved å trykke retur-tasten igjen. Then hit the key combination of Control+D to continue. Deretter trykker du tastekombinasjonen av Ctrl + D for å fortsette. The command prompt will ask you if you want to mark a copy of the mail to any other address, hit Control+D again. Ledeteksten vil spørre deg om du vil merke en kopi av e-post til en annen adresse, trykk Ctrl + D igjen. Check your mailbox. Sjekk postkassen. This command will send out a mail to the email id mentioned with the subject, “Hello world”. Denne kommandoen vil sende ut en mail til e id nevnt med emnet, "Hello world".

To add content to the body of the mail while running the command you can use the following options. For å legge til innhold i kroppen på e-post mens du kjører kommandoen kan du bruke følgende alternativer. If you want to add text on your own: Hvis du vil legge til tekst på egen hånd:

# echo “This will go into the body of the mail.” | mail -s “Hello world” you@youremailid.com # Echo "Dette vil gå inn i selve e-posten." | Mail-s "Hello world" you@youremailid.com

And if you want mail to read the content from a file: Og hvis du ønsker e-post til å lese innholdet fra en fil:

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

Some other useful options in the mail command are: Noen andre nyttige alternativer i post-kommandoen er:

-s subject (The subject of the mail) -s emne (Emnet for post)
-c email-address (Mark a copy to this “email-address”, or CC) -c epostadresse (Mark kopi til denne "e-postadressen", eller CC)
-b email-address (Mark a blind carbon copy to this “email-address”, or BCC) -b e-postadresse (Mark en blindkopi til denne "e-postadressen", eller BCC)

Here's how you might use these options: Her er hvordan du kan bruke disse alternativene:

# 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 "Velkommen til verden av Tommy n Hobbes" | mail-s "Hello world" calvin@cnh.com-c hobbes@cnh.com-b susie.derkins @ cnh.com

MUTT Mutt

One of major drawbacks of using the mail command is that it does not support the sending of attachments. Ett av store ulemper med å bruke e-kommandoen er at den ikke støtter sending av vedlegg. mutt, on the other hand, does support it. mutt, derimot, støtter det. 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. Jeg har funnet denne funksjonen spesielt nyttig for skript som genererer ikke-tekstlige rapporter eller sikkerhetskopier som er relativt lite i størrelse, som jeg gjerne sikkerhetskopien et annet sted. Of course, mutt allows you to do a lot more than just send attachments. Selvfølgelig kan mutt du å gjøre mye mer enn bare å sende vedlegg. It is a much more complete command line mail client than the “mail” command. Det er en mye mer komplett kommandolinje postklient enn "post"-kommandoen. Right now we'll just explore the basic stuff we might need often. Akkurat nå får vi bare utforske grunnleggende ting vi kan trenge ofte. Here's how you would attach a file to a mail: Her ser du hvordan du vil legge ved en fil til en e-post:

# echo “Sending an attachment.” | mutt -a backup.zip -s “attachment” calvin@cnh.com # Echo "Sende et vedlegg." | Mutt-en backup.zip-s "vedlegg" 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. Denne kommandoen vil sende en mail til calvin@cnh.com med subjektet (-r) "vedlegg" hovedteksten "Sende et vedlegg.", Som inneholder vedlegget (-a) backup.zip. Like with the mail command you can use the “-c” option to mark a copy to another mail id. I likhet med den post-kommandoen kan du bruke "-c" for å merke en kopi til en annen post id.

SENDING MAIL FROM A SHELL SCRIPT SENDE-post fra et shell script

Now, with the basics covered you can send mails from your shell scripts. Nå, med det grunnleggende dekket kan du sende mails fra 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. Her er en enkel shell script som gir deg en lesning av bruk av plass på partisjoner og poster dataene for deg.

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

Save these lines in a file on your Linux server and run it. Lagre disse linjene i en fil på din Linux-server og kjøre den. You should receive a mail containing the results of the command. Du vil motta en mail som inneholder resultatet av kommandoen. 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. Hvis derimot, må du sende mer data enn bare dette må du skrive data til en tekstfil og skriv den inn i posten kroppen mens du skriver i posten. 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: Her er og eksempel på et shell script som får diskbruken samt minnebruk, skriver dataene til en midlertidig fil, og deretter går det inn i kroppen på e-post blir sendt ut:

#!/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 "disk og RAM-rapporten" calvin@cnh.com </ tmp / mail_report.log

Now here's a more complicated problem. Nå her en mer komplisert problem. You have to take a backup of a few files and mail then out. Du må ta en sikkerhetskopi av noen filer og post deretter ut. First the directory to be mailed out is archived. Først katalogen sendes ut er arkivert. Then it is sent as an email attachment using mutt. Da er det sendt som e-postvedlegg bruker mutt. Here's a script to do just that: Her er et skript til å gjøre nettopp det:

#!/bin/bash #! / bin / bash
tar -zcf /tmp/backup.tar.gz /home/calvin/files tjære-zcf / tmp / backup.tar.gz / home / Calvin / filer
echo | mutt -a /tmp/backup.tar.gz -s “daily backup of data” calvin@cnh.com ekko | mutt-a / tmp / backup.tar.gz-s "daglig backup av 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. Ekkoet i starten av den siste linjen legger en tom i kroppen av posten blir fastsatt.

This should get you started with sending mails form the Linux command line and from shell scripts. Dette bør komme i gang med å sende post danne Linux kommandolinjen og fra shell scripts. Read up the “man page” for both mail and mutt for more options. Les opp "mannen siden" for både post og mutt for flere alternativer.

Posted in Posted in Email Epost , , Guest Blogger Guest Blogger , , Linux Linux . .

Related Posts: Relaterte innlegg:

How to post to Twitter from the Linux command line Hvordan legge til Twitter fra Linux kommandolinjen
How to enable the root user account in Ubuntu Linux Slik aktiverer root brukerkonto i Ubuntu Linux
Some useful Linux bash tricks Noen nyttige Linux bash triks
How to find your public IP address with the Linux command line Hvordan finne den offentlige IP-adressen med Linux kommandolinjen
How to set up Evolution for email Hvordan sette opp Evolution for e-post

10 Responses 10 Svar

Stay in touch with the conversation, subscribe to the Hold kontakten med samtalen, abonnere på RSS feed for comments on this post RSS feed for kommentarer til dette innlegget . .

  1. Michal Michal says sier

    You may want to have a look at Det kan være lurt å ta en titt på smtp-client.pl smtp-client.pl . . Check out the usage examples. Sjekk ut bruk eksempler. I'd say it's the ultimate command line smtp client, but I'ma little biased Jeg vil si det er den ultimate kommandolinjen SMTP-klient, men jeg er litt partisk ;-)

  2. Matt says Matt sier

    echo | mutt -a -s /tmp/backup.tar.gz “daily backup of data” ekko | mutt-en-s / tmp / backup.tar.gz "daglig backup av data" calvin@cnh.com calvin@cnh.com

    should be: skal være:

    echo | mutt -a /tmp/backup.tar.gz -s “daily backup of data” ekko | mutt-a / tmp / backup.tar.gz-s "daglig backup av data" calvin@cnh.com calvin@cnh.com

  3. Ross McKillop Ross McKillop says sier

    Matt - Matt --

    Thanks very much for catching that! Tusen takk for å fange det! I've made the correction. Jeg har gjort korreksjon. Cheers! Skål!

  4. Jens Arnfelt says Jens Arnfelt sier

    Hi Hei

    You can actually send binary attachment via “mail” the following way : Du kan faktisk sende binære vedlegg via "post" på følgende måte:

    uuencode /etc/hosts /etc/hosts | mail -s “mail with binary attachment” UUENCODE / etc / hosts / etc / hosts | mail-s "post med binære vedlegg" me@mydomain.org me@mydomain.org

    BR BR
    Jens Arnfelt Jens Arnfelt

  5. joviano says joviano sier

    Basically have a test script, whose output comes by mail to me with the use of the command taken from this article: I utgangspunktet har en testskriptet, hvis produksjonen kommer i posten til meg med bruk av kommandoen hentet fra denne artikkelen:

    echo `my test script` | mail -s “Test Result” echo `min testskriptet` | mail-s "Test Result" my_mail@mail.com my_mail@mail.com

    the problem here is that when i do this.. problemet her er at når jeg gjør dette .. it works correctly on the mailing front, however the mail that i then get ignores the line breaks in the output of the scripts. det fungerer riktig på post foran, men e-post som jeg da få ignorerer linjeskift i produksjon av manuskripter. I really need the line breaks for readability of the output results.. Jeg trenger virkelig den linjeskift for lesbarhet av utdataresultater ..

    any pointers on how i could achieve this ?? noen tips om hvordan jeg kunne få til dette? .. .. would really like some help here ville virkelig ha litt hjelp her

  6. martin says martin sier

    I'm looking for a program that will run from a script and will send mail via gmail smtp server. Jeg leter etter et program som kan kjøres fra et manus og sender mail via gmail smtp-server. Any ideas? Any ideas?

  7. Michal Michal says sier

    martin: have a look at martin: ta en titt på 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. - Det er utformet for å kjøre fra et skript eller en cron jobb og sende e-post gjennom en bestemt server. For example: For eksempel:

    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. Det kan også godkjennes med et Gmail-brukernavn og passord blant annet.

  8. martin says martin sier

    Thanks Michal. Takk Michal. I'll check that out. Jeg skal sjekke det ut.

  9. Sujith John says Sujith John sier

    An excellent tutorial !! Et utmerket undervisningstimene! Thanks Much Takk Much

Continuing the Discussion Fortsetter Discussion

  1. How to quickly add Contacts to your Address Book from Mail | TuxWire : The Linux Blog Hvordan du raskt legge til kontakter i adresseboken fra Mail | TuxWire: The Linux Blog linked to this post on September 22, 2009 knyttet til dette innlegget den 22 september 2009

    [...] How to send email from the Linux command line [...] [...] Hvor å sende e-post fra Linux kommandolinjen [...]



Some HTML is OK Some HTML is OK

or, reply to this post via eller svare på dette innlegget via trackback styrekule . .