Send email from EEM applet

From CT3

Jump to: navigation, search

By Ivan Pepelnjak

The action mail introduced in IOS release 12.3(14)T, allows you to send an e-mail straight from the EEM applet. The command has a number of parameters; you have to specify the SMTP gateway, the sender and the recipient. Optionally, you can also specify the e-mail subject and the body. You can hard-code all the information in the EEM applet (and you should if your network has one or two routers), but if you want to deploy your code in larger networks (or publish it), you should parametrize the applet as much as possible.

This topic and numerous other EEM topics are covered in the EEM Workshop

Let's start with the SMTP gateway. Never use the IP address of the SMTP gateway in the EEM applet. If you have configured the routers to use DNS servers, specify the host name of the SMTP gateway, otherwise use the ip host name address configuration command to define the IP address of the smtp gateway. Or, if you want to make your code really portable, define the hostname of the SMTP gateway as an EEM environment variable:

ip host
event manager environment _mail_smtp

Likewise, you should define your domain and the recipient of the e-mail message as EEM variables:

event manager environment _mail_domain
event manager environment _mail_rcpt

In most circumstances, the sender address should be the router's name. You can use the action info type routername EEM applet command which sets the $_info_routername variable and use that variable in the action mail command:

event manager applet SampleApplet
 event ...
 action 1.0 info type routername
 action 2.0 mail server "$_mail_smtp" to "$_mail_rcpt" from "$_info_routername@$_mail_domain" ...

The mail subject can also include the router's name, allowing the operators to quickly identify the reporting device. The mail body (if you decide to have it) could include event-related information (using event-specific variables) or results of a previous action cli command available in the $_cli_result variable. For example, you could decide to send the list of OSPF neighbors in the message body whenever the router reports a change in the OSPF neighbor state with a syslog message:

event manager applet ReportOSPFNeighbors
 event syslog pattern "OSPF-5-ADJCHG"
 action 1.0 info type routername
 action 1.1 cli command "show ip ospf neighbor"
 action 2.0 mail server "$_mail_smtp" to "$_mail_rcpt" from "$_info_routername@$_mail_domain" →
   subject "OSPF neighbor change @ $_info_routername" body "$_cli_result"
Personal tools


Main menu