Email with multiple attachments & HTML body


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Email with multiple attachments & HTML body
# 8  
Old 02-24-2011
Email with attachment & HTML body

I'm new to UNIX and need some assistance in creating/sending an email containing HTML body and a .csv attachment. I've found
numerous posts on the forum that discuss this, however none of the solutions work. I've created a KSH script running on UNIX AIX.
I found a post with a solution that works on a linux box which has uuencode --base64, but it did not work for me. The code ran
but did not send the email.
Any suggestions......

My .html file looks something like this:
Code:
# cat email.html
<html>
<body>
Sample HTML file</p>
</body>
</html>

I'm able to send the html formatted email with no attachments using the following code:
Code:
cat email.html \
| /usr/lib/sendmail -t "someone@email.com"

When I update the code to include the attachement as follows, the email is sent with the attachment but the email body is missing:
Code:
echo "To: $p_email\nSubject: $month $year Subject\n" > $mailfile
(cat email.html; \
uuencode ${ofile1} ${ofile1} >> $mailfile) \
| /usr/lib/sendmail -t < ${mailfile}


Last edited by Scott; 02-24-2011 at 03:46 PM.. Reason: Please use code tags
# 9  
Old 02-24-2011
I developed this on AIX:

cat html_email_with_attachments.body
Code:
<html>
<body>
Body of email.
</body>
</html>

cat html_email_with_attachments
Code:
#!/usr/bin/ksh

# http://en.wikipedia.org/wiki/MIME

export MAILTO=abc@abc.com
export SUBJECT=$0
export BODY=html_email_with_attachments.body
export ATTACH=cards.txt
EMAIL_BOUNDARY_STRING=Z${RANDOM}${RANDOM}${RANDOM}${RANDOM}

(
 echo "To: $MAILTO"
 echo "Subject: $SUBJECT"
 echo "MIME-Version: 1.0"
 echo 'Content-Type: multipart/mixed; boundary="${EMAIL_BOUNDARY_STRING}"'
 echo '--${EMAIL_BOUNDARY_STRING}'

 echo "Content-Type: text/html"
 echo "Content-Disposition: inline"
 cat $BODY
 echo '--${EMAIL_BOUNDARY_STRING}'

 echo 'Content-Type: application/octet-stream; name="'$(basename $ATTACH)'"'
 echo "Content-Transfer-Encoding: base64"
 echo 'Content-Disposition: attachment; filename="'$(basename $ATTACH)'"'
 uuencode -m $ATTACH $(basename $ATTACH)
 echo '--${EMAIL_BOUNDARY_STRING}'

 echo 'Content-Type: application/octet-stream; name="'$(basename $ATTACH)'"'
 echo "Content-Transfer-Encoding: base64"
 echo 'Content-Disposition: attachment; filename="'$(basename $ATTACH)'"'
 uuencode -m $ATTACH $(basename $ATTACH)
 echo '--${EMAIL_BOUNDARY_STRING}--'

) | /usr/sbin/sendmail $MAILTO

then:
Code:
./html_email_with_attachments

# 10  
Old 02-24-2011
Thanks, that works and the email is sent with the attachment and the formatted HTML. The only issue is I'm getting 2 errors as follows:

-m: No such file or directory
-m: No such file or directory

When I man uuencode I don't see the -m option. When I remove this from the code, the email is sent with 2 attachments (both the same). When I remove the last block of code in your example, the email is sent with 2 attachments, the second file is an empty notepad .txt file. Can you explain this?
# 11  
Old 02-24-2011
First, you are running AIX? on 5.3:

Code:
Syntax

       uuencode [ -m ] [ SourceFile ] OutputFile

Description

       The uuencode command converts a binary file to ASCII data. This is useful before using BNU (or uucp) mail to send the file to a remote
       system. The uudecode command converts ASCII data created by the uuencode command back into its original binary form.

       The uuencode command takes the named SourceFile (default standard input) and produces an encoded version on the standard output. The encoding
       uses only printable ASCII characters, and includes the mode of the file and the OutputFile filename used for recreation of the binary image
       on the remote system.

       Use the uudecode command to decode the file.

Flags
       -m
            Encode the output using the MIME Base64 algorithm. If -m is not specified, the old uuencode algorithm will be used.

Second, I wrote the code as:
Code:
 echo "Content-Type: text/html"
 echo "Content-Disposition: inline"
 cat $BODY
 echo '--${EMAIL_BOUNDARY_STRING}'

Code:
 echo 'Content-Type: application/octet-stream; name="'$(basename $ATTACH)'"'
 echo "Content-Transfer-Encoding: base64"
 echo 'Content-Disposition: attachment; filename="'$(basename $ATTACH)'"'
 uuencode -m $ATTACH $(basename $ATTACH)
 echo '--${EMAIL_BOUNDARY_STRING}'

Code:
 echo 'Content-Type: application/octet-stream; name="'$(basename $ATTACH)'"'
 echo "Content-Transfer-Encoding: base64"
 echo 'Content-Disposition: attachment; filename="'$(basename $ATTACH)'"'
 uuencode -m $ATTACH $(basename $ATTACH)
 echo '--${EMAIL_BOUNDARY_STRING}--'

To show how different 'blocks' or things can be included in the email. I believe this format can be repeated many times.

Notice the last:
Code:
echo '--${EMAIL_BOUNDARY_STRING}--'

is slightly different. The trailing -- specifies the end of the email.

If you post your code and the output we can look at it.
# 12  
Old 02-24-2011
Thank you. it is cognitively
# 13  
Old 02-24-2011
I'm runing on HP-UX B.11.11

I used your code exactly the way you wrote it only changing the values in the export statment for the various variables (ie email address, html body, attachment & subject). On the fist run I received the email with the html body and the attachment but received the errors regarding the -m flag.

On a subsequent run, I removed the -m flag from both blocks and received the email with the html body but to copies of my $ATTACH file were attached. The code is as follows:
Code:
EMAIL_BOUNDARY_STRING=Z${RANDOM}${RANDOM}${RANDOM}${RANDOM}
(
echo "To: $MAILTO"
echo "Subject: $SUBJECT"
echo "MIME-Version: 1.0"
echo 'Content-Type: multipart/mixed; boundary="${EMAIL_BOUNDARY_STRING}"'
echo '--${EMAIL_BOUNDARY_STRING}'
echo "Content-Type: text/html"
echo "Content-Disposition: inline"
cat $BODY
echo '--${EMAIL_BOUNDARY_STRING}'
echo 'Content-Type: application/octet-stream; name="'$(basename $ATTACH)'"'
echo "Content-Transfer-Encoding: base64"
echo 'Content-Disposition: attachment; filename="'$(basename $ATTACH)'"'
uuencode $ATTACH $(basename $ATTACH)
echo '--${EMAIL_BOUNDARY_STRING}'
echo 'Content-Type: application/octet-stream; name="'$(basename $ATTACH)'"'
echo "Content-Transfer-Encoding: base64"
echo 'Content-Disposition: attachment; filename="'$(basename $ATTACH)'"'
uuencode $ATTACH $(basename $ATTACH)
echo '--${EMAIL_BOUNDARY_STRING}--'
) | /usr/sbin/sendmail $MAILTO

On the last run, I removed the 2nd block which references the $ATTACH file and this time I received the email with the html body, my $ATTACH file and a 2nd attached file that was an empty .txt file. The code is as follows:
Code:
EMAIL_BOUNDARY_STRING=Z${RANDOM}${RANDOM}${RANDOM}${RANDOM}
(
echo "To: $MAILTO"
echo "Subject: $SUBJECT"
echo "MIME-Version: 1.0"
echo 'Content-Type: multipart/mixed; boundary="${EMAIL_BOUNDARY_STRING}"'
echo '--${EMAIL_BOUNDARY_STRING}'
echo "Content-Type: text/html"
echo "Content-Disposition: inline"
cat $BODY
echo '--${EMAIL_BOUNDARY_STRING}'
echo 'Content-Type: application/octet-stream; name="'$(basename $ATTACH)'"'
echo "Content-Transfer-Encoding: base64"
echo 'Content-Disposition: attachment; filename="'$(basename $ATTACH)'"'
uuencode $ATTACH $(basename $ATTACH)
echo '--${EMAIL_BOUNDARY_STRING}'
) | /usr/sbin/sendmail $MAILTO

Moderator's Comments:
Mod Comment Please use code tags

Last edited by Scott; 02-24-2011 at 03:49 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. HP-UX

Email Using uuenview w/ Multiple Attachments

HP-UX mbhp7640 B.11.31 U ia64 4294967295 unlimited-user license Our database builds a MIME compliant html email, then cats that to sendmail - no problem. Due to horrible issues with the native uuencode, we long ago began using uuenview to encode our attachments - no problem. An example is... (1 Reply)
Discussion started by: bubba77
1 Replies

2. Shell Programming and Scripting

Regarding guidance related to HTML table in email body

Hello All, I have a query here. I am sending an HTML table(which I am creating it by a call to REST API, in a LINUX box) and from there I have to send it into an email. So following are the poins on same: As data is not static so it is writing Dynamic data and creating HTML file. There is... (9 Replies)
Discussion started by: RavinderSingh13
9 Replies

3. Shell Programming and Scripting

HTML table in email body using C Shell

I am using Sun Solaris ver. 5.10 and trying to send an HTML table in email body using mail command in C shell script. I tried following commands:- #1 mail -m "MIME-Version: 1.0;Content-type:text/html;charset=UTF-8" receiver@mail.com < file.html #2 mail -m "Content-type: text/html;" -s "This... (4 Replies)
Discussion started by: jnrohit2k
4 Replies

4. Shell Programming and Scripting

SQL query output convert to HTML & send as email body

Hi , I have a sql query in the unix script ,whose output is shown below.I want to convert this output to HTML table format & send email from unix with this table as email body. p_id src_system amount 1 A 100 2 B 200 3 C ... (3 Replies)
Discussion started by: jagadeeshn04
3 Replies

5. Shell Programming and Scripting

Email body not formatted with html and sendmail

Hi All, I am trying to send the contents of a file as email body. I am using html email and sendmail option of unix. I am using the below piece of code for the same : #!/usr/bin/ksh export MAILTO="email@domain.com" export SUBJECT="Report" export BODY="file_directory_path/test_file.txt"... (1 Reply)
Discussion started by: rockygsd
1 Replies

6. Shell Programming and Scripting

Sending an email with a body and attachments using uuencode

Hi, Im having a bit of an issue with using the uuencode command and sending out an email. My aim is to send an email out which has a body and also have attachments. Currently I can either get one or the other and not both on the same email. uuencode... (4 Replies)
Discussion started by: 02JayJay02
4 Replies

7. UNIX for Advanced & Expert Users

Mutt for html body and multiple html & pdf attachments

Hi all: Been racking my brain on this for the last couple of days and what has been most frustrating is that this is the last piece I need to complete a project. There are numerous posts discussing mutt in this forum and others but I have been unable to find similar issues. Running with... (1 Reply)
Discussion started by: raggmopp
1 Replies

8. Shell Programming and Scripting

How to send email with multiple attachments ?

Hello , I am trying to send an email with two attachments . I have tried all previous suggestion in this forum but none worked. I could send one attachment in an email by uuencode $file "$file" | mailx -m -s "File" xxx@xx.com but unable to send multiple attachments . I have tried ... (8 Replies)
Discussion started by: RaviTej
8 Replies

9. Red Hat

Send HTML body and HTML attachment using MUTT command

Hi there.. I need a proper "mutt" command to send a mail with html body and html attachment at a time. Also if possible let me know the other commands to do this task. Please help me.. (2 Replies)
Discussion started by: vickramshetty
2 Replies

10. Shell Programming and Scripting

Referring to attached images in html email body through mailx

encoding type for images? (5 Replies)
Discussion started by: biswasbaishali
5 Replies
Login or Register to Ask a Question