#!/usr/bin/php -c /usr/lib/php.ini

<?php
        $notification_type = getenv('NOTIFICATIONTYPE');
        $service_desc = getenv('SERVICEDESC');
        $host_alias = getenv('HOSTALIAS');
        $host_address = getenv('HOSTADDRESS');
        $service_state = getenv('SERVICESTATE');
        $long_datetime = getenv('LONGDATETIME');
        $service_output = getenv('SERVICEOUTPUT');
        $notification_authorname = getenv('NOTIFICATIONAUTHORNAME');
        $notification_comment = getenv('NOTIFICATIONCOMMENT');
        $host_displayname = getenv('HOSTDISPLAYNAME');
        $service_displayname = getenv('SERVICEDISPLAYNAME');
        $user_mail = getenv('USEREMAIL');
        require_once "Mail.php"; // You need php Mail::Factory module installed which contains this file
        $from = "TEST@EXAMPLE.COM"; // <update with your sender email address>
        $to = "$user_mail";
        $subject = "** $notification_type - $host_displayname - $service_desc is $service_output **";
        $body = "<html><head>";
        $body .= "<style>
table {
    font-family: arial, sans-serif;
    border-collapse: collapse;
    width: 100%;
}

td, th {
    border: 1px solid #dddddd;
    text-align: left;
    padding: 8px;
}

tr:nth-child(even) {
    background-color: #dddddd;
}
</style>";
        $f_color="#dddddd";
        if($service_state=="WARNING") {$f_color="#f48400";}
        if($service_state=="CRITICAL") {$f_color="#f40000";}
        if($service_state=="OK") {$f_color="#00b71a";}
        if($service_state=="UNKNOWN") {$f_color="#cc00de";}
        $body .= "<body><section>";
        $body .= "<!--for demo wrap-->";
        $body .= "<h3>Icinga2 Notification for Hosts & Services</h3>";
        $body .= "<table>";
        $body .= "<tbody>";
        $body .= "<tr bgcolor=$f_color><td><b><font color=#ffffff>Notification Type</font></b></td><td><font ";
        $body .= "color=#ffffff><b>$notification_type [$service_state]</b></font></td></tr>\r\n";
        $body .= "<tr>";
        $body .= "<td>Service</td>";
        $body .= "<td>$service_desc</td>";
        $body .= "</tr>";
        $body .= "<tr>";
        $body .= "<td>Host</td>";
        $body .= "<td>$host_alias</td>";
        $body .= "</tr>";
        $body .= "<tr>";
        $body .= "<td>Address</td>";
        $body .= "<td>$host_address</td>";
        $body .= "</tr>";
        $body .= "<tr>";
        $body .= "<td>State</td>";
        $body .= "<td>$service_state</td>";
        $body .= "</tr>";
        $body .= "<tr>";
        $body .= "<td>Date/Time</td>";
        $body .= "<td>$long_datetime</td>";
        $body .= "</tr>";
        $body .= "<tr>";
        $body .= "<td>Additional Info</td>";
        $body .= "<td>$service_output</td>";
        $body .= "</tr>";
        $body .= "<tr>";
        $body .= "<td>Comment</td>";
        $body .= "<td>[$notification_authorname] $notification_comment</td>";
        $body .= "</tr>";
        $body .= "</tbody>";
        $body .= "</table>";
        $body .= "</section>";
        $body .= "</body></html>";
        $host = "***.***.***.***";  //UPDATE WITH YOUR SMTP SERVER ADDRESS
        $headers = array("From" => $from , "To" => $to ,"Subject" => $subject,   "Content-Type" => "text/html; charset=ISO-8859-1; MIME-Version: 1.0");
        $smtp = Mail::factory('smtp', array ('host' => $host, 'auth' => false));

        $mail = $smtp->send($to, $headers, $body);
        if (PEAR::isError($mail)) {
                echo("<p>" . $mail->getMessage() . "</p>");
        } else {
                echo("<p>Message successfully sent!</p>");
      