![]() |
|
Guides All Guides and HOWTO's. |
![]() |
|
Thread Tools | Display Modes |
|
|||
![]()
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 |
|
|||
![]()
A tar archive containing the matts script and man page.
To unpack and install: Code:
$ cp /home/xxx/matts.tgz /tmp $ cd /tmp $ tar xvzf matts.tgz Code:
# cp matts /usr/local/bin chown root:wheel /usr/local/bin/matts Copy the man page: Code:
# cp matts.1 /usr/local/man/man1 It is nearly 4 o'clock in the morning and I really have to go to bed now: ![]()
__________________
You don't need to be a genius to debug a pf.conf firewall ruleset, you just need the guts to run tcpdump |
|
||||
![]() Quote:
![]() http://www.ted.com/talks/rives_on_4_a_m.html
__________________
UNIX was not designed to stop you from doing stupid things, because that would also stop you from doing clever things. |
|
||||
![]()
@J65nko
I used my script for that: Code:
#! /bin/sh # subject SUBJ="$( hostname -s ) | $( basename ${0} ) | report" __usage() { echo "usage: $( basename ${0} ) email@domain.com message.txt [file1] [file2]" exit 1 } __check_file() { if [ ! -f "${1}" ] then echo "error: file '${1}' does not exist" exit 1 fi } __check_mail() { if [ ${?} -ne 0 ] then echo "error: mail(1) failed to send message" exit 1 fi } [ ! ${#} -ge 2 ] && __usage __check_file "${2}" MAIL="${1}" MESG="${2}" case ${#} in (2) mail -s "${SUBJ}" "${MAIL}" < "${MESG}" __check_mail ;; (*) shift shift BODY="$( basename ${0} ).body" cat "${MESG}" > "${BODY}" for FILE in "${@}" do __check_file "${FILE}" uuencode "${FILE}" "${FILE}" >> "${BODY}" done mail -s "${SUBJ}" "${MAIL}" < "${BODY}" __check_mail ;; esac
__________________
religions, worst damnation of mankind "If 386BSD had been available when I started on Linux, Linux would probably never had happened." Linus Torvalds Linux is not UNIX! Face it! It is not an insult. It is fact: GNU is a recursive acronym for “GNU's Not UNIX”. vermaden's: links resources deviantart spreadbsd |
|
|||
![]()
@Carpetsmoker
What? You only read my contributions 3 months after posting? ![]() @Vermaden Mail messages created by matts are Mime 1.0 compliant, meaning that other mail clients, e.g. gmail or Yahoo mail or alpine, will recognize the attachments and will offer a simple way to download or retrieve such an attachment. A script which only uses uuencode requires a lot of manual intervention by the user to convert the uuencoded mail message into a file.
__________________
You don't need to be a genius to debug a pf.conf firewall ruleset, you just need the guts to run tcpdump |
|
||||
![]() Quote:
Havent checked if it created MIME 1.0 compilant messages, but Entourage recognized each attachement properly as separate attached file + message content.
__________________
religions, worst damnation of mankind "If 386BSD had been available when I started on Linux, Linux would probably never had happened." Linus Torvalds Linux is not UNIX! Face it! It is not an insult. It is fact: GNU is a recursive acronym for “GNU's Not UNIX”. vermaden's: links resources deviantart spreadbsd |
|
|||
![]()
A new version that instead of the older MD5 now generates a SHA256 checksum file of the attachments.
Multiple file attachments are now dealt with correctly. The attached tarball includes a sample script 'ezmatts' for repeated attachment mailing: Code:
#!/bin/sh # For repeated mails, for example sending backups to mail # You only need to specify the file(s) to attach from the command line # Or specify them here # If matts lives in '/usr/local/bin' or '~/bin', you hav to remove './'i # from './matts' # $ ./matts-them -n *c # remember that the options will be parsed by the shell! # so you need matching single or double quotes to delimit the options # ---- Usage of ./matts ------------ # -f : set From: # -t : set To: # -s : set Subject: # -c : set Cc: # -b : set Bcc: # -m : message text # -n : do NOT send, just show # -h : help, show this message # # For inline #./matts \ #-f johndoe@lenap.utp.xnet \ #-t johndoe+BUP@gmail.com \ #-s 'The newest matts with SHA256 instead of MD5' \ #-m 'The old version as well as the latest one ....' \ #-n \ #matts matts.prev # --- Make backup of 'zeno's /etc/wireguard directory # maybe not too smart to mail wireguard stuff it to gmail .... BACKUP='zeno-bup-wireguard.tgz' doas tar cvzf $BACKUP /etc/wireguard ./matts \ -f johndoe@lenap.utp.xnet \ -t johndoe+BUP@gmail.com \ -s 'Host zeno backup of /etc/wireguard' \ -n \ $BACKUP #-m 'The files you requested. Regards' \ #-n \ # Specifing the files on the command line # $ ./ezmatts *txt # shell variable "$@" contains whatever you specified on the command # line # ./matts \ # -f johndoe@lenap.utp.xnet \ # -t johndoe+BUP@gmail.com \ # -s 'Project homework assignment' \ # -n \ # "$@"
__________________
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; 3rd August 2022 at 03:21 AM. |
![]() |
Tags |
attachments, mail, mail attachments command line, mail command line, matts, mime 1.0 |
Thread Tools | |
Display Modes | |
|
|
![]() |
||||
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 |