How To Send MIME Attachments Using EEM

Document

Mar 31, 2010 3:18 PM
Mar 31st, 2010

It is very easy to send email messages using EEM.  Often times, it is sufficient to simply include CLI output or other messages in the body of the email.  However, some output may be quite large, or you may have a need to send binary attachments (e.g. Embedded Packet Capture files).  In those cases, it would be better to use MIME encoding, and add the data as file attachments.  Here is a short proof-of-concept EEM script that demonstrates how to do this.

::cisco::eem::register_event_none

namespace import ::cisco::eem::*

namespace import ::cisco::lib::*

# First, create the headers and body of your message

set email_body_pre "Mailservername: $_email_server
From: $_email_from
To: $_email_to
Cc:
Subject: Email from Router $_router_name
MIME-Version: 1.0
Content-type: multipart/mixed; boundary=\"EEM_email_boundary\"
\n--EEM_email_boundary\n
\n--EEM_email_boundary
Content-Type: application/octet-stream
Content-Transfer-Encoding: Base64
Content-Disposition: attachment; filename=\"test.log\"\n\n"

# Then, read in the data you wish to attach.

if [catch {open "flash:test.log"} result] {
    error $result $errorInfo
}
set fd $result

# Encode that data using the built-in base64 library.
set email_b64 [::base64::encode [read $fd]]
close $fd

# Construct the final message with the headers, body, and MIME parts.

set email_body_mime "\n--EEM_email_boundary--"

set email_body [format "%s%s%s" $email_body_pre $email_b64 $email_body_mime]

# Send the email.

if [catch {smtp_send_email $email_body} result] {
    error $result $errorInfo
}

Average Rating: 4 (1 ratings)

Actions

Login or Register to take actions

This Document

Posted March 31, 2010 at 3:18 PM
Stats:
Comments:0 Avg. Rating:4
Views:1988 Contributors:0
Shares:0
Tags: tac, email, eem, mime, howto
+

Documents Leaderboard