Sponsored Content
Top Forums Shell Programming and Scripting Need help in sending html mail with attachment Post 302933068 by kshji on Tuesday 27th of January 2015 09:52:38 AM
Old 01-27-2015
Here is example script used only ksh + base64 + sendmail
Code:
# sendmail inline text+html and attachments

get_mimetype()
{
  Xfname="$1"
  [ "$Xfname" = "" ] && echo "usage:$0 fname" >&2 && return 1
  [ ! -f "$Xfname" ] && echo "no file $Xfname" >&2 && return 2
  mtype=$(file --mime-type "$Xfname" )
  mtype=${mtype##* } # take last value from the answer, space is delim.
  echo "$mtype"
}

####
# parse file, expand variables and commands, dangerous if source file has not controlled
# but nice method to use templates to make dynamic output
parse_file()
{
 [ "$*" = "" ] && return
 [ ! -f "$1" ] && return
 eval echo  "\"$(cat $1 | sed 's+\"+\\"+g'   )\""
}

#################################################
# MAIN
#################################################
from="some@example.xx"
to="some@some.us"

epoc=$(printf "%(%#)T" now)  # other sh users can use date something ...
subject="Message $epoc"
procid=$$
boundary="My_/some1234.$epoc.$procid.0"
boundarybody="My_/some1234.$epoc.$procid.1"
body="This is my message body $epoc"
# of course you can make body using file as html block has done

# make mail and pipe for sendmail
# mail body text + html
{
echo "$(<<EOT
From: $from
To: $to
Subject: $subject
Mime-Version: 1.0
Content-Type: multipart/mixed; boundary="$boundary"

--$boundary
Content-Type: multipart/alternative; boundary="$boundarybody"

--$boundarybody

Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

$body

$(date), $(uptime)

--$boundarybody
Content-Type: text/html; charset=ISO-8859-1
Content-Disposition: inline

$(parse_file example.html)

--$boundarybody--
EOT
)"

# attachments = check the mime-type + encoded base64
# loop my all tst.* files ...
for file in tst.*
do

  [ ! -f "$file" ] && echo "Warning: $file not found, skip" >&2 && continue

  mimetype=$(get_mimetype "$file")

echo "$(<<EOT
--$boundary
Content-Type: $mimetype
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="$file"

  $(base64 "$file")

EOT
)"

done

# last boundary
echo "--${boundary}--"

} | tee mailcopy.tmp | /usr/lib/sendmail -t -oi
# mail has saved to file mailcopy.tmp = debug output

And example html body file (example.html):
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head><title>some</title>
</head>
<body>

<h3>Header $epoc</h3>
<p>Hello, world!
$(date), $(uptime)
</p>

</body>
</html>

 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Error when sending mail attachment

I have been sending an email attachment from my unix box, but keep getting an error? All though the recipient still receives the email and attachment. Will this error cause problems in the future and how to I cure it? $ uuencode PReSvPRINTER.txt file | mailx -s "File" me@world.com uuencode:... (1 Reply)
Discussion started by: dbrundrett
1 Replies

2. UNIX for Dummies Questions & Answers

Sending attachment thru a mail

Is there any way we can send file attachemnts through mails from a unix server. Does the 'mail' command have such an option ?? (1 Reply)
Discussion started by: Rohini Vijay
1 Replies

3. Shell Programming and Scripting

Sending HTML attachment through mail

Hi I am new to unix and scripting.I am trying to send a html file as an attachment. SUBJECT="Type of Exceptions in Application" TO=Sushovan.Samals@gmail.com SPOOLFILE=/data/reg/tlogs/Monitor.html #echo "Send the E-mail message..." uuencode $SPOOLFILE $SPOOLFILE | mailx -s "$SUBJECT" $TO... (2 Replies)
Discussion started by: sushovan
2 Replies

4. UNIX for Dummies Questions & Answers

How to send html file in a mail not as an attachment but it should display in the mail in table for

Hi The below script working when we are sending the html as attachment can u please guide how to send thesmae data in table form direct in the mail and not in mail attachment . cat Employee.sql SET VERIFY OFF SET PAGESIZE 200 SET MARKUP HTML ON SPOOL ON PREFORMAT OFF ENTMAP ON - HEAD... (0 Replies)
Discussion started by: mani_isha
0 Replies

5. Shell Programming and Scripting

Problem in sending inline html with an attachment using sendmail

MIME-Version: 1.0 Content-Type: multipart/mixed; boundary=border --border Content-Type: text/html Content-Disposition: inline <html><body><h2>This text should be displayed with html formatting</h2></body></html> --border Content-Type: text/plain Content-Disposition: attachment This text... (2 Replies)
Discussion started by: thulasidharan2k
2 Replies

6. UNIX for Dummies Questions & Answers

Sending html email with html attachment

Hello, I have a script which is sending an html file as an attachment. #!/usr/bin/ksh export MAILTO="user@company.com" export CONTENT="/usr/tmp/file.html" export SUBJECT="EmailSubject" ( echo "Subject: $SUBJECT" echo "MIME-Version: 1.0" echo "Content-Type: text/html" echo... (0 Replies)
Discussion started by: sreenathkg
0 Replies

7. Shell Programming and Scripting

Sending a csv attachment and html text together.

Hello, I need to send below text (in a file ABC)as html text in mail body and the same as csv attachment 1,2,3 4,5,6 7,8,9 but to send as html text in mailbody we use echo "Subject: Report " | cat - ABC | /usr/lib/sendmail -t a@xyz.com and to send as an attachment in csv format we... (9 Replies)
Discussion started by: skhichi
9 Replies

8. UNIX for Advanced & Expert Users

Sending mail with attachment

Hi, I am using Sun solaris OS unix server I am trying to send mail with an attachment using below script cat test.txt;uuencode test.txt test.txt|mailx -s "$subject" someone@somewhere I m getting mails but with no attachment. Hence i manipulate the script as below and i am... (2 Replies)
Discussion started by: sv0081493
2 Replies

9. Shell Programming and Scripting

HTML mail with Attachment

Hi, I am using the below code: #!/bin/ksh SUBJ="Send mail from Unix with file attachments" TO=sudha.viswanathan@jpmorgan.com CC=sudha.viswanathan@jpmorgan.com ( cat << ! To : ${TO} Subject : ${SUBJ} Cc : ${CC} ! cat << ! MIME-Version: 1.0 Content-Type: text/html `cat... (1 Reply)
Discussion started by: sudvishw
1 Replies

10. How to Post in the The UNIX and Linux Forums

Sending a mail with different attachment in AIX

How to Send a mail with multiple attachment also differenet extension using uuencode in AIX. Can you please help me. Its so urgent. Thanks. Regards, Swapnil ---------- Post updated at 05:37 AM ---------- Previous update was at 05:35 AM ---------- I have below code. But attachment... (1 Reply)
Discussion started by: Swapnil Mawle
1 Replies
TNEF(1) 						      General Commands Manual							   TNEF(1)

NAME
tnef - decode Microsoft's Transport Neutral Encapsulation Format SYNOPSIS
tnef [options] [FILE] tnef {--help | --version} DESCRIPTION
This manual page documents the tnef filter. tnef decodes e-mail attachments encoded in Microsoft's Transport Neutral Encapsulation Format (hereafter, TNEF), which "wraps" Microsoft e-mail attachments. Unfortunately, these "wrapped" attachments are inaccessible to any e-mail client that does not understand TNEF. Fortunately, the tnef fil- ter can be used by any MIME-aware client to unpack these attachments. OPTIONS
-f FILE, --file=FILE use FILE as input ('-' denotes stdin). When this option is omitted, tnef reads data from stdin. -C DIR, --directory=DIR unpack file attachments into DIR. -x SIZE, --maxsize=SIZE limit maximum size of extracted archive (bytes) -t, --list list attached files, do not extract. -w, --interactive, --confirmation ask for confirmation for every action. --overwrite when extracting attachments, overwrite existing files. --number-backups when extracting attachments, if file FOO will be overwritten, create FOO.n instead. --use-paths honor file pathnames specified in the TNEF attachment. For security reasons, paths to attached files are ignored by default. --save-body FILE Save message body data found in the TNEF data. There can be up to three message bodies in the file, plain text, HTML encoded, and RTF encoded. Which are saved is specified by the --body-pref option. By default the message bodies are written to a file named message with an extension based upon the type (txt, html, rtf). --body-pref PREF Specifies which of the possibly three message body formats will be saved. PREF can be up to three characters long and each charac- ter must be one of 'r', 'h', or 't' specifying RTF, HTML or text. The order is the order that the data will be checked, the first type found will be saved. If PREF is the special value of 'all' then any and all message body data found will be saved. The default is 'rht'. --save-rtf FILE DEPRECATED. Equivalent to --save-body=FILE --body-pref=r -h, --help show usage message. -V, --version display version and copyright. -v, --verbose produce verbose output. --debug enable debug output. EXAMPLE
The following example demonstrates typical tnef usage with a popular Unix mail client called "mutt". Step 1 -- Configure ~/.mailcap Mutt can't use tnef for its intended purpose until an appropriate content type definition exists in ~/.mailcap . Here's a sample defini- tion: application/ms-tnef; tnef -w %s This mailcap entry says that whenever the MIME content type: application/ms-tnef is encountered, use this command to decode it: tnef -w %s The latter command string invokes tnef, specifying both the -w option and the attachment (created as a temporary file) as command line arguments. Step 2 -- Add The Filter To $PATH Mutt can't invoke tnef if the filter isn't accessible via $PATH. Step 3 -- Test Mutt Use mutt to read a message that includes a TNEF attachment. Mutt will note that an attachment of type "application/ms-tnef is unsup- ported". Press the "v" key to open mutt's "view attachment" menu. Move the cursor over the TNEF attachment and press the enter key to "view" the attachment. Mutt will launch tnef and invoke it using the command line syntax specified in ~/.mailcap (step 1). tnef then decodes all file(s) included in the TNEF attachment, prompting for confir- mation prior to creating an individual file (refer to -w option above). -w is useful here because it gives the end user a chance to view the filename(s) included in the mail message. Note that Mutt's attachment menu also supports a pipe option, which permits the user to pipe attachments to an external filter (how conve- nient). So, to list the contents of a TNEF attachment prior to decoding it, press the "|" key and enter this command: tnef -t SEE ALSO
metamail(1), mailcap(4), mutt(1), other email clients. AUTHOR
Mark Simpson. REPORTING BUGS
Report bugs to Mark Simpson <verdammelt@users.sourceforge.net> OTHER REFERENCES
This web page: http://support.microsoft.com/support/kb/articles/Q136/2/04.asp describes how to configure Microsoft email clients so that the TNEF format is disabled when sending messages to non-TNEF-compatible clients. Filter TNEF MIME Decoder TNEF(1)
All times are GMT -4. The time now is 03:00 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy