Discussion:
command line email with inline html attachment
(too old to reply)
Rob Dover
2003-08-21 23:05:55 UTC
Permalink
I have looked all over and haven't been able to get a solid answer to
this.
I am looking for some form of a mailer utility that will allow me to
either attach or pipe an html file into an email message such that it
will display in a graphical client when received. I have found
several that will let me attach the html file as an attachment but the
attachment have to be manually launched upon receipt.
I am trying to send the html formatted output of a linux log analyzer
to an email account where it will be read with either M$ Outlook or
Outlook Express. I would prefer when the message is opened the html is
displayed automatically.
I understand I need to get a couple of specific headers into the
message but haven't been able to figure out how to do it.
Any help or suggestions appreciated.
TIA -Rob-
Sean Anderson
2003-08-22 13:52:44 UTC
Permalink
I've been doing this exact stuff at work and I didn't use anything special -
I just looked up a bunch of stuff on Google (Google is your friend) about
the mime types etc. I did bookmark a number of sites, bit I don't have
convenient access to them at the moment (oh well)

Anyway, I wrote a ksh script which simply uses mailx with the -t option and
redirected a file into it. I'm not sure if mailx is on linux, but basically
you need to get the mailer to use the headers you give it.

mailx -t < ${MAILTEMP}

MAILTEMP is just a temp file which contains everything needed to send a mail
and I just echoed each line into this file.

Here is a cut and pasted bit from the script - there's a fair bit more to
the script, but it's not necessary for you to get the idea.

==== BEGIN EXAMPLE ====
# Create file for mailing
MAILTEMP=/tmp/mailfile.${$}

# Construct headers
echo "To: ${TO}" > ${MAILTEMP}
echo "Subject: ${SUBJECT}" >> ${MAILTEMP}
echo "From: ${FROM}" >> ${MAILTEMP}
if [[ IMPORTANT -eq 1 ]]; then
echo "Importance: high" >> ${MAILTEMP}
fi
echo "MIME-Version: 1.0" >> ${MAILTEMP}
echo "Content-type: text/html; charset=iso-8859-1" >> ${MAILTEMP}

# Require blank line between headers and body
echo >> ${MAILTEMP}

# Now add HTML document to send
cat ${HTMLFILE} >> ${MAILTEMP}

# And send it
mailx -t < ${MAILTEMP}

rm ${MAILTEMP}
===== END EXAMPLE =====

I've taken all the error checking out for clarity. I also had a bunch of
other options - i.e. here is the help for it. If you get into attachments,
you need to change the content type to multipart mixed - again - it's all
out there, it just takes a little digging.

Hope this helps,
Cheers,
Sean.

=== HELP OUTPUT ====
usage:
mail_html_content options [inputfile]

options:
--to "to list" # list of addresses separated by commas,
this is mandatory
[--from "from list"] # address of sender, defaults to "<real name
cut>"
[--subject "subject"] # subject for e-mail
[--important] # flag e-mail as important/priority
[--addhtmlheaders] # add <HTML><BODY> and </BODY></HTML> tags
to top and bottom
[--preformatted] # input is preformatted
{--style stylesheet} # include style sheet. File should not have
<STYLE> tags
# included
# this option can be included more than once
# this option is only valid
when --addhtmlheaders is
# also specified, otherwise it is ignored
{--attach-binary "file"} # add file as a binary attachment, can be
anything
{--attach-text "file"} # add file as a text attachment, this must
be ascii text
{--attach-image (gif|jpeg) "file"} # add image attachment for html
reference. This can be
# referenced with <img
src="file"> in the html document being sent
{--attach-preformatted "file"} # Add preformatted mime attachments.
This generally only makes sense
# in combination with the --boundary
option
[--boundary "boundary text"] # for multipart messages, use this
boundary text
# useful if multipart message is
constructed outside of this script
# and therefore implies a multipart
message will be sent
[--nomail] # show output on stdout instead of mailing -
good for debugging
[inputfile] # input document. If missing, stdin used
Rob Dover
2003-08-25 00:24:48 UTC
Permalink
Post by Sean Anderson
I've been doing this exact stuff at work and I didn't use anything special -
I just looked up a bunch of stuff on Google (Google is your friend) about
the mime types etc. I did bookmark a number of sites, bit I don't have
convenient access to them at the moment (oh well)
Anyway, I wrote a ksh script which simply uses mailx with the -t option and
redirected a file into it. I'm not sure if mailx is on linux, but basically
you need to get the mailer to use the headers you give it.
mailx -t < ${MAILTEMP}
What flavour of *nix are you running Sean? I haven't been able to find
a GPL version of mailx that supports the -t switch. There are a couple
of man pages that indicate a mailx with a -t switch but they seem to
be only for SunOS :(
My linux (RH 7.3) doesn't use a true mailx. Their mailx package
installs their generic mail util only.
I've tried a number of other MUA's as well but still can't find one
that will let me add or modify a specific header.
Thanks -Rob-
Sean Anderson
2003-08-27 09:24:51 UTC
Permalink
Ok, I had a look around, though I haven't tested, you could try sendmail. If
I read the man page correctly, you can use the same "-t" option

i.e. /usr/sbin/sendmail -t < ${MAILFILE}
Post by Rob Dover
Post by Sean Anderson
I've been doing this exact stuff at work and I didn't use anything special -
I just looked up a bunch of stuff on Google (Google is your friend) about
the mime types etc. I did bookmark a number of sites, bit I don't have
convenient access to them at the moment (oh well)
Anyway, I wrote a ksh script which simply uses mailx with the -t option and
redirected a file into it. I'm not sure if mailx is on linux, but basically
you need to get the mailer to use the headers you give it.
mailx -t < ${MAILTEMP}
What flavour of *nix are you running Sean? I haven't been able to find
a GPL version of mailx that supports the -t switch. There are a couple
of man pages that indicate a mailx with a -t switch but they seem to
be only for SunOS :(
My linux (RH 7.3) doesn't use a true mailx. Their mailx package
installs their generic mail util only.
I've tried a number of other MUA's as well but still can't find one
that will let me add or modify a specific header.
Thanks -Rob-
Loading...