sendmail with attachments | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

sendmail with attachments

Shell Programming and Scripting


Tags
attachements through sendmail, sendmail

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 09-04-2009
suthera suthera is offline
Registered User
 
Join Date: Jun 2009
Last Activity: 4 December 2009, 4:50 AM EST
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
sendmail with attachments

Hi,

I got the following script from Ygor on this site:



Code:
#!/usr/bin/ksh

export MAILTO="email_address"
export CONTENT="/export/home/aisdba/email_body.html"
export SUBJECT="subject of email"
(
 echo "Subject: $SUBJECT"
 echo "MIME-Version: 1.0"
 echo "Content-Type: text/html"
 echo "Content-Disposition: inline"
 cat $CONTENT
) | /usr/sbin/sendmail $MAILTO

It works perfectly and means you don't need to use MIME to format the body of the email message you just use the HTML file defined in the CONTENT section above (its brilliant).

HOWEVER

I need to send the email with an attachment, now this is not the same file used for the body of the email rather a completely different file e.g. a txt file say. The following doesn't work:


Code:
#!/usr/bin/ksh

export MAILTO="email_address"
export CONTENT="/export/home/aisdba/email_body.html"
export SUBJECT="subject of email"
(
 echo "Subject: $SUBJECT"
 echo "MIME-Version: 1.0"
 echo "Content-Type: text/html"
 echo "Content-Disposition: inline"
 cat $CONTENT
) | /usr/sbin/sendmail $MAILTO > /file_location/filename.txt


It just messes with the body of the email and causes HTML code to be displayed rather than interpreted by Outlook.

Any ideas on how to keep the HTML interpreted in the body of the email and send the email with an attachment?

Thanks
Alec

Last edited by Ygor; 09-06-2009 at 09:23 PM.. Reason: Added CODE tags.
Sponsored Links
    #2  
Old 09-04-2009
methyl methyl is offline Forum Advisor  
Advisor
 
Join Date: Mar 2008
Last Activity: 18 April 2014, 5:13 AM EDT
Posts: 6,396
Thanks: 287
Thanked 671 Times in 642 Posts
Take care - the script as posted would overwrite /file_location/filename.txt .
On my system the various Mime lines are surplus so I have removed them.

The construct below should cause the text file to be a proper attachment.

The full stop at the end of the message is assumed in some version of sendmail but there is no harm in including it.

On some unixes the text file conversion program is called unix2dos rather than ux2dos .


Code:
#!/usr/bin/ksh

export MAILTO="email_address"
export CONTENT="/export/home/aisdba/email_body.html"
export SUBJECT="subject of email"
(
echo "Subject: $SUBJECT"
# This appears in the mail body
cat $CONTENT
# The next line creates the attachment with a suitable extension to read
# with Windows notepad
ux2dos "/file_location/filename.txt" | uuencode myattach.txt
echo "."
) | /usr/sbin/sendmail $MAILTO

Sponsored Links
    #3  
Old 09-06-2009
Ygor's Avatar
Ygor Ygor is offline Forum Advisor  
Advisor
 
Join Date: Oct 2003
Last Activity: 21 November 2013, 8:38 AM EST
Location: 54.23, -4.53
Posts: 1,801
Thanks: 1
Thanked 110 Times in 98 Posts
This is a script I have to send html mail with a pdf attachment...
Code:
#!/usr/bin/ksh

export MAILTO="spam@ebay.com"
export SUBJECT="Mail Subject"
export BODY="/tmp/email_body.html"
export ATTACH="/tmp/attachment.pdf"
(
 echo "To: $MAILTO"
 echo "Subject: $SUBJECT"
 echo "MIME-Version: 1.0"
 echo 'Content-Type: multipart/mixed; boundary="-q1w2e3r4t5"'
 echo
 echo '---q1w2e3r4t5'
 echo "Content-Type: text/html"
 echo "Content-Disposition: inline"
 cat $BODY
 echo '---q1w2e3r4t5'
 echo 'Content-Type: application; name="'$(basename $ATTACH)'"'
 echo "Content-Transfer-Encoding: base64"
 echo 'Content-Disposition: attachment; filename="'$(basename $ATTACH)'"'
 uuencode --base64 $ATTACH $(basename $ATTACH)
 echo '---q1w2e3r4t5--'
) | /usr/sbin/sendmail $MAILTO

I'm using GNU uuencode, hence the --base64, so you may need to change it.
The Following User Says Thank You to Ygor For This Useful Post:
KRR (10-10-2013)
    #4  
Old 09-08-2009
suthera suthera is offline
Registered User
 
Join Date: Jun 2009
Last Activity: 4 December 2009, 4:50 AM EST
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks for both your replies. Ygor's reply is the one that seems to be working better for me, but the systems I use are very clunky so don't discount methyl's answer.

With Ygor's code I am now receiving an attachment, with the correct file name, however the file is empty, however I can see that the correct is on box, populated and ready to be attached.

I receive the following error at the end of my script run:

uuencode: illegal option -- base64

So I gather its the way I am encoding this attachment that is the problem. I don't know what exact uuencode I am using or the encoding options open to me, the actual uuencode file date is 2005. What other encoding options are there? My system is likely to use the most awkward one so lots of suggestions are welcome! Thanks
Sponsored Links
    #5  
Old 09-08-2009
Ygor's Avatar
Ygor Ygor is offline Forum Advisor  
Advisor
 
Join Date: Oct 2003
Last Activity: 21 November 2013, 8:38 AM EST
Location: 54.23, -4.53
Posts: 1,801
Thanks: 1
Thanked 110 Times in 98 Posts
See if you have mimencode or similar: ls /usr/bin/*encode
Sponsored Links
    #6  
Old 09-09-2009
suthera suthera is offline
Registered User
 
Join Date: Jun 2009
Last Activity: 4 December 2009, 4:50 AM EST
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks Ygor, nothing brought up in the search apart from uuencode sadly. My usual command to attach files to an email is:

uuencode $CSV_FILE $(basename $CSV_FILE)
) | if [ $DATABASE_LIVE = 1 ]
then mailx -b "$COPY_TO" -s "$SUBJECT" -r "$MAIL_FROM" "$MAIL_TO_LIVE"
else mailx -s "$SUBJECT" -r "$MAIL_FROM" "$MAIL_TO_TEST"
fi


However I wanted have the body of the email in MIME or even better (as per your coding) a HTML file that I can construct within my shell script. This makes the body of the email look good (HTML formatting) but also relevant to UNIX Box and Database(s) (with Shell Script variables).

Is there a way to combine the script? Many thanks.
Sponsored Links
    #7  
Old 09-09-2009
Ygor's Avatar
Ygor Ygor is offline Forum Advisor  
Advisor
 
Join Date: Oct 2003
Last Activity: 21 November 2013, 8:38 AM EST
Location: 54.23, -4.53
Posts: 1,801
Thanks: 1
Thanked 110 Times in 98 Posts
I don't think so. Either create a plain text message with uuencoded attachments (via mailx) or a MIME multi-part message with mimencoded attachments (via sendmail).
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
sendmail attachments rbatte1 UNIX for Dummies Questions & Answers 4 04-01-2009 05:50 AM
mail with attachments ninjadan Solaris 6 08-03-2008 10:57 PM
Attachments incredible Post Here to Contact Site Administrators and Moderators 6 07-07-2008 07:38 AM
multiple attachments edog Answers to Frequently Asked Questions 5 01-06-2003 01:09 PM



All times are GMT -4. The time now is 12:45 AM.