|
Guides All Guides and HOWTO's. |
|
Thread Tools | Display Modes |
|
|||
matts: a shell script to mail attachments from the command line
matts: a shell script to mail files as attachments from the command line
1.1 Introduction Mailing output of commands or contents of files in-line is not difficult to do from within the shell. Some examples:
However, sending an attachment using the MIME standard cannot be done easily from the command line. In the following section we will take a detailed look at a MIME mail message containing an attachment and then you will understand why. 1.2 Investigating a MIME mail message The MIME acronym stands for Multipurpose Internet Mail Extensions. These extensions add support for single or multiple non-text attachments. See http://en.wikipedia.org/wiki/MIME. The MIME part of a mail message: Code:
1 MIME-Version: 1.0 2 Content-Type: multipart/mixed; 3 boundary="===========N=E=X=T===5facb5052688c84e499c086252c902db========" 4 Content-Transfer-Encoding: 7bit 5 6 This message is in MIME format. If you see this, 7 then your mail reader is not MIME-aware 8 9 --===========N=E=X=T===5facb5052688c84e499c086252c902db======== 10 Content-Type: text/plain; charset=ISO-8859-1 11 Content-Transfer-Encoding: 7bit 12 13 Find attached the following file: backup.tgz 14 -rw-r--r-- 1 root wheel 2022 Feb 27 04:13 backup.tgz 15 16 The MD5 checksum: MD5 (backup.tgz) = 1c03908b6ba8414a86f98410e4a9cbdf 17 18 --===========N=E=X=T===5facb5052688c84e499c086252c902db======== 19 Content-Type: application/octet-stream; name="backup.tgz" 20 Content-Transfer-Encoding: base64 21 Content-Disposition: attachment; filename="backup.tgz" 22 23 H4sIAAAAAAAAA+1XbXfaVhLO1+pXTDE+mFPLhsQvKQ7dyEa2lQLyIjltsmdPjpAuoEYvVC+22Tb/ 24 fZ+5kjD28XrTD+1+WM0HkLh3npm588zcwckyx12EIsrUTNxlL/4M6XQ7nePD7otOIY+/jw8Pjl50 25 [snip away many lines] 26 l2yeELKMa9XBzBXxnDLj668sTmgX6W+tG2qLp1207NzNqoRUY3BxDSnK//pffi211FJLLbXUUkst 27 tdRSSy211FJLLbXUUkst/2/yb0AcgmMAKAAA 28 29 --===========N=E=X=T===5facb5052688c84e499c086252c902db========-- Code:
1 MIME-Version: 1.0 2 Content-Type: multipart/mixed; 3 boundary="===========N=E=X=T===5facb5052688c84e499c086252c902db========" 4 Content-Transfer-Encoding: 7bit 5 6 This message is in MIME format. If you see this, 7 then your mail reader is not MIME-aware 8 9 --===========N=E=X=T===5facb5052688c84e499c086252c902db======== Lines 2-3 define the "Content-Type" as "multipart/mixed" and specify an unique boundary, "===========N=E=X=T===5facb5052688c84e499c086252c9 02db========", as end of part marker. The "Content-Transfer-Encoding" is set for 7 bits. Line 5-8 contain a message in case the mail reader program does not know anything of MIME. A MIME-aware mailer will just skip this message. In line 9, for the first time, we meet the unique boundary. Please note, and this is important, the '--' in front. Code:
10 Content-Type: text/plain; charset=ISO-8859-1 11 Content-Transfer-Encoding: 7bit 12 13 Find attached the following file: backup.tgz 14 -rw-r--r-- 1 root wheel 2022 Feb 27 04:13 backup.tgz 15 16 The MD5 checksum: MD5 (backup.tgz) = 1c03908b6ba8414a86f98410e4a9cbdf 17 18 --===========N=E=X=T===5facb5052688c84e499c086252c902db======== Code:
19 Content-Type: application/octet-stream; name="backup.tgz" 20 Content-Transfer-Encoding: base64 21 Content-Disposition: attachment; filename="backup.tgz" 22 23 H4sIAAAAAAAAA+1XbXfaVhLO1+pXTDE+mFPLhsQvKQ7dyEa2lQLyIjltsmdPjpAuoEYvVC+22Tb/ 24 fZ+5kjD28XrTD+1+WM0HkLh3npm588zcwckyx12EIsrUTNxlL/4M6XQ7nePD7otOIY+/jw8Pjl50 25 [snip away many lines] 26 l2yeELKMa9XBzBXxnDLj668sTmgX6W+tG2qLp1207NzNqoRUY3BxDSnK//pffi211FJLLbXUUkst 27 tdRSSy211FJLLbXUUkst/2/yb0AcgmMAKAAA 28 29 --===========N=E=X=T===5facb5052688c84e499c086252c902db========-- Line 21, the "Content-Disposition" defines it as an attachment with "backup.tgz" as filename. The base64 encoded data, not completely shown here, is in line 23-27, and is followed by the last boundary. To signal this boundary as the last one, it not only has an "--" at the start, but also at the end. As mentioned before the structure of a MIME message with a normal text part and an attachment part, is not really too complicated. 1.3 The mailing attachment(s) script 'matts' Features:
Examples of usage:
1.4 Man page The man page can be copied to the man directory. For example on OpenBSD: Code:
# cp man.1 /usr/local/man/man1 $Id: mail-attachments.xml,v 1.3 2009/06/24 01:36:24 j65nko Exp $
$Id: vbul-html.xsl,v 1.14 2008/09/12 03:44:16 j65nko Exp $
__________________
You don't need to be a genius to debug a pf.conf firewall ruleset, you just need the guts to run tcpdump Last edited by J65nko; 1st September 2022 at 07:17 AM. Reason: simplify the title |
Tags |
attachments, mail, mail attachments command line, mail command line, matts, mime 1.0 |
|
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
command-line-fu (digg like site with UNIX commands) | vermaden | Guides | 3 | 13th March 2009 03:56 PM |
avidemux2 command line | roddierod | FreeBSD General | 1 | 10th November 2008 08:27 PM |
OSS , playing a sound via command line.. | scotsman | FreeBSD General | 7 | 29th August 2008 08:01 PM |
Script to mail me resources usage? | bigb89 | Programming | 5 | 20th July 2008 06:58 AM |
How launch script or command from icewm? | aleunix | General software and network | 8 | 8th June 2008 09:12 PM |