How to – Email von der Linux cmd versenden

by

The Linux command line can be very powerful once you know how to use it. You can parse data, monitor processes, automate backups and do a lot of other useful and cool things using it. There often comes a need to generate a report and mail it out. It could be as simple a requirement as a notification that the day’s backup went through fine, or did not. I’ll help you get started with sending mails from the Linux command line and in shell scripts. We will also cover sending attachments from the command line. We will begin with the „mail“ command.

MAIL

First run a quick test to make sure the „sendmail“ application is installed and working correctly. Execute the following command, replacing „email@php-faq.eu“ with your e-mail address.

Hit the return key and you will come to a new line. Enter the text „This is a test from my server“. Follow up the text by hitting the return key again. Then hit the key combination of Control+D to continue. The command prompt will ask you if you want to mark a copy of the mail to any other address, hit Control+D again. Check your mailbox. This command will send out a mail to the email id mentioned with the subject, „Hello world“.

To add content to the body of the mail while running the command you can use the following options. If you want to add text on your own:

And if you want mail to read the content from a file:

 

Some other useful options in the mail command are:

-s subject (The subject of the mail)
-c email-address (Mark a copy to this „email-address“, or CC)
-b email-address (Mark a blind carbon copy to this „email-address“, or BCC)

Here’s how you might use these options:

MUTT

One of major drawbacks of using the mail command is that it does not support the sending of attachments. mutt, on the other hand, does support it. 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. 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:

This command will send a mail to email@php-faq.eu 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.

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:

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:

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 (enter the commands „man mail“ and „man mutt“ for the full manual on each).

 

cmd email

Shellscript

 

Using telnet

This may help when testing MTA setup such as Postfix, Sendmail, qmail, etc..

Note S: stand for System message and C: stand for Client user. It is just for tutorial purpose. Do not need to enter C: in your input.
Just dot (period character) is meaningful.

Using mail program

IMAP

POP3

Test Your Pop3 Connection From Telnet

Other Telnet commands:

stat = Status of you mailbox
dele N = delete mail nummer „N“

 

 

Otherwise

 

Or parse from file,

Use mutt for attaching file,

 

Bashing…

 

 

You can actually send binary attachment via „mail“ the following way :

 

According to other websites you can send attachments using „mail“ if you pipe the attachment through uuencode into the mail command line.

Examples: