-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile2mail.sh
executable file
·57 lines (41 loc) · 1.13 KB
/
file2mail.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#!/bin/sh
set -e
FROM="[email protected]"
TO="[email protected]"
PATH=/bin:/usr/bin:/usr/sbin
FILE=$1
if [ ! -r "$FILE" ]; then
echo "Usage: $0 file_to_send"
exit 64
fi
message=`mktemp -t file2kindle`
trap "rm -f $message" INT EXIT TERM
# XXX beteer to encode non ASCII filename but it is not easy to do in shell
# and I'm don't need it
ascii_file_name=$(basename "$FILE" | iconv -s -f UTF-8 -t US-ASCII)
file_mime_type=`file --brief --mime-type "$FILE"`
rfc822_date=$(date -R)
message_id="$(date +%s).$$@$(hostname)"
boundary="x________x"
cat << EOM > $message
To: ${TO}
From: ${FROM}
Subject: File for a kindle
Message-ID: <${message_id}>
Date: ${rfc822_date}
User-Agent: /bin/sh
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary="${boundary}"
This is a multi-part message in MIME format.
--${boundary}
Content-Type: text/plain; charset=US-ASCII
Pelase see attached file.
--${boundary}
Content-Type: $file_mime_type
Content-Transfer-Encoding: base64
Content-Disposition: attachment;
filename="${ascii_file_name}"
EOM
openssl base64 < $FILE >> $message
echo "--${boundary}--" >> $message
sendmail -i -t -f $FROM < $message