Example #1
0
function email_notify($check,$check_result,$subscription) {
 global $status_array;
 $user = new User($subscription->getUserId());
 echo 'email plugin!';
 $email = new fEmail();
 // This sets up fSMTP to connect to the gmail SMTP server
 // with a 5 second timeout. Gmail requires a secure connection.
 $smtp = new fSMTP('smtp.gmail.com', 465, TRUE, 5);
 $smtp->authenticate('*****@*****.**', 'example');
 $email->addRecipient($user->getEmail(), $user->getUsername());
 // Set who the email is from
 $email->setFromEmail('*****@*****.**','Tattle');
 // Set the subject include UTF-8 curly quotes
 $email->setSubject('Tattle : Alert for ' . $check->prepareName());
 // Set the body to include a string containing UTF-8
 $state = $status_array[$check_result->getStatus()];
 $email->setHTMLBody("<p>$state Alert for {$check->prepareName()} </p><p>The check returned {$check_result->prepareValue()}</p><p>Warning Threshold is : ". $check->getWarn() . "</p><p>Error Threshold is : ". $check->getError() . '</p><p>View Alert Details : <a href="' . $fURL::getDomain() . '/' . CheckResult::makeURL('list',$check_result) . '">'.$check->prepareName()."</a></p>");
 $email->setBody("
$state Alert for {$check->prepareName()}
The check returned {$check_result->prepareValue()}
Warning Threshold is : ". $check->getWarn() . "
Error Threshold is : ". $check->getError() . "
           ");
 try {  
   $message_id = $email->send($smtp);
 } catch ( fConnectivityException $e) { 
   fCore::debug("email send failed",FALSE);
 }


}
Example #2
0
function email_plugin_notify($check,$check_result,$subscription,$alt_email=false) {
  global $status_array;
  $user = new User($subscription->getUserId());
  $email = new fEmail();
  // This sets up fSMTP to connect to the gmail SMTP server
  // with a 5 second timeout. Gmail requires a secure connection.
  $smtp = new fSMTP(sys_var('smtp_server'), sys_var('smtp_port'), TRUE, 5);
  $smtp->authenticate(sys_var('smtp_user'), sys_var('smtp_pass'));
  if ($alt_email) {
    $email_address = usr_var('alt_email',$user->getUserId());
  } else {
    $email_address = $user->getEmail(); 
  }
  $email->addRecipient($email_address, $user->getUsername());
  // Set who the email is from
  $email->setFromEmail(sys_var('email_from'), sys_var('email_from_display'));
  // Set the subject include UTF-8 curly quotes
  $email->setSubject(str_replace('{check_name}', $check->prepareName(), sys_var('email_subject')));
  // Set the body to include a string containing UTF-8
  $state = $status_array[$check_result->getStatus()];
  $email->setHTMLBody("<p>$state Alert for {$check->prepareName()} </p><p>The check returned {$check_result->prepareValue()}</p><p>Warning Threshold is : ". $check->getWarn() . "</p><p>Error Threshold is : ". $check->getError() . '</p><p>View Alert Details : <a href="' . fURL::getDomain() . '/' . CheckResult::makeURL('list',$check_result) . '">'.$check->prepareName()."</a></p>");
  $email->setBody("
  $state Alert for {$check->prepareName()}
The check returned {$check_result->prepareValue()}
Warning Threshold is : ". $check->getWarn() . "
Error Threshold is : ". $check->getError() . "
           ");
  try {  
    $message_id = $email->send($smtp);
  } catch ( fConnectivityException $e) { 
    fCore::debug("email send failed",FALSE);
  }
}
 /**
  * Sends the email
  * 
  * The return value is the message id, which should be included as the
  * `Message-ID` header of the email. While almost all SMTP servers will not
  * modify this value, testing has indicated at least one (smtp.live.com
  * for Windows Live Mail) does.
  * 
  * @throws fValidationException  When ::validate() throws an exception
  * 
  * @param  fSMTP $connection  The SMTP connection to send the message over
  * @return string  The message id for the message - see method description for details
  */
 public function send($connection = NULL)
 {
     $this->validate();
     // The mail() function on Windows doesn't support names in headers so
     // we must strip them down to just the email address
     if ($connection === NULL && fCore::checkOS('windows')) {
         $vars = array('bcc_emails', 'bounce_to_email', 'cc_emails', 'from_email', 'reply_to_email', 'sender_email', 'to_emails');
         foreach ($vars as $var) {
             if (!is_array($this->{$var})) {
                 if (preg_match(self::NAME_EMAIL_REGEX, $this->{$var}, $match)) {
                     $this->{$var} = $match[2];
                 }
             } else {
                 $new_emails = array();
                 foreach ($this->{$var} as $email) {
                     if (preg_match(self::NAME_EMAIL_REGEX, $email, $match)) {
                         $email = $match[2];
                     }
                     $new_emails[] = $email;
                 }
                 $this->{$var} = $new_emails;
             }
         }
     }
     $to = trim($this->buildMultiAddressHeader("", $this->to_emails));
     $top_level_boundary = $this->createBoundary();
     $headers = $this->createHeaders($top_level_boundary, $this->message_id);
     $subject = str_replace(array("\r", "\n"), '', $this->subject);
     $subject = $this->makeEncodedWord($subject);
     $body = $this->createBody($top_level_boundary);
     if ($this->smime_encrypt || $this->smime_sign) {
         list($headers, $body) = $this->createSMIMEBody($to, $subject, $headers, $body);
     }
     // Remove extra line breaks
     $headers = trim($headers);
     $body = trim($body);
     if ($connection) {
         $to_emails = $this->extractEmails($this->to_emails);
         $to_emails = array_merge($to_emails, $this->extractEmails($this->cc_emails));
         $to_emails = array_merge($to_emails, $this->extractEmails($this->bcc_emails));
         $from = $this->bounce_to_email ? $this->bounce_to_email : current($this->extractEmails(array($this->from_email)));
         $connection->send($from, $to_emails, "To: " . $to . "\r\nSubject: " . $subject . "\r\n" . $headers, $body);
         return $this->message_id;
     }
     // Sendmail when not in safe mode will allow you to set the envelope from address via the -f parameter
     $parameters = NULL;
     if (!fCore::checkOS('windows') && $this->bounce_to_email) {
         preg_match(self::EMAIL_REGEX, $this->bounce_to_email, $matches);
         $parameters = '-f ' . $matches[0];
         // Windows takes the Return-Path email from the sendmail_from ini setting
     } elseif (fCore::checkOS('windows') && $this->bounce_to_email) {
         $old_sendmail_from = ini_get('sendmail_from');
         preg_match(self::EMAIL_REGEX, $this->bounce_to_email, $matches);
         ini_set('sendmail_from', $matches[0]);
     }
     // This is a gross qmail fix that is a last resort
     if (self::$popen_sendmail || self::$convert_crlf) {
         $to = str_replace("\r\n", "\n", $to);
         $subject = str_replace("\r\n", "\n", $subject);
         $body = str_replace("\r\n", "\n", $body);
         $headers = str_replace("\r\n", "\n", $headers);
     }
     // If the user is using qmail and wants to try to fix the \r\r\n line break issue
     if (self::$popen_sendmail) {
         $sendmail_command = ini_get('sendmail_path');
         if ($parameters) {
             $sendmail_command .= ' ' . $parameters;
         }
         $sendmail_process = popen($sendmail_command, 'w');
         fprintf($sendmail_process, "To: %s\n", $to);
         fprintf($sendmail_process, "Subject: %s\n", $subject);
         if ($headers) {
             fprintf($sendmail_process, "%s\n", $headers);
         }
         fprintf($sendmail_process, "\n%s\n", $body);
         $error = pclose($sendmail_process);
         // This is the normal way to send mail
     } else {
         // On Windows, mail() sends directly to an SMTP server and will
         // strip a leading . from the body
         if (fCore::checkOS('windows')) {
             $body = preg_replace('#^\\.#', '..', $body);
         }
         if ($parameters) {
             $error = !mail($to, $subject, $body, $headers, $parameters);
         } else {
             $error = !mail($to, $subject, $body, $headers);
         }
     }
     if (fCore::checkOS('windows') && $this->bounce_to_email) {
         ini_set('sendmail_from', $old_sendmail_from);
     }
     if ($error) {
         throw new fConnectivityException('An error occured while trying to send the email entitled %s', $this->subject);
     }
     return $this->message_id;
 }
 /**
  * @dataProvider serverProvider
  */
 public function testSendMultipleToCcBcc($server, $port, $secure, $username, $password)
 {
     $token = $this->generateSubjectToken();
     $smtp = new fSMTP($server, $port, $secure, 10);
     if ($username) {
         $smtp->authenticate($username, $password);
     }
     $email = new fEmail();
     $email->setFromEmail($username ? $username : '******');
     $email->addRecipient(EMAIL_ADDRESS, 'Test User');
     $email->addRecipient(str_replace('@', '_2@', EMAIL_ADDRESS), 'Test User 2');
     $email->addCCRecipient(str_replace('@', '_3@', EMAIL_ADDRESS), 'Test User 3');
     $email->addBCCRecipient(str_replace('@', '_4@', EMAIL_ADDRESS), 'Test User 4');
     $email->setSubject($token . ': Testing Multiple Recipients');
     $email->setBody('This is a test of sending multiple recipients');
     $message_id = $email->send($smtp);
     $message = $this->findMessage($token, EMAIL_USER);
     $this->assertEquals($message_id, $message['headers']['message-id']);
     $this->assertEquals($username ? $username : '******', $message['headers']['from']['mailbox'] . '@' . $message['headers']['from']['host']);
     $this->assertEquals($token . ': Testing Multiple Recipients', $message['headers']['subject']);
     $this->assertEquals('This is a test of sending multiple recipients', trim($message['text']));
     $message = $this->findMessage($token, str_replace('tests', 'tests_2', EMAIL_USER));
     $this->assertEquals(array('personal' => 'Test User', 'mailbox' => 'tests', 'host' => 'flourishlib.com'), $message['headers']['to'][0]);
     $message = $this->findMessage($token, str_replace('tests', 'tests_3', EMAIL_USER));
     $this->assertEquals(array('personal' => 'Test User 3', 'mailbox' => 'tests_3', 'host' => 'flourishlib.com'), $message['headers']['cc'][0]);
     $message = $this->findMessage($token, str_replace('tests', 'tests_4', EMAIL_USER));
     $this->assertEquals(FALSE, isset($message['headers']['bcc']));
     $smtp->close();
 }
Example #5
0
function notify_multiple_users($user_from, $recipients, $subject, $body)
{
    $email = new fEmail();
    // This sets up fSMTP to connect to the gmail SMTP server
    // with a 5 second timeout. Gmail requires a secure connection.
    $smtp = new fSMTP(sys_var('smtp_server'), sys_var('smtp_port'), sys_var('require_ssl') === 'true' ? TRUE : FALSE, 5);
    if (sys_var('require_auth') === 'true') {
        $smtp->authenticate(sys_var('smtp_user'), sys_var('smtp_pass'));
    }
    // Add the recipients
    foreach ($recipients as $rec) {
        $email->addRecipient($rec['mail'], $rec['name']);
    }
    // Set who the email is from
    $email->setFromEmail($user_from->getEmail(), $user_from->getUsername());
    // Set the subject
    $email->setSubject($subject);
    // Set the body
    $email->setHTMLBody($body);
    $email->setBody($body);
    try {
        $message_id = $email->send($smtp);
    } catch (fConnectivityException $e) {
        fCore::debug($e, FALSE);
        fCore::debug("email send failed", FALSE);
        $e->printMessage();
        $e->printTrace();
    }
}
Example #6
0
 /**
  * @dataProvider serverProvider
  */
 public function testSendMultipleToCcBcc($server, $port, $secure, $username, $password)
 {
     $token = $this->generateSubjectToken();
     $smtp = new fSMTP($server, $port, $secure, 5);
     if ($username) {
         $smtp->authenticate($username, $password);
     }
     $email = new fEmail();
     $email->setFromEmail($username ? $username : '******');
     $email->addRecipient(EMAIL_ADDRESS, 'Test User');
     $email->addRecipient(str_replace('@', '_2@', EMAIL_ADDRESS), 'Test User 2');
     $email->addCCRecipient(str_replace('@', '_3@', EMAIL_ADDRESS), 'Test User 3');
     $email->addBCCRecipient(str_replace('@', '_4@', EMAIL_ADDRESS), 'Test User 4');
     $email->setSubject($token . ': Testing Multiple Recipients');
     $email->setBody('This is a test of sending multiple recipients');
     $message_id = $email->send($smtp);
     $message = $this->findMessage($token, EMAIL_USER);
     $this->assertEquals($message_id, $message['headers']['Message-ID']);
     $this->assertEquals($username ? $username : '******', $message['headers']['From']);
     $this->assertEquals($token . ': Testing Multiple Recipients', $message['headers']['Subject']);
     $this->assertEquals('This is a test of sending multiple recipients', trim($message['plain']));
     $message = $this->findMessage($token, str_replace('tests', 'tests_2', EMAIL_USER));
     // It seems the windows imap extension doesn't support the personal part of an email address
     $is_windows = stripos(php_uname('a'), 'windows') !== FALSE;
     $this->assertEquals($is_windows ? '*****@*****.**' : '"Test User" <*****@*****.**>', $message['headers']['To']);
     $message = $this->findMessage($token, str_replace('tests', 'tests_3', EMAIL_USER));
     $this->assertEquals($is_windows ? '*****@*****.**' : '"Test User 3" <*****@*****.**>', $message['headers']['Cc']);
     $message = $this->findMessage($token, str_replace('tests', 'tests_4', EMAIL_USER));
     $this->assertEquals(FALSE, isset($message['headers']['Bcc']));
     $smtp->close();
 }