How to - Email von der Linux cmd versenden

09.12.2021

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 protected]” with your e-mail address.

[root@php-faq]# mail -s "Hello world" [email protected]

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:

[root@php-faq]# echo "This will go into the body of the mail." | mail -s "Hello world" [email protected]

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

[root@php-faq]# mail -s "Hello world" [email protected] < /home/user/application.log

 

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:

[root@php-faq]# echo "Welcome to the world of PHP" | mail -s "Hello world" [email protected] -c [email protected] -b [email protected]

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:

[root@php-faq]# echo "Sending an attachment." | mutt -a backup.zip -s "attachment" [email protected]

This command will send a mail to [email protected] 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" [email protected]

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 Reports" [email protected] < /tmp/mail_report.log

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/user/files echo | mutt -a /tmp/backup.tar.gz -s "daily backup of data" [email protected]

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

   echo "hello world" | mail -s "a subject" [email protected]
   echo "This will go into the body of the mail." | mail -s "Hello world" [email protected]
   mail -s "Hello world" [email protected] < /home/user/application.log
   top -b -n 1 | mail -s "any subject" [email protected]
   mail -s "Your Subject" [email protected] < /file/with/mail/content		
	(/file/with/mail/content should be a plaintext file, not a file attachment or an image, etc)
   echo "Sending an attachment." | mutt -a file.zip -s "attachment" [email protected]

Shellscript

#############
#!/bin/sh
#set -x
LANG=de_DE

# ARG

FROM="[email protected]"
TO="[email protected]"
SUBJECT="test é"
MSG="BODY text"
FILES="fic1.pdf fic2.pdf"

# http://de.wikipedia.org/wiki/Multipurpose_Internet_Mail_Extensions

SUB_CHARSET=$(echo ${SUBJECT} | file -bi - | cut -d"=" -f2)
SUB_B64=$(echo ${SUBJECT} | uuencode --base64 - | tail -n+2 | head -n+1)

NB_FILES=$(echo ${FILES} | wc -w)
NB=0
cat <<EOF | /usr/sbin/sendmail -t
From: ${FROM}
To: ${TO}
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary=frontier
Subject: =?${SUB_CHARSET}?B?${SUB_B64}?=

--frontier
Content-Type: $(echo ${MSG} | file -bi -)
Content-Transfer-Encoding: 7bit

${MSG}
$(test $NB_FILES -eq 0 && echo "--frontier--" || echo "--frontier")
$(for file in ${FILES} ; do
 let NB=${NB}+1
 FILE_NAME="$(basename $file)"
 echo "Content-Type: $(file -bi $file); name="${FILE_NAME}""
 echo "Content-Transfer-Encoding: base64"
 echo "Content-Disposition: attachment; filename="${FILE_NAME}""
 #echo ""
 uuencode --base64 ${file} ${FILE_NAME}
 test ${NB} -eq ${NB_FILES} && echo "--frontier--" || echo 
"--frontier"
done)
EOF
#######################

 

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.

[root@php-faq]# telnet mail.lxu.io 25
   S: Trying 223.65.47.290...
   S: Connected to mail.lxu.io.
   S: Escape character is '^]'.
   S: 220 mail.lxu.io ESMTP Postfix
   C: ehlo lxu.io
   S: 250-mail.lxu.io
   S: 250-PIPELINING 
   S: 250-SIZE 10240000 
   S: 250-VRFY 
   S: 250-ETRN 
   S: 250-STARTTLS 
   S: 250-AUTH LOGIN PLAIN DIGEST-MD5 CRAM-MD5 
   S: 250-AUTH=LOGIN PLAIN DIGEST-MD5 CRAM-MD5 
   S: 250 8BITMIME
   C: mail from: <[email protected]>
   S: 250 Ok
   C: rcpt to: <[email protected]>
   S: 250 ok
   C: data
   S: 354 End data with .
   C: Subject: Your subject message
   C: Your messages.
   C: Your messages.
   C: .
   S: 250 Ok: queued as ABC1D1C123
   C: quit
   S: 221 Bye
   S: Connection closed by foreign host.

Using mail program

[root@php-faq]# mail [email protected]
   Subject: Hello
   Hi,
Testing. Testing. Take care.. 
(Type  DOT (.) followed by ENTER KEY}
   Cc: (Press ENTER KEY)

IMAP

  [root@php-faq]# telnet localhost 143
   1 capability
   2 login username@domain password

POP3

Test Your Pop3 Connection From Telnet

   Type: telnet "the name of the pop3 server" 110
   Type: user "username"
   Type: pass "password"
   Type: list
   You will then get a list over the mails in your mailbox
   Type: retr "mailnumber"
   You will then see the mail with the mail number
   End the session by typing "Quit"

Other Telnet commands:
	**stat** = Status of you mailbox **dele N** = delete mail nummer "N"

 

[root@php-faq]# mailq -AC

[root@php-faq]# mailq -Lq

[root@php-faq]# mail -s "Hello world" [email protected]   
	ENTER then type message body then CTRL+D.

 

**Otherwise**

[root@php-faq]# echo "This will go into the body of the mail." | mail -s "Hello world" [email protected]

 

Or parse from file, [root@php-faq]# mail -s "Hello world" [email protected] < /tmp/message.log

Use mutt for attaching file, [root@php-faq]# echo "Sending an attachment." | mutt -a backup.zip -s "attachment" [email protected]

 

Bashing…

#!/bin/bash
   df -h | mail -s "disk space report" root

	df -h > /tmp/mail_report.log
	free -m >> /tmp/mail_report.log
	mail -s "disk and RAM report" root < /tmp/mail_report.log
#!/bin/bash
   tar -zcf /tmp/backup.tar.gz /data/files
   echo | mutt -a /tmp/backup.tar.gz -s "daily backup of data" root

 

[root@php-faq]# echo | mutt -a /tmp/backup.tar.gz -s "daily backup of data" [email protected]

 

You can actually send binary attachment via “mail” the following way : [root@php-faq]# uuencode /etc/hosts /etc/hosts | mail -s "mail with binary attachment" [email protected]

 

[root@php-faq]# ( df -h; free -m ) | mail -s "disk and RAM report"[email protected] According to other websites you can send attachments using “mail” if you pipe the attachment through uuencode into the mail command line.

Examples:

[root@php-faq]# uuencode surfing.jpeg surfing.jpeg | mail [email protected]
   (cat mailtext; uuencode surfing.jpeg surfing.jpeg) | mail [email protected]
comments powered by Disqus