Send Email via iRule ProceduresΒΆ

This iRule sends an email using procedures and a sideband connection to your organizational email server. You can call the procedure from virtually any iRule event.
####
# this Irule sends an email to the F5 admin via the mailserver.myorg.org mailserver. This iRule calls the procedure in the procLibrary iRule.
####
when HTTP_RESPONSE {
    set error 0
    if {$error} {
        set errorSubject "F5 Email Notification."
        set errorBody "There was an event that happened. Check your F5 local log!"
        call thisiRule::sendEmail $errorSubject $errorBody
    }
    else {
        #Do something cool
    }
}
proc sendEmail {sourceSubject sourceBody} {
        set currentTime [clock format [clock seconds] -format {%H:%M:%S}]
        set todaysDate [clock format [clock seconds] -format {%m-%d:%Y}]
        set subject ${sourceSubject}
        set body "${sourceBody}\r\n\r\n ${currentTime}hrs on ${todaysDate}.\r\n\r\n This is an automated email sent via the this iRule iRule.\r\n\r\nSincerely,\r\nYour F5 Support Team"
        log local0. $body
        catch {call procLibrary::sendEmail $subject $body "email1@myorg.org email2@myorg.org"}
}

###
# This procedure library procLibrary sends an email using a sideband connection to your organizations email server.
# It has 3 input parameters; subject, body and additional email addresses. The calling procedure needs to seperate the additional
#   emails with a " ". Example "email1@myorg.org email2@myorg.org".
###
proc sendEmail {sourceSubject sourceEmailBody sourceAddEmailAddress} {
    set email_debug 0
    set hostName [info hostname]
    set mailFrom "root@$hostname"
    set mailTo "admin@myorg.org"
    set mailServer "xxx.xxx.xxx.xxx:25"
    set subject $sourceSubject
    set emailBody $sourceEmailBody
    set addEmailAddress ""
    if {$sourceAddEmailAddress ne ""} {
        append sourceAddEmailAddress "${mailTo}"
        foreach emailAddress $sourceAddEmailAddress {
            set emailConn [connect -timeout 2000 -idle 10 -status conn_status $mailServer]
            if {$email_debug} {log local0. "connect_status: $conn_status"}
            set emailData "HELO mailserver.myorg.org\r\nMAIL FROM: $mailFrom\r\nRCPT To: ${emailAddress}\r\nDATA\r\nSUBJECT: \r\n\r\n\r\n$emailBody\r\n.\r\n"
            set send_info [send -timeout 2000 -status send_status $emailConn $emailData]
            if {$email_debug} {log local0. "emailData: $emailData"}
            if {$email_debug} {log local0. "send_status: $send_status"}
            set recv_data [recv -timeout 2000 -status recv_status $emailConn]
            if {$email_debug} {log local0. "recv_Data: $recv_data"}
            close $emailConn
            if {$email_debug} {log local0. "EmailTo: $emailAddress"}
        }
    }
    else {
        if {$email_debug} {log local0. "connect_status: $conn_status"}
        set emailData "HELO mailserver.myorg.org\r\nMAIL FROM: $mailFrom\r\nRCPT To: ${emailAddress}\r\nDATA\r\nSUBJECT: \r\n\r\n\r\n$emailBody\r\n.\r\n"
        set send_info [send -timeout 2000 -status send_status $emailConn $emailData]
        if {$email_debug} {log local0. "emailData: $emailData"}
        if {$email_debug} {log local0. "send_status: $send_status"}
        set recv_data [recv -timeout 2000 -status recv_status $emailConn]
        if {$email_debug} {log local0. "recv_Data: $recv_data"}
        close $emailConn
    }
}

The BIG-IP API Reference documentation contains community-contributed content. F5 does not monitor or control community code contributions. We make no guarantees or warranties regarding the available code, and it may contain errors, defects, bugs, inaccuracies, or security vulnerabilities. Your access to and use of any code available in the BIG-IP API reference guides is solely at your own risk.