SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Wizard Wolf_22's Avatar
    Join Date
    Jul 2005
    Posts
    1,711
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Using ksh and sendmail to send an html message w/ attachment?

    Not sure if anyone around here can help me with this but I'm struggling w/ using ksh (Korn Shell) on a Red Hat machine to send an HTML message with a simple txt file attachment...

    Here's what I have so far:

    Code:
    #!/bin/ksh
    
    attachment="archive/$logfilename"
    
    subject="Foobar"
    email_to="mister.foo@bar.com"
    email_from="foo@bar.com"
    
    email_msg="
    <!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\"><head>
    <meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\">
    <title>Foobar</title>
    </head>
    
    <body>
    <div>
    Test
    </div>
    </body>
    </html>
    "
    
    cat <<- _EOF_ | /usr/lib/sendmail -oi -t
    From: $email_from
    To: $email_to
    Subject: $subject
    Date: $(date +%Y%m%d)
    Return-Path: help@foobar.net
    MIME-Version: 1.0
    Content-Type: multipart/mixed; boundary="MAIL_BOUNDARY";
    Content-Transfer-Encoding: 7bit
    
    --MAIL_BOUNDARY
    Content-Type: text/html; charset="US-ASCII"
    Content-Transfer-Encoding: 7bit
    
    $email_msg
    
    if [[ -n $attachment && -f $attachment && -r $attachment ]]; then
    --MAIL_BOUNDARY
    Content-Type: application/html; name="$logfilename";
    Content-Disposition: attachment; filename="$logfilename";
    
    fi
    
    _EOF_
    
    --MAIL_BOUNDARY
    When I send the message, I get it and there is a txt file attached, but the contents is never correct because it outputs shell code as the contents of the attachment--which tells me I have my MIME headers screwed up.

    Any input / feedback is appreciated. The txt file is just a log file for a process we have.

    Do I have to somehow use ksh to grab the contents of the file, store it in a variable, and then echo that variable inside the attachment area??? Pretty lost right now... (ksh is something I'm not very familiar with.)

  2. #2
    SitePoint Wizard Wolf_22's Avatar
    Join Date
    Jul 2005
    Posts
    1,711
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Figured it out...

    Make a file named "whatever.sh" and throw that onto a Linux box. Paste the following into it, change the data, and you should be good to go (at least, I was!):

    Code:
    #!/bin/ksh
    
    attachment="archive/$logfilename"
    payload="archive/$contactsarchive"
    
    subject="Foobar"
    email_to="Wolf_22@sitepoint.com"
    email_from="foo@bar.com"
    
    email_msg="
    <!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\"><head>
    <meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\">
    <title>Foobar</title>
    <style type=\"text/css\">
    html,body{margin:0;padding:0;height:100%}
    body{height:100%;line-height:100%;background-image:url('http://foobar.com/whatever.png');background-position:center}
    </style>
    </head>
    
    <body>
    <!-- Your HTML goes here. Just make sure it's in a table because most e-mail apps (i.e. - Outlook) tend to generally prefer tables.) Outlook 2007 is particularly picky about stuff like this.
    </body>
    </html>
    "
    
    while read line
    do
      log="$line $IFS $log"
    done < "$attachment"
    
    while read line
    do
      payload="$line $IFS $payload"
    done < "$payload"
    
    cat <<- _EOF_ | /usr/lib/sendmail -t
    From: $email_from
    To: $email_to
    Subject: $subject ($current_time)
    Date: $(date +%Y%m%d)
    Return-Path: foo_help@bar.com
    MIME-Version: 1.0
    Content-Type: multipart/mixed; boundary="MAIL_BOUNDARY";
    Content-Transfer-Encoding: 7bit
    This is a message with multiple parts in MIME format.
    --MAIL_BOUNDARY
    Content-Type: text/html; charset="US-ASCII"
    $email_msg
    --MAIL_BOUNDARY
    Content-Type: application/html; name="$logfilename";
    Content-Disposition: attachment; filename="$logfilename";
    --MAIL_BOUNDARY
    $log
    --MAIL_BOUNDARY
    Content-Type: application/html; name="$contactsarchive";
    Content-Disposition: attachment; filename="$contactsarchive";
    --MAIL_BOUNDARY
    $payload
    --MAIL_BOUNDARY--
    Notes: all this does is send an e-mail to whoever with whatever log files. Not sure if the headers are as universal as they should be, but it worked for me when I tested it against Gmail and Outlook 2007. The log files get attached and everything works great. I guess some apps exist in the Linux world that make this whole process a bit more modernized without being forced to manually mess with headers, but I learned about this after the fact... Plus, I could never find anything on the next about using those "easier" solutions with special e-mail templates as above. So it's all good.

    Hope this helps someone in the future. Questions or comments welcomed.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •