Esempio n. 1
0
 function updateOrCreateEmail($part = '', $opts, $cm = false)
 {
     // DB_DataObject::debugLevel(1);
     $template_name = preg_replace('/\\.[a-z]+$/i', '', basename($opts['file']));
     if (!file_exists($opts['file'])) {
         $this->jerr("file does not exist : " . $opts['file']);
     }
     if (!empty($opts['master']) && !file_exists($opts['master'])) {
         $this->jerr("master file does not exist : " . $opts['master']);
     }
     if (empty($cm)) {
         $cm = DB_dataObject::factory('core_email');
         $ret = $cm->get('name', $template_name);
         if ($ret && empty($opts['update'])) {
             $this->jerr("use --update 1 to update the template..");
         }
     }
     $mailtext = file_get_contents($opts['file']);
     if (!empty($opts['master'])) {
         $body = $mailtext;
         $mailtext = file_get_contents($opts['master']);
         $mailtext = str_replace('{outputBody():h}', $body, $mailtext);
     }
     require_once 'Mail/mimeDecode.php';
     require_once 'Mail/RFC822.php';
     $decoder = new Mail_mimeDecode($mailtext);
     $parts = $decoder->getSendArray();
     if (is_a($parts, 'PEAR_Error')) {
         echo $parts->toString() . "\n";
         exit;
     }
     $headers = $parts[1];
     $from = new Mail_RFC822();
     $from_str = $from->parseAddressList($headers['From']);
     $from_name = trim($from_str[0]->personal, '"');
     $from_email = $from_str[0]->mailbox . '@' . $from_str[0]->host;
     if ($cm->id) {
         $cc = clone $cm;
         $cm->setFrom(array('bodytext' => $parts[2], 'updated_dt' => date('Y-m-d H:i:s')));
         $cm->update($cc);
     } else {
         $cm->setFrom(array('from_name' => $from_name, 'from_email' => $from_email, 'subject' => $headers['Subject'], 'name' => $template_name, 'bodytext' => $parts[2], 'updated_dt' => date('Y-m-d H:i:s'), 'created_dt' => date('Y-m-d H:i:s')));
         $cm->insert();
     }
     return $cm;
 }
Esempio n. 2
0
 /**
  * Sends an e-mail
  * This messages needs to be saved into the 'sent items' folder
  *
  * @param SyncSendMail  $sm     SyncSendMail object
  *
  * @access public
  * @return boolean
  * @throws StatusException
  */
 public function SendMail($sm)
 {
     ZLog::Write(LOGLEVEL_DEBUG, sprintf("BackendIMAP->SendMail(): RFC822: %d bytes  forward-id: '%s' reply-id: '%s' parent-id: '%s' SaveInSent: '%s' ReplaceMIME: '%s'", strlen($sm->mime), Utils::PrintAsString($sm->forwardflag ? isset($sm->source->itemid) ? $sm->source->itemid : "error no itemid" : false), Utils::PrintAsString($sm->replyflag ? isset($sm->source->itemid) ? $sm->source->itemid : "error no itemid" : false), Utils::PrintAsString(isset($sm->source->folderid) ? $sm->source->folderid : false), Utils::PrintAsString($sm->saveinsent), Utils::PrintAsString(isset($sm->replacemime))));
     // by splitting the message in several lines we can easily grep later
     foreach (preg_split("/((\r)?\n)/", $sm->mime) as $rfc822line) {
         ZLog::Write(LOGLEVEL_WBXML, "RFC822: " . $rfc822line);
     }
     $sourceMessage = $sourceMail = false;
     // If we have a reference to a source message and we are not replacing mime (since we wouldn't use it)
     if (isset($sm->source->folderid) && isset($sm->source->itemid) && (!isset($sm->replacemime) || $sm->replacemime === false)) {
         ZLog::Write(LOGLEVEL_DEBUG, sprintf("BackendIMAP->SendMail(): We have a source message and we try to fetch it"));
         $parent = $this->getImapIdFromFolderId($sm->source->folderid);
         if ($parent === false) {
             throw new StatusException(sprintf("BackendIMAP->SendMail(): Could not get imapid from source folderid '%'", $sm->source->folderid), SYNC_COMMONSTATUS_ITEMNOTFOUND);
         } else {
             $this->imap_reopen_folder($parent);
             $sourceMail = @imap_fetchheader($this->mbox, $sm->source->itemid, FT_UID) . @imap_body($this->mbox, $sm->source->itemid, FT_PEEK | FT_UID);
             $mobj = new Mail_mimeDecode($sourceMail);
             $sourceMessage = $mobj->decode(array('decode_headers' => false, 'decode_bodies' => true, 'include_bodies' => true, 'rfc_822bodies' => true, 'charset' => 'utf-8'));
             unset($mobj);
             //We will need $sourceMail if the message is forwarded and not inlined
             // If it's a reply, we mark the original message as answered
             if ($sm->replyflag) {
                 if (!@imap_setflag_full($this->mbox, $sm->source->itemid, "\\Answered", ST_UID)) {
                     ZLog::Write(LOGLEVEL_WARN, sprintf("BackendIMAP->SendMail(): Unable to mark the message as Answered"));
                 }
             }
             // If it's a forward, we mark the original message as forwarded
             if ($sm->forwardflag) {
                 if (!@imap_setflag_full($this->mbox, $sm->source->itemid, "\\Forwarded", ST_UID)) {
                     ZLog::Write(LOGLEVEL_WARN, sprintf("BackendIMAP->SendMail(): Unable to mark the message as Forwarded"));
                 }
             }
         }
     }
     ZLog::Write(LOGLEVEL_DEBUG, sprintf("BackendIMAP->SendMail(): We get the new message"));
     $mobj = new Mail_mimeDecode($sm->mime);
     $message = $mobj->decode(array('decode_headers' => true, 'decode_bodies' => true, 'include_bodies' => true, 'rfc_822bodies' => true, 'charset' => 'utf-8'));
     unset($mobj);
     ZLog::Write(LOGLEVEL_DEBUG, sprintf("BackendIMAP->SendMail(): We get the From and To"));
     $Mail_RFC822 = new Mail_RFC822();
     $toaddr = "";
     $this->setFromHeaderValue($message->headers);
     $fromaddr = $this->parseAddr($Mail_RFC822->parseAddressList($message->headers["from"]));
     if (isset($message->headers["to"])) {
         $toaddr = $this->parseAddr($Mail_RFC822->parseAddressList($message->headers["to"]));
         ZLog::Write(LOGLEVEL_DEBUG, sprintf("BackendIMAP->SendMail(): To defined: %s", $toaddr));
     }
     unset($Mail_RFC822);
     // overwrite CC and BCC with the decoded versions, because we will parse/validate the address in the sending method
     if (isset($message->headers["cc"])) {
         $message->headers["cc"] = $message->headers["cc"];
     }
     if (isset($message->headers["bcc"])) {
         $message->headers["bcc"] = $message->headers["bcc"];
     }
     $this->setReturnPathValue($message->headers, $fromaddr);
     $finalBody = "";
     $finalHeaders = array();
     // if it's a S/MIME message I don't do anything with it
     if (is_smime($message)) {
         $mobj = new Mail_mimeDecode($sm->mime);
         $parts = $mobj->getSendArray();
         unset($mobj);
         if ($parts === false) {
             throw new StatusException(sprintf("BackendIMAP->SendMail(): Could not getSendArray for SMIME messages"), SYNC_COMMONSTATUS_MAILSUBMISSIONFAILED);
         } else {
             list($recipients, $finalHeaders, $finalBody) = $parts;
             $this->setFromHeaderValue($finalHeaders);
             $this->setReturnPathValue($finalHeaders, $fromaddr);
         }
     } else {
         //http://pear.php.net/manual/en/package.mail.mail-mime.example.php
         //http://pear.php.net/manual/en/package.mail.mail-mimedecode.decode.php
         //http://pear.php.net/manual/en/package.mail.mail-mimepart.addsubpart.php
         // I don't mind if the new message is multipart or not, I always will create a multipart. It's simpler
         $finalEmail = new Mail_mimePart('', array('content_type' => 'multipart/mixed'));
         if ($sm->replyflag && (!isset($sm->replacemime) || $sm->replacemime === false)) {
             ZLog::Write(LOGLEVEL_DEBUG, sprintf("BackendIMAP->SendMail(): Replying message"));
             $this->addTextParts($finalEmail, $message, $sourceMessage, true);
             if (isset($message->parts)) {
                 // We add extra parts from the replying message
                 add_extra_sub_parts($finalEmail, $message->parts);
             }
             // A replied message doesn't include the original attachments
         } else {
             if ($sm->forwardflag && (!isset($sm->replacemime) || $sm->replacemime === false)) {
                 if (!defined('IMAP_INLINE_FORWARD') || IMAP_INLINE_FORWARD === false) {
                     ZLog::Write(LOGLEVEL_DEBUG, "BackendIMAP->SendMail(): Forwarding message as attached file - eml");
                     $finalEmail->addSubPart($sourceMail, array('content_type' => 'message/rfc822', 'encoding' => 'base64', 'disposition' => 'attachment', 'dfilename' => 'forwarded_message.eml'));
                 } else {
                     ZLog::Write(LOGLEVEL_DEBUG, "BackendIMAP->SendMail(): Forwarding inlined message");
                     $this->addTextParts($finalEmail, $message, $sourceMessage, false);
                     if (isset($message->parts)) {
                         // We add extra parts from the forwarding message
                         add_extra_sub_parts($finalEmail, $message->parts);
                     }
                     if (isset($sourceMessage->parts)) {
                         // We add extra parts from the forwarded message
                         add_extra_sub_parts($finalEmail, $sourceMessage->parts);
                     }
                 }
             } else {
                 ZLog::Write(LOGLEVEL_DEBUG, sprintf("BackendIMAP->SendMail(): is a new message or we are replacing mime"));
                 $this->addTextPartsMessage($finalEmail, $message);
                 if (isset($message->parts)) {
                     // We add extra parts from the new message
                     add_extra_sub_parts($finalEmail, $message->parts);
                 }
             }
         }
         // We encode the final message
         $boundary = '=_' . md5(rand() . microtime());
         $finalEmail = $finalEmail->encode($boundary);
         $finalHeaders = array('Mime-Version' => '1.0');
         // We copy all the non-existent headers, minus content_type
         ZLog::Write(LOGLEVEL_DEBUG, sprintf("BackendIMAP->SendMail(): Copying new headers"));
         foreach ($message->headers as $k => $v) {
             if (strcasecmp($k, 'content-type') != 0 && strcasecmp($k, 'content-transfer-encoding') != 0 && strcasecmp($k, 'mime-version') != 0) {
                 if (!isset($finalHeaders[$k])) {
                     $finalHeaders[ucwords($k)] = $v;
                 }
             }
         }
         foreach ($finalEmail['headers'] as $k => $v) {
             if (!isset($finalHeaders[$k])) {
                 $finalHeaders[$k] = $v;
             }
         }
         $finalBody = "This is a multi-part message in MIME format.\n" . $finalEmail['body'];
         unset($finalEmail);
     }
     unset($sourceMail);
     unset($message);
     unset($sourceMessage);
     ZLog::Write(LOGLEVEL_DEBUG, sprintf("BackendIMAP->SendMail(): Final mail to send:"));
     foreach ($finalHeaders as $k => $v) {
         ZLog::Write(LOGLEVEL_WBXML, sprintf("%s: %s", $k, $v));
     }
     foreach (preg_split("/((\r)?\n)/", $finalBody) as $bodyline) {
         ZLog::Write(LOGLEVEL_WBXML, sprintf("Body: %s", $bodyline));
     }
     $send = $this->sendMessage($fromaddr, $toaddr, $finalHeaders, $finalBody);
     if ($send) {
         if (isset($sm->saveinsent)) {
             $this->saveSentMessage($finalHeaders, $finalBody);
         } else {
             ZLog::Write(LOGLEVEL_DEBUG, "BackendIMAP->SendMail(): Not saving in SentFolder");
         }
     }
     unset($finalHeaders);
     unset($finalBody);
     return $send;
 }
Esempio n. 3
0
 /**
  *
  *
  *
  *
  *  FIXME !!!! -- USE Pman_Core_Mailer !!!!!
  *
  *
  *
  *  
  */
 function buildMail($templateFile, $args)
 {
     $args = (array) $args;
     $content = clone $this;
     foreach ((array) $args as $k => $v) {
         $content->{$k} = $v;
     }
     $ff = HTML_FlexyFramework::get();
     //?? is this really the place for this???
     if (!$ff->cli && empty($args['no_auth']) && !in_array($templateFile, array('password_reset', 'password_welcome'))) {
         $content->authUser = $this->getAuthUser();
         if (!$content->authUser) {
             return PEAR::raiseError("Not authenticated");
         }
     }
     // should handle x-forwarded...
     $content->HTTP_HOST = isset($_SERVER["HTTP_HOST"]) ? $_SERVER["HTTP_HOST"] : (isset($ff->HTTP_HOST) ? $ff->HTTP_HOST : 'localhost');
     /* use the regex compiler, as it doesnt parse <tags */
     $tops = array('compiler' => 'Flexy', 'nonHTML' => true, 'filters' => array('SimpleTags', 'Mail'));
     if (!empty($args['templateDir'])) {
         $tops['templateDir'] = $args['templateDir'];
     }
     require_once 'HTML/Template/Flexy.php';
     $template = new HTML_Template_Flexy($tops);
     $template->compile("mail/{$templateFile}.txt");
     /* use variables from this object to ouput data. */
     $mailtext = $template->bufferedOutputObject($content);
     $htmlbody = false;
     // if a html file with the same name exists, use that as the body
     // I've no idea where this code went, it was here before..
     if (false !== $template->resolvePath("mail/{$templateFile}.html")) {
         $tops['nonHTML'] = false;
         $template = new HTML_Template_Flexy($tops);
         $template->compile("mail/{$templateFile}.html");
         $htmlbody = $template->bufferedOutputObject($content);
     }
     //echo "<PRE>";print_R($mailtext);
     //print_R($mailtext);exit;
     /* With the output try and send an email, using a few tricks in Mail_MimeDecode. */
     require_once 'Mail/mimeDecode.php';
     require_once 'Mail.php';
     $decoder = new Mail_mimeDecode($mailtext);
     $parts = $decoder->getSendArray();
     if (PEAR::isError($parts)) {
         return $parts;
         //echo "PROBLEM: {$parts->message}";
         //exit;
     }
     list($recipents, $headers, $body) = $parts;
     $recipents = array($this->email);
     if (!empty($content->bcc) && is_array($content->bcc)) {
         $recipents = array_merge($recipents, $content->bcc);
     }
     $headers['Date'] = date('r');
     if ($htmlbody !== false) {
         require_once 'Mail/mime.php';
         $mime = new Mail_mime(array('eol' => "\n"));
         $mime->setTXTBody($body);
         $mime->setHTMLBody($htmlbody);
         // I think there might be code in mediaoutreach toEmail somewhere
         // h embeds images here..
         $body = $mime->get();
         $headers = $mime->headers($headers);
     }
     return array('recipients' => $recipents, 'headers' => $headers, 'body' => $body);
 }
Esempio n. 4
0
 /**
  * ---------------- Global Tools ---------------   
  */
 function toData()
 {
     $templateFile = $this->template;
     $args = (array) $this->contents;
     $content = clone $this->page;
     foreach ($args as $k => $v) {
         $content->{$k} = $v;
     }
     $content->msgid = empty($content->msgid) ? md5(time() . rand()) : $content->msgid;
     $ff = HTML_FlexyFramework::get();
     $http_host = isset($_SERVER["HTTP_HOST"]) ? $_SERVER["HTTP_HOST"] : 'pman.HTTP_HOST.not.set';
     if (isset($ff->Pman['HTTP_HOST'])) {
         $http_host = $ff->Pman['HTTP_HOST'];
     }
     $content->HTTP_HOST = $http_host;
     // this should be done by having multiple template sources...!!!
     require_once 'HTML/Template/Flexy.php';
     $tmp_opts = array('site_prefix' => false);
     if (!empty($this->templateDir)) {
         $tmp_opts['templateDir'] = $this->templateDir;
     }
     $fopts = HTML_FlexyFramework::get()->HTML_Template_Flexy;
     if (!empty($fopts['DB_DataObject_translator'])) {
         $tmp_opts['DB_DataObject_translator'] = $fopts['DB_DataObject_translator'];
     }
     if (!empty($fopts['locale'])) {
         $tmp_opts['locale'] = $fopts['locale'];
     }
     // local opt's overwrite
     if (!empty($this->locale)) {
         $tmp_opts['locale'] = $this->locale;
     }
     $htmlbody = false;
     $html_tmp_opts = $tmp_opts;
     $htmltemplate = new HTML_Template_Flexy($html_tmp_opts);
     if (is_string($htmltemplate->resolvePath('mail/' . $templateFile . '.body.html'))) {
         // then we have a multi-part email...
         if (!empty($this->html_locale)) {
             $html_tmp_opts['locale'] = $this->html_locale;
         }
         $htmltemplate = new HTML_Template_Flexy($html_tmp_opts);
         $htmltemplate->compile('mail/' . $templateFile . '.body.html');
         $htmlbody = $htmltemplate->bufferedOutputObject($content);
         $this->htmlbody = $htmlbody;
         // for the html body, we may want to convert the attachments to images.
         //            var_dump($htmlbody);exit;
         if ($this->replaceImages) {
             $htmlbody = $this->htmlbodytoCID($htmlbody);
         }
         if ($this->css_embed) {
             $htmlbody = $this->htmlbodyCssEmbed($htmlbody);
         }
     }
     $tmp_opts['nonHTML'] = true;
     //print_R($tmp_opts);
     // $tmp_opts['force'] = true;
     $template = new HTML_Template_Flexy($tmp_opts);
     $template->compile('mail/' . $templateFile . '.txt');
     /* use variables from this object to ouput data. */
     $mailtext = $template->bufferedOutputObject($content);
     //print_r($mailtext);exit;
     //echo "<PRE>";print_R($mailtext);
     /* With the output try and send an email, using a few tricks in Mail_MimeDecode. */
     require_once 'Mail/mimeDecode.php';
     require_once 'Mail.php';
     $decoder = new Mail_mimeDecode($mailtext);
     $parts = $decoder->getSendArray();
     if (PEAR::isError($parts)) {
         return $parts;
         //echo "PROBLEM: {$parts->message}";
         //exit;
     }
     $isMime = false;
     require_once 'Mail/mime.php';
     $mime = new Mail_mime(array('eol' => "\n", 'html_charset' => 'utf-8', 'text_charset' => 'utf-8', 'head_charset' => 'utf-8'));
     // clean up the headers...
     $parts[1]['Message-Id'] = '<' . $content->msgid . '@' . $content->HTTP_HOST . '>';
     if ($htmlbody !== false) {
         // got a html headers...
         if (isset($parts[1]['Content-Type'])) {
             unset($parts[1]['Content-Type']);
         }
         $mime->setTXTBody($parts[2]);
         $mime->setHTMLBody($htmlbody);
         //            var_dump($mime);exit;
         foreach ($this->images as $cid => $cdata) {
             $mime->addHTMLImage($cdata['file'], $cdata['mimetype'], $cid . '.' . $cdata['ext'], true, $cdata['contentid']);
         }
         $isMime = true;
     }
     if (!empty($this->attachments)) {
         //if got a attachments
         $header = $mime->headers($parts[1]);
         if (isset($parts[1]['Content-Type'])) {
             unset($parts[1]['Content-Type']);
         }
         if (!$isMime) {
             if (preg_match('/text\\/html/', $header['Content-Type'])) {
                 $mime->setHTMLBody($parts[2]);
                 $mime->setTXTBody('This message is in HTML only');
             } else {
                 $mime->setTXTBody($parts[2]);
                 $mime->setHTMLBody('<PRE>' . htmlspecialchars($parts[2]) . '</PRE>');
             }
         }
         foreach ($this->attachments as $attch) {
             $mime->addAttachment($attch['file'], $attch['mimetype'], !empty($attch['name']) ? $attch['name'] : '', true);
         }
         $isMime = true;
     }
     if ($isMime) {
         $parts[2] = $mime->get();
         $parts[1] = $mime->headers($parts[1]);
     }
     $ret = array('recipents' => $parts[0], 'headers' => $parts[1], 'body' => $parts[2], 'mailer' => $this);
     if ($this->rcpts !== false) {
         $ret['recipents'] = $this->rcpts;
     }
     // if 'to' is empty, then add the recipents in there... (must be an array?
     if (!empty($ret['recipents']) && is_array($ret['recipents']) && (empty($ret['headers']['To']) || !strlen(trim($ret['headers']['To'])))) {
         $ret['headers']['To'] = implode(',', $ret['recipents']);
     }
     // add bcc if necessary..
     if (!empty($this->bcc)) {
         $ret['bcc'] = $this->bcc;
     }
     return $ret;
 }
<?php

require_once 'include/mimeDecode.php';
$mailtext = file_get_contents("testing/samples/messages/smime001.txt");
$decoder = new Mail_mimeDecode($mailtext);
$parts = $decoder->getSendArray();
if ($parts === false) {
    printf("ERROR splitting message\n");
} else {
    list($recipents, $headers, $body) = $parts;
    printf("RECIPIENTS\n");
    print_r($recipents);
    printf("\nHEADERS\n");
    print_r($headers);
    printf("\nBODY\n");
    print_r($body);
    printf("\n");
    //$mail = Mail::factory('smtp');
    //$mail->send($recipents,$headers,$body);
}