Forum Discussion

Andrew_Husking's avatar
Oct 23, 2012

iRule to send emails

 

The iRule below will send an email to an APM user's email address (pulled from AD), whenever an APM iRule event is triggered.

 

 

The email will be sent in HTML not plain text.

 

 

the rule should be fairly self explainitory, the only thing you need is to run an AD query object in the VPE before the iRule gets called so that the user's attributes are all there.

 

 

 

 

Code:

 

 

when ACCESS_POLICY_AGENT_EVENT {

 

if { [ACCESS::policy agent_id] eq "EmailUser" } {

 

set mailfrom "from@blah.com"

 

set mailserv "1.1.1.1:25"

 

 

set response "Hi [ACCESS::session data get session.ad.last.attr.givenName],

 

Your device user agent isn't allowed

 

UserAgent: [ACCESS::session data get session.user.agent] "

 

set email [ACCESS::session data get session.ad.last.attr.mail]

 

 

set conn [connect -timeout 3000 -idle 30 -status conn_status $mailserv ]

 

 

set data "HELO\r\nMAIL FROM: $mailfrom\r\nRCPT TO: $email\r\nDATA\r\nSUBJECT: Invalid Email Client\r\nMIME-Version: 1.0;\r\nContent-Type: text/html;charset=iso-8859-1;\r\n\r\n\r\n$response\r\n\r\n.\r\n"

 

set send_info [send -timeout 3000 -status send_status $conn $data]

 

set recv_data [recv -timeout 3000 -status recv_status 393 $conn]

 

log local0. $recv_data

 

close $conn

 

}

 

}

 

 

3 Replies

  • Hi Andrew,

     

     

    That's novel. Thanks for posting. This would make a great codeshare example:

     

     

    https://devcentral.f5.com/wiki/iRules.Edit.aspx?Template=iRule%20Submission

     

     

    Aaron
    • Abed_AL-R's avatar
      Abed_AL-R
      Icon for Cirrostratus rankCirrostratus

      Hi

      Could you please update the link? :)

      Because it seems to be working