/**
 * Send an iMIP message since they look like a non-local user.
 *  
 * @param string $method The METHOD parameter from the iTIP
 * @param string $to_email The e-mail address we're going to send to
 * @param vCalendar $vcal The iTIP part of the message.
 */
function doImipMessage($method, $to_email, vCalendar $itip)
{
    global $c, $request;
    header('Debug: Sending iMIP ' . $method . ' message to ' . $to_email);
    $mime = new MultiPart();
    $mime->addPart($itip->Render(), 'text/calendar; charset=UTF-8; method=' . $method);
    $friendly_part = isset($c->iMIP->template[$method]) ? $c->iMIP->template[$method] : <<<EOTEMPLATE
This is a meeting ##METHOD## which your e-mail program should be able to
import into your calendar.  Alternatively you could save the attachment
and load that into your calendar instead.
EOTEMPLATE;
    $components = $itip->GetComponents('VTIMEZONE', false);
    $replaceable = array('METHOD', 'DTSTART', 'DTEND', 'SUMMARY', 'DESCRIPTION', 'URL');
    foreach ($replaceable as $pname) {
        $search = '##' . $pname . '##';
        if (strstr($friendly_part, $search) !== false) {
            $property = $itip->GetProperty($pname);
            if (empty($property)) {
                $property = $components[0]->GetProperty($pname);
            }
            if (empty($property)) {
                $replace = '';
            } else {
                switch ($pname) {
                    case 'DTSTART':
                    case 'DTEND':
                        $when = new RepeatRuleDateTime($property);
                        $replace = $when->format('c');
                        break;
                    default:
                        $replace = $property->GetValue();
                }
            }
            $friendly_part = str_replace($search, $replace, $friendly_part);
        }
    }
    $mime->addPart($friendly_part, 'text/plain');
    $email = new EMail();
    $email->SetFrom($request->principal->email());
    $email->AddTo($to_email);
    $email->SetSubject($components[0]->GetPValue('SUMMARY'));
    $email->SetBody($mime->getMimeParts());
    if (isset($c->iMIP->pretend_email)) {
        $email->Pretend($mime->getMimeHeaders());
    } else {
        if (!isset($c->iMIP->send_email) || !$c->iMIP->send_email) {
            $email->PretendLog($mime->getMimeHeaders());
        } else {
            $email->Send($mime->getMimeHeaders());
        }
    }
}
 protected function _DoExecute($oTrigger, $aContextArgs, &$oLog)
 {
     $sPreviousUrlMaker = ApplicationContext::SetUrlMakerClass();
     try {
         $this->m_iRecipients = 0;
         $this->m_aMailErrors = array();
         $bRes = false;
         // until we do succeed in sending the email
         // Determine recicipients
         //
         $sTo = $this->FindRecipients('to', $aContextArgs);
         $sCC = $this->FindRecipients('cc', $aContextArgs);
         $sBCC = $this->FindRecipients('bcc', $aContextArgs);
         $sFrom = MetaModel::ApplyParams($this->Get('from'), $aContextArgs);
         $sReplyTo = MetaModel::ApplyParams($this->Get('reply_to'), $aContextArgs);
         $sSubject = MetaModel::ApplyParams($this->Get('subject'), $aContextArgs);
         $sBody = MetaModel::ApplyParams($this->Get('body'), $aContextArgs);
         $oObj = $aContextArgs['this->object()'];
         $sMessageId = sprintf('iTop_%s_%d_%f@%s.openitop.org', get_class($oObj), $oObj->GetKey(), microtime(true), MetaModel::GetEnvironmentId());
         $sReference = '<' . $sMessageId . '>';
     } catch (Exception $e) {
         ApplicationContext::SetUrlMakerClass($sPreviousUrlMaker);
         throw $e;
     }
     ApplicationContext::SetUrlMakerClass($sPreviousUrlMaker);
     if (!is_null($oLog)) {
         // Note: we have to secure this because those values are calculated
         // inside the try statement, and we would like to keep track of as
         // many data as we could while some variables may still be undefined
         if (isset($sTo)) {
             $oLog->Set('to', $sTo);
         }
         if (isset($sCC)) {
             $oLog->Set('cc', $sCC);
         }
         if (isset($sBCC)) {
             $oLog->Set('bcc', $sBCC);
         }
         if (isset($sFrom)) {
             $oLog->Set('from', $sFrom);
         }
         if (isset($sSubject)) {
             $oLog->Set('subject', $sSubject);
         }
         if (isset($sBody)) {
             $oLog->Set('body', $sBody);
         }
     }
     $oEmail = new EMail();
     if ($this->IsBeingTested()) {
         $oEmail->SetSubject('TEST[' . $sSubject . ']');
         $sTestBody = $sBody;
         $sTestBody .= "<div style=\"border: dashed;\">\n";
         $sTestBody .= "<h1>Testing email notification " . $this->GetHyperlink() . "</h1>\n";
         $sTestBody .= "<p>The email should be sent with the following properties\n";
         $sTestBody .= "<ul>\n";
         $sTestBody .= "<li>TO: {$sTo}</li>\n";
         $sTestBody .= "<li>CC: {$sCC}</li>\n";
         $sTestBody .= "<li>BCC: {$sBCC}</li>\n";
         $sTestBody .= "<li>From: {$sFrom}</li>\n";
         $sTestBody .= "<li>Reply-To: {$sReplyTo}</li>\n";
         $sTestBody .= "<li>References: {$sReference}</li>\n";
         $sTestBody .= "</ul>\n";
         $sTestBody .= "</p>\n";
         $sTestBody .= "</div>\n";
         $oEmail->SetBody($sTestBody);
         $oEmail->SetRecipientTO($this->Get('test_recipient'));
         $oEmail->SetRecipientFrom($this->Get('test_recipient'));
         $oEmail->SetReferences($sReference);
         $oEmail->SetMessageId($sMessageId);
     } else {
         $oEmail->SetSubject($sSubject);
         $oEmail->SetBody($sBody);
         $oEmail->SetRecipientTO($sTo);
         $oEmail->SetRecipientCC($sCC);
         $oEmail->SetRecipientBCC($sBCC);
         $oEmail->SetRecipientFrom($sFrom);
         $oEmail->SetRecipientReplyTo($sReplyTo);
         $oEmail->SetReferences($sReference);
         $oEmail->SetMessageId($sMessageId);
     }
     if (isset($aContextArgs['attachments'])) {
         $aAttachmentReport = array();
         foreach ($aContextArgs['attachments'] as $oDocument) {
             $oEmail->AddAttachment($oDocument->GetData(), $oDocument->GetFileName(), $oDocument->GetMimeType());
             $aAttachmentReport[] = array($oDocument->GetFileName(), $oDocument->GetMimeType(), strlen($oDocument->GetData()));
         }
         $oLog->Set('attachments', $aAttachmentReport);
     }
     if (empty($this->m_aMailErrors)) {
         if ($this->m_iRecipients == 0) {
             return 'No recipient';
         } else {
             $iRes = $oEmail->Send($aErrors, false, $oLog);
             // allow asynchronous mode
             switch ($iRes) {
                 case EMAIL_SEND_OK:
                     return "Sent";
                 case EMAIL_SEND_PENDING:
                     return "Pending";
                 case EMAIL_SEND_ERROR:
                     return "Errors: " . implode(', ', $aErrors);
             }
         }
     } else {
         if (is_array($this->m_aMailErrors) && count($this->m_aMailErrors) > 0) {
             $sError = implode(', ', $this->m_aMailErrors);
         } else {
             $sError = 'Unknown reason';
         }
         return 'Notification was not sent: ' . $sError;
     }
 }
Example #3
0
    /**
    * E-mails a temporary password in response to a request from a user.
    *
    * This could be called from somewhere within the application that allows
    * someone to set up a user and invite them.
    *
    * This function includes EMail.php to actually send the password.
    */
    function EmailTemporaryPassword($username, $email_address, $body_template = "")
    {
        global $c;
        $password_sent = false;
        $where = "";
        $params = array();
        if (isset($username) && $username != "") {
            $where = 'WHERE active AND lower(usr.username) = :lcusername';
            $params[':lcusername'] = strtolower($username);
        } else {
            if (isset($email_address) && $email_address != "") {
                $where = 'WHERE active AND lower(usr.email) = :lcemail';
                $params[':lcemail'] = strtolower($email_address);
            }
        }
        if ($where != '') {
            if (!isset($body_template) || $body_template == "") {
                $body_template = <<<EOTEXT

@@debugging@@A temporary password has been requested for @@system_name@@.

Temporary Password: @@password@@

This has been applied to the following usernames:

@@usernames@@
and will be valid for 24 hours.

If you have any problems, please contact the system administrator.

EOTEXT;
            }
            $qry = new AwlQuery('SELECT * FROM usr ' . $where, $params);
            $qry->Exec('Session::EmailTemporaryPassword');
            if ($qry->rows() > 0) {
                $q2 = new AwlQuery();
                $q2->Begin();
                while ($row = $qry->Fetch()) {
                    $mail = new EMail("Access to {$c->system_name}");
                    $mail->SetFrom($c->admin_email);
                    $usernames = "";
                    $debug_to = "";
                    if (isset($c->debug_email)) {
                        $debug_to = "This e-mail would normally be sent to:\n ";
                        $mail->AddTo("Tester <{$c->debug_email}>");
                    }
                    $tmp_passwd = '';
                    for ($i = 0; $i < 8; $i++) {
                        $tmp_passwd .= substr('ABCDEFGHIJKLMNOPQRSTUVWXYZ+#.-=*%@0123456789abcdefghijklmnopqrstuvwxyz', rand(0, 69), 1);
                    }
                    $q2->QDo('INSERT INTO tmp_password (user_no, password) VALUES(?,?)', array($row->user_no, $tmp_passwd));
                    if (isset($c->debug_email)) {
                        $debug_to .= "{$row->fullname} <{$row->email}> ";
                    } else {
                        $mail->AddTo("{$row->fullname} <{$row->email}>");
                    }
                    $usernames .= "        {$row->username}\n";
                    if ($mail->To != "") {
                        if (isset($c->debug_email)) {
                            $debug_to .= "\n============================================================\n";
                        }
                        $sql .= "COMMIT;";
                        $qry = new AwlQuery($sql);
                        $qry->Exec("Session::SendTemporaryPassword");
                        $body = str_replace('@@system_name@@', $c->system_name, $body_template);
                        $body = str_replace('@@password@@', $tmp_passwd, $body);
                        $body = str_replace('@@usernames@@', $usernames, $body);
                        $body = str_replace('@@debugging@@', $debug_to, $body);
                        $mail->SetBody($body);
                        $mail->Send();
                        $password_sent = true;
                    }
                }
            }
        }
        return $password_sent;
    }