logo

Linux mail-opdracht

Linux mail command is een opdrachtregelhulpprogramma waarmee we e-mails vanaf de opdrachtregel kunnen verzenden. Het zal erg handig zijn om e-mails vanaf de opdrachtregel te verzenden als we programmatisch e-mails willen genereren vanuit shell-scripts of webapplicaties. Het mailcommando kan zowel rechtstreeks door de terminal als door het Shell-script worden gebruikt. Er zijn echter veel andere opdrachten beschikbaar in Linux die kunnen worden gebruikt om e-mails vanaf de opdrachtregel te verzenden, zoals sendmail, straathond, SSMTP, telnet , en meer. In deze sectie zullen we ons concentreren op de mailopdracht.

Installatie van mailopdracht in Linux

De mailopdracht kan door verschillende pakketten worden geïnstalleerd, zoals:

  • gnu mailutils
  • heirloom-mailx
  • bsd-mailx

Elk pakket faciliteert met verschillende functies en opties. Bijvoorbeeld de erfstuk-matrix pakket kan een externe smtp-server gebruiken voor het verzenden van berichten, terwijl de overige twee servers de lokale smtp-server gebruiken.

We zullen het mailutils-pakket installeren, de populairste manier om e-mails vanaf de opdrachtregel te verzenden. Het ondersteunt de mailopdracht om de e-mails te verzenden.

Om het mailutils-pakket te installeren, voert u de onderstaande opdracht uit:

 sudo apt install mailutils 

De bovenstaande opdracht vraagt ​​om een ​​systeemwachtwoord, typ het wachtwoord en druk op BINNENKOMEN sleutel. Het verifieert de installatie, type 'En' om de installatie te bevestigen. Beschouw de onderstaande uitvoermodule:

Linux mail-opdracht

Het zal een daemon-proces starten en om de postfix-configuratie vragen, selecteer de gewenste postfix-optie.

converteer tekenreeks naar json java
Linux mail-opdracht

Er zijn vier postfix-opties beschikbaar voor installatie. Ze zijn als volgt:

Linux mail-opdracht

Om de gewenste optie te selecteren, bladert u door de optie met behulp van PIJL en selecteer deze met behulp van TAB+ENTER sleutels. Postfix werkt samen met de server om verzoeken te verwerken. Nadat u de postfix-optie hebt geselecteerd, wordt het mailutils-pakket met succes op uw computer geïnstalleerd.

Hoe werkt het mailcommando?

Het is belangrijk om te weten hoe de opdracht werkt. De mailopdracht van het mailutils-pakket roept het standaard binaire bestand sendmail aan om de mail naar een gespecificeerde bestemming te sturen. Het maakt verbinding met de lokale MTA, een lokaal werkende SMTP-server die e-mails op poort 25 ondersteunt.

Het betekent dat er een postfix die een smtp-server bevat op onze machine moet draaien om de mailopdracht te kunnen gebruiken. De postfix vraagt ​​DNS, de bestemming waar de e-mail moet worden afgeleverd, DNS antwoordt een lijst met mailservers en maakt een SMTP-verbinding. Als het ontbreekt, krijgen we de foutmelding ' send-mail: Kan mail:25 niet openen '.

Verzend e-mail met behulp van de opdracht mail

Het verzenden van e-mails vanaf de opdrachtregel is een eenvoudig proces. Om een ​​e-mail vanaf de terminal te verzenden, voert u de opdracht als volgt uit:

 mail -s 'Subject' 

De optie '-s' wordt gebruikt om het onderwerp op te geven. Na het uitvoeren van de opdracht wordt om het CC-adres gevraagd. Typ het Cc-adres (carbon copy); je kunt het leeg laten door op de Enter-toets te drukken. Typ de tekst en druk op CTRL+D om deze te verzenden. Beschouw de onderstaande uitvoer:

Linux mail-opdracht

Vanuit de bovenstaande uitvoer wordt de e-mail naar een opgegeven e-mailadres verzonden.

Geef de hoofdtekst van de e-mail op één regel op

We kunnen het onderwerp en de boodschap in één regel specificeren. Om de berichttekst op één regel op te geven, voert u de onderstaande opdracht uit:

json-formaat voorbeeld
 mail -s 'subject' <<< 'Message' 

Met de bovenstaande opdracht wordt de e-mail naar het opgegeven adres verzonden. Overweeg het onderstaande commando:

Linux mail-opdracht

Of we kunnen de opdracht ook als volgt specificeren:

 echo 'Message body' | mail -s 'Subject' aFrom:Dwivedi [email protected] 

Bericht ophalen uit een bestand

We kunnen ook berichten uit een bestand ophalen. Het is handig als we de mailopdracht willen oproepen vanuit een shellscript of andere programma's. Om een ​​bericht vanuit een bestand te verzenden, voert u de opdracht als volgt uit:

 mail -s &apos;Hello World&apos; [email protected]  <p>The above command will send the message from the file mailcontent.txt. For quick, we can also use the below command:</p> <pre> $ echo &apos;Message body&apos; | mail -s &apos;Subject&apos; </pre> <h3>Specify CC and BCC</h3> <p>We can also attach a bcc and cc address within a command. To attach a bcc and cc address, use the -b and -c options, respectively.</p> <p>To add a bcc address, execute the command as follows:</p> <pre> mail -s &apos;Hello World&apos; -b userto </pre> <p>To add a cc address, execute the command as follows:</p> <pre> mail -s &apos;Hello World&apos; -c userto </pre> <p>Also, we can specify both cc and bcc addresses in a single command. Execute the command as follows:</p> <pre> mail -s &apos;Hello World&apos; -b userto -c userto </pre> <h3>Adding multiple recipients</h3> <p>The mail command allows us to add more than one recipient in a single mail other than cc and bcc. To add more than one mail address, just put the other mail addresses after the first mail address followed by a comma(,). Consider the below command:</p> <pre> mail -s &apos;Hello World&apos; , </pre> <p>The above command will deliver the message to both addresses.</p> <h3>Specify the sender name and address</h3> <p>We can add the additional header information such as &apos;FROM&apos; name and address to attach with the email. To specify the additional information with the mail command, use the -a option with the command. Execute the command as follows:</p> <pre> $ echo &apos;Message body&apos; | mail -s &apos;Subject&apos; -aFrom:Sender_name recipient address </pre> <p>The above command will deliver the specified information to the receiver.</p> <h3>Adding an attachment</h3> <p>We can also attach a file with the mail. The -a option is used to attach a file along with the mail. To add an attachment, execute the command as follows:</p> <pre> echo &apos;Message body&apos; | mail -s &apos;Subject&apos; -r &apos;&apos; -a /path/to/file </pre> <p>The above command will attach a specified file from the specified path with mail.</p> <h2>Read mails</h2> <p>The mail command can be used to read all your incoming mails. However, it is not cool to read mails from the command line, but we can understand it for the shake of knowledge. To read the inbox mails, execute the mail command without any option as follows:</p> <pre> mail </pre> <p>The above command will display all incoming mails.</p> <h2>Manual for the mail command</h2> <p>To read the manual for the mail command, execute the man command as follows:</p> <pre> man mail </pre> <p>The above command will display the manual of the mail command on your terminal. Consider the below output:</p> <img src="//techcodeview.com/img/linux-tutorial/07/linux-mail-command-6.webp" alt="Linux mail Command"> <p>Press the &apos;q&apos; key to exit from the manual and &apos;h&apos; key for help. The help option lists more options to assist you. Consider the below image:</p> <img src="//techcodeview.com/img/linux-tutorial/07/linux-mail-command-7.webp" alt="Linux mail Command"> <p>To exit from the help, press the &apos;q&apos; key.</p> <hr>

Geef CC en BCC op

We kunnen ook een bcc- en cc-adres aan een opdracht toevoegen. Om een ​​bcc- en cc-adres toe te voegen, gebruikt u respectievelijk de opties -b en -c.

Om een ​​bcc-adres toe te voegen, voert u de opdracht als volgt uit:

 mail -s &apos;Hello World&apos; -b userto 

Om een ​​CC-adres toe te voegen, voert u de opdracht als volgt uit:

 mail -s &apos;Hello World&apos; -c userto 

We kunnen ook zowel cc- als bcc-adressen opgeven in één enkele opdracht. Voer de opdracht als volgt uit:

 mail -s &apos;Hello World&apos; -b userto -c userto 

Meerdere ontvangers toevoegen

Met de opdracht mail kunnen we meer dan één ontvanger toevoegen aan één enkele e-mail, behalve cc en bcc. Om meer dan één e-mailadres toe te voegen, plaatst u de overige e-mailadressen na het eerste e-mailadres, gevolgd door een komma(,). Overweeg het onderstaande commando:

 mail -s &apos;Hello World&apos; , 

Met het bovenstaande commando wordt het bericht op beide adressen afgeleverd.

Geef de naam en het adres van de afzender op

We kunnen de aanvullende headerinformatie, zoals de 'FROM'-naam en het adres, toevoegen om bij de e-mail te voegen. Om de aanvullende informatie op te geven met de mailopdracht, gebruikt u de optie -a bij de opdracht. Voer de opdracht als volgt uit:

 $ echo &apos;Message body&apos; | mail -s &apos;Subject&apos; -aFrom:Sender_name recipient address 

Het bovenstaande commando levert de gespecificeerde informatie aan de ontvanger.

Een bijlage toevoegen

Wij kunnen ook een bestand bij de mail voegen. De optie -a wordt gebruikt om een ​​bestand bij de e-mail te voegen. Om een ​​bijlage toe te voegen, voert u de opdracht als volgt uit:

 echo &apos;Message body&apos; | mail -s &apos;Subject&apos; -r &apos;&apos; -a /path/to/file 

Met de bovenstaande opdracht wordt een opgegeven bestand van het opgegeven pad bijgevoegd bij e-mail.

Linux taakbeheer

Mails lezen

Het mailcommando kan worden gebruikt om al uw inkomende e-mails te lezen. Het is echter niet cool om e-mails vanaf de opdrachtregel te lezen, maar we kunnen het wel begrijpen door de kennis te schudden. Om de inbox-e-mails te lezen, voert u de mailopdracht zonder enige optie als volgt uit:

 mail 

Met de bovenstaande opdracht worden alle inkomende e-mails weergegeven.

Handleiding voor de mailopdracht

Om de handleiding voor het mail-commando te lezen, voert u het man-commando als volgt uit:

 man mail 

Met het bovenstaande commando wordt de handleiding van het mailcommando op uw terminal weergegeven. Beschouw de onderstaande uitvoer:

Linux mail-opdracht

Druk op de 'q'-toets om de handleiding te verlaten en op de 'h'-toets voor hulp. De helpoptie bevat meer opties om u te helpen. Beschouw de onderstaande afbeelding:

Linux mail-opdracht

Om de help te verlaten, drukt u op de 'q'-toets.