An open community 
of Macintosh users,
for Macintosh users.

FineTunedMac Dashboard widget now available! Download Here

Previous Thread
Next Thread
Print Thread
UNIX Shell Scripting Question
#8414 02/16/10 03:33 PM
Joined: Aug 2009
etaoin Offline OP
OP Offline

Joined: Aug 2009
*NOTE: Moderators, if this doesn't belong here, please move as you see fit.

I like to dabble in shell scripting on various UNIX-based systems we have to automate things. I mostly use C-shell. In a number of my scripts, I send e-mail notifications using the built-in mail functionality.

A simple script would contain these lines:

echo "From: tomjones@mycompany.com" > /tmp/message
echo "Subject: Notification" >> /tmp/message
echo "Hi John. Here is your notification." >> /tmp/message
mail johndoe@hiscompany.com < /tmp/message

How would I attach a PDF file to this simple message to send to John? The O'Reilly sendmail manual hasn't been much help. I've Googled around and can't find anything C-shell-ish that works. I've viewed the source of an incoming e-mail that has a PDF attachment and tried various iterations using the Content arguments I found. Can't get anything to work.

Any ideas would be appreciated. Thanks.


Re: UNIX Shell Scripting Question
etaoin #8470 02/19/10 12:45 AM
Joined: Aug 2009
Likes: 1
Offline

Joined: Aug 2009
Likes: 1
The problem with trying to do this from the command line is that you can't attach a binary file like a PDF to an email message/ Can't be done; the email standard doesn't allow it.

When you attach a file to an email, the email program does a lot of behind-the-scenes magic to make it happen.

First, the file you are attaching is encoded using a binary-to-ASCII encoder such as MIME base64 or UUEncode.

Then, a header is placed in the email message to alert the receiving email program that what follows isn't text, but rather is an encoded attachment.

Then the encoded attachment is placed in the message.

Then, another header is placed in the message to tell the email program, "Hey, this is the end of the attachment, so now you know to decode this much."

To do this from the command line would involve something like this:

echo "From: tomjones@mycompany.com" > /tmp/message
echo "Subject: Notification" >> /tmp/message
uuencode file.pdf file.pdf > /tmp/file.uue
echo ------=_NextPart_001_02B3_01CAB0BD.6062DDC0
Content-Type: multipart/alternative;
boundary="----=_NextPart_002_02B4_01CAB0BD.6062DDC0"'
echo '------=_NextPart_002_02B4_01CAB0BD.6062DDC0
Content-Type: text/plain;
charset=UTF-8
Content-Transfer-Encoding: quoted-printable' >> /tmp/message
>>/tmp/message
echo "Hi John. Here is your notification." >> /tmp/message
echo '------=_NextPart_002_02B4_01CAB0BD.6062DDC0
Content-Id: <image001.jpg@01CAB0BD.5E84F600>
Content-Type: image/PDF;
name=file.PDF
Content-Transfer-Encoding: UUEncode' >> /tmp/message
cat /tmp/message /tmp/file.uue >/tmp/message
mail johndoe@hiscompany.com < /tmp/message

There's no guarantee that this code will work as is; you might have to do some tweaking of the headers to make it function. And you may have to change the identifiers in the headers as well. Reading up on how MIME works would probably be useful; I don't know if the headers contain the byte length of each section or not, but if they do, you'll have to count the number of characters in the email text and the number of bytes in the encoded PDF file and change the headers accordingly.


Photo gallery, all about me, and more: www.xeromag.com/franklin.html
Re: UNIX Shell Scripting Question
tacit #8482 02/19/10 08:44 PM
Joined: Aug 2009
etaoin Offline OP
OP Offline

Joined: Aug 2009
To clarify, I wasn't trying to do this from the command line. I've written a C-shell script that runs on a Sun server. It delivers a PDF file attached to a simple e-mail for a given recipient. Here's the code I used that works just dandy. It's purpose is to send an electronic tearsheet -- proof of publication in our newspaper. The PDF file is of the page that the customer's ad appeared on.

echo "From: etearsheets@wichitaeagle.com" > ${MESSAGE}
echo "Subject: Wichita Eagle E-Tearsheet" >> ${MESSAGE}
echo " " >> ${MESSAGE}
echo " * * * THIS IS A SYSTEM-GENERATED MESSAGE. PLEASE DO NOT REPLY * * *" >> ${MESSAGE}
echo " " >> ${MESSAGE}
echo "Here is your e-tearsheet from The Wichita Eagle." >> ${MESSAGE}
echo "Valued Customer: ${CUSTOMER}" >> ${MESSAGE}
echo "Ad Number: ${ADNUMBER}" >> ${MESSAGE}
echo "Published on: ${WEEKDAY}, ${PUBDATE}, on Page ${PAGE}" >> ${MESSAGE}
echo " " >> ${MESSAGE}
uuencode ${PDFCOPIES}/${PDFFILE} ${PDFFILE} >> ${MESSAGE}
set RECIPIENT = ${EMAIL1}
mail ${RECIPIENT} < ${MESSAGE}

Re: UNIX Shell Scripting Question
etaoin #11937 09/22/10 02:21 PM
Joined: Aug 2009
etaoin Offline OP
OP Offline

Joined: Aug 2009
Just a follow-up if anyone's interested.

After launching this project with gleeful acceptance by most of our customers, there were a few recipients who complained that the PDF file attachment was coming across in-line in the body of the e-mails as a bunch of gibberish. 95% of the recipients, including me using Entourage to view e-mails, were doing OK. This small group wasn't. Need to search for better encoding, maybe?

Since a couple of the recipients used GMAIL accounts, I set one up for myself thinking this might be a "lowest common denominator" for testing. Indeed, when I triggered my script to e-mail this account, all I saw was a long string of gibberish following the few lines of greeting.

I turned to trying MIME encoding variations. Nothing seemed to work. Hit one wall after another. I finally read somewhere that uuencode is not MIME-compatible. Sheesh!

Hours upon hours of Googling and testing later, I finally discovered that if I used MAILX instead of MAIL, my original coding worked! All customers seem to be happy now.

I've got no idea what the difference is, but there must be something special there.

Just in case you were interested.

Re: UNIX Shell Scripting Question
etaoin #11942 09/22/10 05:21 PM
Joined: Aug 2009
Offline

Joined: Aug 2009
Originally Posted By: etaoin
Hours upon hours of Googling and testing later, I finally discovered that if I used MAILX instead of MAIL, my original coding worked! All customers seem to be happy now.

I've got no idea what the difference is, but there must be something special there


well well well I had no idea the "uuencode" function existed... good to know!

as to your encoding problem, the most thorough way to troubleshoot would be to compare emails that work and don't work, and see what the difference is. You may need to use a hex view to spot subtle problems.

It would not surprise me if it requires a particular kind of linefeed in the email in general, or in the block of uuencoded text.

Code:
apple:~ virtual1 $ cat /System/Library/CoreServices/SystemVersion.plist | uuencode - | sed "s/\$/`echo $'\r'`/" | xxd | head -n 10
0000000: 6265 6769 6e20 3634 3420 2d0d 0a4d 2f23  begin 644 -..M/#
0000010: 5d58 3b36 5040 3d46 3552 3c56 454f 3b43  ]X;6P@=F5R<VEO;C
0000020: 5442 2c32 5850 2842 2145 3b46 2d4f 3926  TB,2XP(B!E;F-O9&
0000030: 454e 3953 5442 3535 3126 2b33 4042 2f53  EN9STB551&+3@B/S
0000040: 582a 2f22 2524 3354 2d34 0d0a 4d36 3521  X*/"%$3T-4..M65!
0000050: 2528 2721 4c3a 372d 5428 2521 3530 4451  %('!L:7-T(%!50DQ
0000060: 2930 5260 422b 325c 4f30 3721 503b 2634  )0R`B+2\O07!P;&4
0000070: 4f2b 5431 3431 2221 3033 2445 3335 2260  O+T141"!03$E35"`
0000080: 512b 4360 4f2b 5435 2e0d 0a4d 2842 6042  Q+C`O+T5...M(B`B
0000090: 3a27 3154 3c23 484f 2b57 3d57 3d52 5941  :'1T<#HO+W=W=RYA


looks like uuencode outputs unix linefeeds. (OA only) The above fixes that problem. I hate it when I google for an answer and find ten copies of the wrong answer. sed 's/$'"/`echo \\\r`/" does NOT work. Anyway, the above converts the output of uuencode to dos (CRLF) linefeeds. Email apps can be fickle though, if they see a single unix linefeed in the email it could stop them in their tracks so you may have to make sure the entire email is CRLF. Build the email the way you were before with >> and then cat THAT through the sed to a second file, and email that file, so the entire email is dos style linefeeds.

so much fun dealing with email apps that expect everything to be provided to them a a very peticular way.




I work for the Department of Redundancy Department

Moderated by  alternaut, cyn 

Link Copied to Clipboard
Powered by UBB.threads™ PHP Forum Software 7.7.4
(Release build 20200307)
Responsive Width:

PHP: 7.4.33 Page Time: 0.023s Queries: 24 (0.013s) Memory: 0.5943 MB (Peak: 0.6643 MB) Data Comp: Zlib Server Time: 2024-04-16 08:12:04 UTC
Valid HTML 5 and Valid CSS