function SendMail($rfc822, $smartdata = array(), $protocolversion = false) { // file_put_contents(BASE_PATH."/mail.dmp/".$this->_folderid(), $rfc822); if ($protocolversion < 14.0) { debugLog("IMAP-SendMail: " . (isset($rfc822) ? $rfc822 : "") . "task: " . (isset($smartdata['task']) ? $smartdata['task'] : "") . " itemid: " . (isset($smartdata['itemid']) ? $smartdata['itemid'] : "") . " parent: " . (isset($smartdata['folderid']) ? $smartdata['folderid'] : "")); } $mimeParams = array('decode_headers' => false, 'decode_bodies' => true, 'include_bodies' => true, 'input' => $rfc822, 'crlf' => "\r\n", 'charset' => 'utf-8'); $mobj = new Mail_mimeDecode($mimeParams['input'], $mimeParams['crlf']); $message = $mobj->decode($mimeParams, $mimeParams['crlf']); $Mail_RFC822 = new Mail_RFC822(); $toaddr = $ccaddr = $bccaddr = ""; if (isset($message->headers["to"])) { $toaddr = $this->parseAddr($Mail_RFC822->parseAddressList($message->headers["to"])); } if (isset($message->headers["cc"])) { $ccaddr = $this->parseAddr($Mail_RFC822->parseAddressList($message->headers["cc"])); } if (isset($message->headers["bcc"])) { $bccaddr = $this->parseAddr($Mail_RFC822->parseAddressList($message->headers["bcc"])); } $map_user_fullname = false; if (defined('IMAP_USERNAME_FULLNAME') && strlen(IMAP_USERNAME_FULLNAME) > 0) { $map_user_fullname = unserialize(IMAP_USERNAME_FULLNAME); } $addedfullname = false; // save some headers when forwarding mails (content type & transfer-encoding) $headers = ""; $forward_h_ct = ""; $forward_h_cte = ""; $envelopefrom = ""; $use_orgbody = false; // clean up the transmitted headers // remove default headers because we are using imap_mail $changedfrom = false; $returnPathSet = false; $body_base64 = false; $org_charset = ""; $org_boundary = false; foreach ($message->headers as $k => $v) { if ($k == "subject" || $k == "to" || $k == "cc" || $k == "bcc" || $k == "sender") { continue; } debugLog("Header Sentmail: " . $k . " = " . trim($v)); if ($k == "content-type") { // if the message is a multipart message, then we should use the sent body if (preg_match("/multipart/i", $v)) { $use_orgbody = true; $org_boundary = $message->ctype_parameters["boundary"]; } // save the original content-type header for the body part when forwarding if ($smartdata['task'] == 'forward' && $smartdata['itemid'] && !$use_orgbody) { $forward_h_ct = $v; continue; } if ($smartdata['task'] == 'reply' && $smartdata['itemid'] && !$use_orgbody) { $forward_h_ct = $v; continue; } // set charset always to utf-8 $org_charset = $v; $v = preg_replace("/charset=([A-Za-z0-9-\"']+)/", "charset=\"utf-8\"", $v); } if ($k == "content-transfer-encoding") { // if the content was base64 encoded, encode the body again when sending if (trim($v) == "base64") { $body_base64 = true; } // save the original encoding header for the body part when forwarding if ($smartdata['task'] == 'forward' && $smartdata['itemid']) { $forward_h_cte = $v; continue; } } // if the message is a multipart message, then we should use the sent body if (($smartdata['task'] == 'new' || $smartdata['task'] == 'reply' || $smartdata['task'] == 'forward') && (isset($smartdata['replacemime']) && $smartdata['replacemime'] == true || $k == "content-type" && preg_match("/multipart/i", $v))) { $use_orgbody = true; $org_boundary = $message->ctype_parameters["boundary"]; } // check if "from"-header is set, do nothing if it's set // else set it to IMAP_DEFAULTFROM if ($k == "from") { if (trim($v)) { $changedfrom = true; ini_set('sendmail_from', $v); $fromaddr = $v; } elseif (!trim($v) && IMAP_DEFAULTFROM) { $changedfrom = true; if (IMAP_DEFAULTFROM == 'username') { $v = $this->_username; } else { if (IMAP_DEFAULTFROM == 'domain') { $v = $this->_domain; } else { $v = $this->_username . IMAP_DEFAULTFROM; } } $envelopefrom = "-f{$v}"; ini_set('sendmail_from', $v); $fromaddr = $v; } } // depending on from field and if username is in map_user_fullname, we add users full name here to the email // address if ($k == "from" && $map_user_fullname && isset($map_user_fullname[$this->_username])) { $addedfullname = true; $v_parts = explode(" ", str_replace(">", "", str_replace("<", "", $v))); $v = '"' . $map_user_fullname[$this->_username] . '" <' . $v_parts[sizeof($v_parts) - 1] . '>'; } // check if "Return-Path"-header is set if ($k == "return-path") { $returnPathSet = true; if (!trim($v) && IMAP_DEFAULTFROM) { if (IMAP_DEFAULTFROM == 'username') { $v = $this->_username; } else { if (IMAP_DEFAULTFROM == 'domain') { $v = $this->_domain; } else { $v = $this->_username . IMAP_DEFAULTFROM; } } $envelopefrom = "-f{$v}"; ini_set('sendmail_from', $v); $fromaddr = $v; } } // all other headers stay if ($headers) { $headers .= "\n"; } $headers .= ucfirst($k) . ": " . trim($v); } // set "From" header if not set on the device if (IMAP_DEFAULTFROM && !$changedfrom) { if (IMAP_DEFAULTFROM == 'username') { $v = $this->_username; } else { if (IMAP_DEFAULTFROM == 'domain') { $v = $this->_domain; } else { $v = $this->_username . IMAP_DEFAULTFROM; } } $envelopefrom = "-f{$v}"; ini_set('sendmail_from', $v); $fromaddr = $v; $v_parts = explode(" ", str_replace(">", "", str_replace("<", "", $v))); if ($map_user_fullname && isset($map_user_fullname[$this->_username])) { $v = '"' . $map_user_fullname[$this->_username] . '" <' . $v_parts[sizeof($v_parts) - 1] . '>'; } if ($headers) { $headers .= "\n"; } $headers .= 'From: ' . $v; } // set "Return-Path" header if not set on the device if (IMAP_DEFAULTFROM && !$returnPathSet) { if (IMAP_DEFAULTFROM == 'username') { $v = $this->_username; } else { if (IMAP_DEFAULTFROM == 'domain') { $v = $this->_domain; } else { $v = $this->_username . IMAP_DEFAULTFROM; } } if ($headers) { $headers .= "\n"; } $headers .= 'Return-Path: ' . $v; } // if this is a multipart message with a boundary, we must use the original body if ($use_orgbody) { debugLog("IMAP-Sendmail: use_orgbody = true"); list(, $body) = $mobj->_splitBodyHeader($rfc822); $repl_body = $this->getBody($message); } else { debugLog("IMAP-Sendmail: use_orgbody = false"); $body = $this->getBody($message); } if (isset($smartdata['replacemime']) && $smartdata['replacemime'] == true && isset($message->ctype_primary)) { if ($headers) { $headers .= "\n"; } $headers .= "Content-Type: " . $message->ctype_primary . "/" . $message->ctype_secondary . (isset($message->ctype_parameters['boundary']) ? ";\n\tboundary=" . $message->ctype_parameters['boundary'] : ""); } $body = str_replace("\r", "", $body); // reply if ($smartdata['task'] == 'reply' && isset($smartdata['itemid']) && isset($smartdata['folderid']) && $smartdata['itemid'] && $smartdata['folderid'] && (!isset($smartdata['replacemime']) || isset($smartdata['replacemime']) && $smartdata['replacemime'] == false)) { $this->imap_reopenFolder($smartdata['folderid']); // receive entire mail (header + body) to decode body correctly if (defined("IMAP_USE_FETCHHEADER") && IMAP_USE_FETCHHEADER === false) { $origmail = @imap_fetchbody($this->_mbox, $smartdata['itemid'], "", FT_UID | FT_PEEK); } else { $origmail = @imap_fetchheader($this->_mbox, $smartdata['itemid'], FT_UID) . @imap_body($this->_mbox, $smartdata['itemid'], FT_PEEK | FT_UID); } $mobj2 = new Mail_mimeDecode($origmail); // receive only body $body .= $this->getBody($mobj2->decode(array('decode_headers' => false, 'decode_bodies' => true, 'include_bodies' => true, 'input' => $origmail, 'crlf' => "\n", 'charset' => 'utf-8'))); // unset mimedecoder & origmail - free memory unset($mobj2); unset($origmail); } // encode the body to base64 if it was sent originally in base64 by the pda // the encoded body is included in the forward if ($body_base64 && !$use_orgbody && !isset($forward)) { debugLog("IMAP-Sendmail: body_base64 = true and use_orgbody = false"); $body = chunk_split(base64_encode($body)); } else { debugLog("IMAP-Sendmail: body_base64 = false or use_orgbody = false"); } // forward if ($smartdata['task'] == 'forward' && isset($smartdata['itemid']) && isset($smartdata['folderid']) && $smartdata['itemid'] && $smartdata['folderid'] && (!isset($smartdata['replacemime']) || isset($smartdata['replacemime']) && $smartdata['replacemime'] == false)) { debugLog("IMAP Smartfordward is called"); $this->imap_reopenFolder($smartdata['folderid']); // receive entire mail (header + body) if (defined("IMAP_USE_FETCHHEADER") && IMAP_USE_FETCHHEADER === false) { $origmail = @imap_fetchbody($this->_mbox, $smartdata['itemid'], "", FT_UID | FT_PEEK); } else { $origmail = @imap_fetchheader($this->_mbox, $smartdata['itemid'], FT_UID) . @imap_body($this->_mbox, $smartdata['itemid'], FT_PEEK | FT_UID); } // build a new mime message, forward entire old mail as file if (!defined('IMAP_INLINE_FORWARD') || IMAP_INLINE_FORWARD === false) { if ($body_base64) { $body = chunk_split(base64_encode($body)); } $boundary = $org_boundary ? $org_boundary : false; // build a new mime message, forward entire old mail as file list($aheader, $body) = $this->mail_attach("forwarded_message.eml", strlen($origmail), $origmail, $body, $forward_h_ct, $forward_h_cte, $boundary); $headers .= "\n{$aheader}"; } else { $mobj2 = new Mail_mimeDecode($origmail); $mess2 = $mobj2->decode(array('decode_headers' => true, 'decode_bodies' => true, 'include_bodies' => true, 'charset' => 'utf-8')); if (!$use_orgbody) { $nbody = $body; } else { $nbody = $repl_body; } $nbody .= "\r\n\r\n"; $nbody .= "-----Original Message-----\r\n"; if (isset($mess2->headers['from'])) { $nbody .= "From: " . $mess2->headers['from'] . "\r\n"; } if (isset($mess2->headers['to']) && strlen($mess2->headers['to']) > 0) { $nbody .= "To: " . $mess2->headers['to'] . "\r\n"; } if (isset($mess2->headers['cc']) && strlen($mess2->headers['cc']) > 0) { $nbody .= "Cc: " . $mess2->headers['cc'] . "\r\n"; } if (isset($mess2->headers['date'])) { $nbody .= "Sent: " . $mess2->headers['date'] . "\r\n"; } if (isset($mess2->headers['subject'])) { $nbody .= "Subject: " . $mess2->headers['subject'] . "\r\n"; } $nbody .= "\r\n"; $nbody .= $this->getBody($mess2); if ($body_base64) { $nbody = chunk_split(base64_encode($nbody)); if ($use_orgbody) { $repl_body = chunk_split(base64_encode($repl_body)); } } if ($use_orgbody) { debugLog("-------------------"); debugLog("old:\n'{$repl_body}'\nnew:\n'{$nbody}'\nund der body:\n'{$body}'"); //$body is quoted-printable encoded while $repl_body and $nbody are plain text, //so we need to decode $body in order replace to take place $body = str_replace($repl_body, $nbody, quoted_printable_decode($body)); } else { $body = $nbody; } if (isset($mess2->parts)) { $attached = false; if ($org_boundary) { $att_boundary = $org_boundary; // cut end boundary from body $body = substr($body, 0, strrpos($body, "--{$att_boundary}--")); } else { $att_boundary = strtoupper(md5(uniqid(time()))); // add boundary headers $headers .= "\n" . "Content-Type: multipart/mixed; boundary={$att_boundary}"; } foreach ($mess2->parts as $part) { if (isset($part->disposition) && ($part->disposition == "attachment" || $part->disposition == "inline")) { if (isset($part->d_parameters['filename'])) { $attname = $part->d_parameters['filename']; } else { if (isset($part->ctype_parameters['name'])) { $attname = $part->ctype_parameters['name']; } else { if (isset($part->headers['content-description'])) { $attname = $part->headers['content-description']; } else { $attname = "unknown attachment"; } } } // ignore html content if ($part->ctype_primary == "text" && $part->ctype_secondary == "html") { continue; } // if ($use_orgbody || $attached) { $body .= $this->enc_attach_file($att_boundary, $attname, strlen($part->body), $part->body, $part->ctype_primary . "/" . $part->ctype_secondary); } else { $encmail = $body; $attached = true; $body = $this->enc_multipart($att_boundary, $body, $forward_h_ct, $forward_h_cte); $body .= $this->enc_attach_file($att_boundary, $attname, strlen($part->body), $part->body, $part->ctype_primary . "/" . $part->ctype_secondary); } } } $body .= "--{$att_boundary}--\n\n"; } unset($mobj2); } // unset origmail - free memory unset($origmail); } // remove carriage-returns from body $body = str_replace("\r\n", "\n", $body); //advanced debugging //debugLog("IMAP-SendMail: parsed message: ". print_r($message,1)); debugLog("IMAP-SendMail: headers: {$headers}"); debugLog("IMAP-SendMail: subject: {$message->headers["subject"]}"); debugLog("IMAP-SendMail: body: {$body}"); // if (!defined('IMAP_USE_IMAPMAIL') || IMAP_USE_IMAPMAIL == true) { $send = @imap_mail($toaddr, !isset($message->headers["subject"]) ? "" : $message->headers["subject"], $body, $headers, $ccaddr, $bccaddr); } else { if (!empty($ccaddr)) { $headers .= "\nCc: {$ccaddr}"; } if (defined('INTERNAL_SMTPCLIENT_SERVERNAME') && INTERNAL_SMTPCLIENT_SERVERNAME != '') { $headers .= "\nSubject: " . (!isset($message->headers["subject"]) ? "" : $message->headers["subject"]); $headers .= "\nTo: " . (!isset($message->headers["to"]) ? "" : $message->headers["to"]); $send = @InternalSMTPClient($fromaddr, !isset($message->headers["to"]) ? "" : $message->headers["to"], !isset($message->headers["cc"]) ? "" : $message->headers["cc"], !isset($message->headers["bcc"]) ? "" : $message->headers["bcc"], $headers . "\n" . $body); } else { if (!empty($bccaddr)) { $headers .= "\nBcc: {$bccaddr}"; } $send = @mail($toaddr, !isset($message->headers["subject"]) ? "" : $message->headers["subject"], $body, $headers, $envelopefrom); } } // email sent? if (!$send) { debugLog("The email could not be sent. Last-IMAP-error: " . imap_last_error()); return 120; } // add message to the sent folder // build complete headers $headers .= "\nTo: {$toaddr}"; $headers .= "\nSubject: " . $message->headers["subject"]; if (!defined('IMAP_USE_IMAPMAIL') || IMAP_USE_IMAPMAIL == true) { if (!empty($ccaddr)) { $headers .= "\nCc: {$ccaddr}"; } if (!empty($bccaddr)) { $headers .= "\nBcc: {$bccaddr}"; } } //debugLog("IMAP-SendMail: complete headers: $headers"); $asf = false; if ($this->_sentID) { $asf = $this->addSentMessage($this->_sentID, $headers, $body); } else { if (defined('IMAP_SENTFOLDER') && IMAP_SENTFOLDER) { $asf = $this->addSentMessage(IMAP_SENTFOLDER, $headers, $body); debugLog("IMAP-SendMail: Outgoing mail saved in configured 'Sent' folder '" . IMAP_SENTFOLDER . "': " . ($asf ? "success" : "failed")); } else { debugLog("IMAP-SendMail: No Sent mailbox set"); if ($this->addSentMessage("INBOX.Sent", $headers, $body)) { debugLog("IMAP-SendMail: Outgoing mail saved in 'INBOX.Sent'"); $asf = true; } else { if ($this->addSentMessage("Sent", $headers, $body)) { debugLog("IMAP-SendMail: Outgoing mail saved in 'Sent'"); $asf = true; } else { if ($this->addSentMessage("Sent Items", $headers, $body)) { debugLog("IMAP-SendMail: Outgoing mail saved in 'Sent Items'"); $asf = true; } } } } } // unset mimedecoder - free memory unset($mobj); return $send && $asf; }
function SendMail($rfc822, $forward = false, $reply = false, $parent = false) { debugLog("IMAP-SendMail: " . $rfc822 . "for: {$forward} reply: {$reply} parent: {$parent}"); $mobj = new Mail_mimeDecode($rfc822); $message = $mobj->decode(array('decode_headers' => false, 'decode_bodies' => true, 'include_bodies' => true, 'input' => $rfc822, 'crlf' => "\n", 'charset' => 'utf-8')); $toaddr = $ccaddr = $bccaddr = ""; if (isset($message->headers["to"])) { $toaddr = $this->parseAddr(Mail_RFC822::parseAddressList($message->headers["to"])); } if (isset($message->headers["cc"])) { $ccaddr = $this->parseAddr(Mail_RFC822::parseAddressList($message->headers["cc"])); } if (isset($message->headers["bcc"])) { $bccaddr = $this->parseAddr(Mail_RFC822::parseAddressList($message->headers["bcc"])); } // save some headers when forwarding mails (content type & transfer-encoding) $headers = ""; $forward_h_ct = ""; $forward_h_cte = ""; $use_orgbody = false; // clean up the transmitted headers // remove default headers because we are using imap_mail $changedfrom = false; $returnPathSet = false; $body_base64 = false; $org_charset = ""; foreach ($message->headers as $k => $v) { if ($k == "subject" || $k == "to" || $k == "cc" || $k == "bcc") { continue; } if ($k == "content-type") { // save the original content-type header for the body part when forwarding if ($forward) { $forward_h_ct = $v; continue; } // set charset always to utf-8 $org_charset = $v; $v = preg_replace("/charset=([A-Za-z0-9-\"']+)/", "charset=\"utf-8\"", $v); } if ($k == "content-transfer-encoding") { // if the content was base64 encoded, encode the body again when sending if (trim($v) == "base64") { $body_base64 = true; } // save the original encoding header for the body part when forwarding if ($forward) { $forward_h_cte = $v; continue; } } // if the message is a multipart message, then we should use the sent body if (!$forward && $k == "content-type" && preg_match("/multipart/i", $v)) { $use_orgbody = true; } // check if "from"-header is set if ($k == "from" && !trim($v) && IMAP_DEFAULTFROM) { $changedfrom = true; if (IMAP_DEFAULTFROM == 'username') { $v = $this->_username; } else { if (IMAP_DEFAULTFROM == 'domain') { $v = $this->_domain; } else { $v = $this->_username . IMAP_DEFAULTFROM; } } } // check if "Return-Path"-header is set if ($k == "return-path") { $returnPathSet = true; if (!trim($v) && IMAP_DEFAULTFROM) { if (IMAP_DEFAULTFROM == 'username') { $v = $this->_username; } else { if (IMAP_DEFAULTFROM == 'domain') { $v = $this->_domain; } else { $v = $this->_username . IMAP_DEFAULTFROM; } } } } // all other headers stay if ($headers) { $headers .= "\n"; } $headers .= ucfirst($k) . ": " . $v; } // set "From" header if not set on the device if (IMAP_DEFAULTFROM && !$changedfrom) { if (IMAP_DEFAULTFROM == 'username') { $v = $this->_username; } else { if (IMAP_DEFAULTFROM == 'domain') { $v = $this->_domain; } else { $v = $this->_username . IMAP_DEFAULTFROM; } } if ($headers) { $headers .= "\n"; } $headers .= 'From: ' . $v; } // set "Return-Path" header if not set on the device if (IMAP_DEFAULTFROM && !$returnPathSet) { if (IMAP_DEFAULTFROM == 'username') { $v = $this->_username; } else { if (IMAP_DEFAULTFROM == 'domain') { $v = $this->_domain; } else { $v = $this->_username . IMAP_DEFAULTFROM; } } if ($headers) { $headers .= "\n"; } $headers .= 'Return-Path: ' . $v; } // if this is a multipart message with a boundary, we must use the original body if ($use_orgbody) { list(, $body) = $mobj->_splitBodyHeader($rfc822); } else { $body = $this->getBody($message); } // reply if (isset($reply) && isset($parent) && $reply && $parent) { $this->imap_reopenFolder($parent); // receive entire mail (header + body) to decode body correctly $origmail = @imap_fetchheader($this->_mbox, $reply, FT_PREFETCHTEXT | FT_UID) . @imap_body($this->_mbox, $reply, FT_PEEK | FT_UID); $mobj2 = new Mail_mimeDecode($origmail); // receive only body $body .= $this->getBody($mobj2->decode(array('decode_headers' => false, 'decode_bodies' => true, 'include_bodies' => true, 'input' => $origmail, 'crlf' => "\n", 'charset' => 'utf-8'))); // unset mimedecoder & origmail - free memory unset($mobj2); unset($origmail); } // encode the body to base64 if it was sent originally in base64 by the pda // the encoded body is included in the forward if ($body_base64) { $body = base64_encode($body); } // forward if (isset($forward) && isset($parent) && $forward && $parent) { $this->imap_reopenFolder($parent); // receive entire mail (header + body) $origmail = @imap_fetchheader($this->_mbox, $forward, FT_PREFETCHTEXT | FT_UID) . @imap_body($this->_mbox, $forward, FT_PEEK | FT_UID); // build a new mime message, forward entire old mail as file list($aheader, $body) = $this->mail_attach("forwarded_message.eml", strlen($origmail), $origmail, $body, $forward_h_ct, $forward_h_cte); // unset origmail - free memory unset($origmail); // add boundary headers $headers .= "\n" . $aheader; } //advanced debugging //debugLog("IMAP-SendMail: parsed message: ". print_r($message,1)); //debugLog("IMAP-SendMail: headers: $headers"); //debugLog("IMAP-SendMail: subject: {$message->headers["subject"]}"); //debugLog("IMAP-SendMail: body: $body"); $send = @imap_mail($toaddr, $message->headers["subject"], $body, $headers, $ccaddr, $bccaddr); // email sent? if (!$send) { debugLog("The email could not be sent. Last-IMAP-error: " . imap_last_error()); } // add message to the sent folder // build complete headers $cheaders = "To: " . $toaddr . "\n"; $cheaders .= "Subject: " . $message->headers["subject"] . "\n"; $cheaders .= "Cc: " . $ccaddr . "\n"; $cheaders .= $headers; $asf = false; if ($this->_sentID) { $asf = $this->addSentMessage($this->_sentID, $cheaders, $body); } else { if (IMAP_SENTFOLDER) { $asf = $this->addSentMessage(IMAP_SENTFOLDER, $cheaders, $body); debugLog("IMAP-SendMail: Outgoing mail saved in configured 'Sent' folder '" . IMAP_SENTFOLDER . "': " . ($asf ? "success" : "failed")); } else { debugLog("IMAP-SendMail: No Sent mailbox set"); if ($this->addSentMessage("INBOX.Sent", $cheaders, $body)) { debugLog("IMAP-SendMail: Outgoing mail saved in 'INBOX.Sent'"); $asf = true; } else { if ($this->addSentMessage("Sent", $cheaders, $body)) { debugLog("IMAP-SendMail: Outgoing mail saved in 'Sent'"); $asf = true; } else { if ($this->addSentMessage("Sent Items", $cheaders, $body)) { debugLog("IMAP-SendMail: Outgoing mail saved in 'Sent Items'"); $asf = true; } } } } } // unset mimedecoder - free memory unset($mobj); return $send && $asf; }
public function SendMail($sm) { $forward = $reply = isset($sm->source->itemid) && $sm->source->itemid ? $sm->source->itemid : false; $parent = false; ZLog::Write(LOGLEVEL_DEBUG, sprintf("IMAPBackend->SendMail(): RFC822: %d bytes forward-id: '%s' reply-id: '%s' parent-id: '%s' SaveInSent: '%s' ReplaceMIME: '%s'", strlen($sm->mime), Utils::PrintAsString($sm->forwardflag), Utils::PrintAsString($sm->replyflag), Utils::PrintAsString(isset($sm->source->folderid) ? $sm->source->folderid : false), Utils::PrintAsString($sm->saveinsent), Utils::PrintAsString(isset($sm->replacemime)))); if (isset($sm->source->folderid) && $sm->source->folderid) { // convert parent folder id back to work on an imap-id $parent = $this->getImapIdFromFolderId($sm->source->folderid); } // 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); } $mobj = new Mail_mimeDecode($sm->mime); $message = $mobj->decode(array('decode_headers' => false, 'decode_bodies' => true, 'include_bodies' => true, 'charset' => 'utf-8')); $Mail_RFC822 = new Mail_RFC822(); $toaddr = $ccaddr = $bccaddr = ""; if (isset($message->headers["to"])) { $toaddr = $this->parseAddr($Mail_RFC822->parseAddressList($message->headers["to"])); } if (isset($message->headers["cc"])) { $ccaddr = $this->parseAddr($Mail_RFC822->parseAddressList($message->headers["cc"])); } if (isset($message->headers["bcc"])) { $bccaddr = $this->parseAddr($Mail_RFC822->parseAddressList($message->headers["bcc"])); } // save some headers when forwarding mails (content type & transfer-encoding) $headers = ""; $forward_h_ct = ""; $forward_h_cte = ""; $envelopefrom = ""; $use_orgbody = false; // clean up the transmitted headers // remove default headers because we are using imap_mail $changedfrom = false; $returnPathSet = false; $body_base64 = false; $org_charset = ""; $org_boundary = false; $multipartmixed = false; foreach ($message->headers as $k => $v) { if ($k == "subject" || $k == "to" || $k == "cc" || $k == "bcc") { continue; } if ($k == "content-type") { // if the message is a multipart message, then we should use the sent body if (preg_match("/multipart/i", $v)) { $use_orgbody = true; $org_boundary = $message->ctype_parameters["boundary"]; } // save the original content-type header for the body part when forwarding if ($sm->forwardflag && !$use_orgbody) { $forward_h_ct = $v; continue; } // set charset always to utf-8 $org_charset = $v; $v = preg_replace("/charset=([A-Za-z0-9-\"']+)/", "charset=\"utf-8\"", $v); } if ($k == "content-transfer-encoding") { // if the content was base64 encoded, encode the body again when sending if (trim($v) == "base64") { $body_base64 = true; } // save the original encoding header for the body part when forwarding if ($sm->forwardflag) { $forward_h_cte = $v; continue; } } // check if "from"-header is set, do nothing if it's set // else set it to IMAP_DEFAULTFROM if ($k == "from") { if (trim($v)) { $changedfrom = true; } elseif (!trim($v) && IMAP_DEFAULTFROM) { $changedfrom = true; if (IMAP_DEFAULTFROM == 'username') { $v = $this->username; } else { if (IMAP_DEFAULTFROM == 'domain') { $v = $this->domain; } else { $v = $this->username . IMAP_DEFAULTFROM; } } $envelopefrom = "-f{$v}"; } } // check if "Return-Path"-header is set if ($k == "return-path") { $returnPathSet = true; if (!trim($v) && IMAP_DEFAULTFROM) { if (IMAP_DEFAULTFROM == 'username') { $v = $this->username; } else { if (IMAP_DEFAULTFROM == 'domain') { $v = $this->domain; } else { $v = $this->username . IMAP_DEFAULTFROM; } } } } // all other headers stay if ($headers) { $headers .= "\n"; } $headers .= ucfirst($k) . ": " . $v; } // set "From" header if not set on the device if (IMAP_DEFAULTFROM && !$changedfrom) { if (IMAP_DEFAULTFROM == 'username') { $v = $this->username; } else { if (IMAP_DEFAULTFROM == 'domain') { $v = $this->domain; } else { $v = $this->username . IMAP_DEFAULTFROM; } } if ($headers) { $headers .= "\n"; } $headers .= 'From: ' . $v; $envelopefrom = "-f{$v}"; } // set "Return-Path" header if not set on the device if (IMAP_DEFAULTFROM && !$returnPathSet) { if (IMAP_DEFAULTFROM == 'username') { $v = $this->username; } else { if (IMAP_DEFAULTFROM == 'domain') { $v = $this->domain; } else { $v = $this->username . IMAP_DEFAULTFROM; } } if ($headers) { $headers .= "\n"; } $headers .= 'Return-Path: ' . $v; } // if this is a multipart message with a boundary, we must use the original body if ($use_orgbody) { list(, $body) = $mobj->_splitBodyHeader($sm->mime); $repl_body = $this->getBody($message); } else { $body = $this->getBody($message); } // reply if ($sm->replyflag && $parent) { $this->imap_reopenFolder($parent); // receive entire mail (header + body) to decode body correctly $origmail = @imap_fetchheader($this->mbox, $reply, FT_UID) . @imap_body($this->mbox, $reply, FT_PEEK | FT_UID); if (!$origmail) { throw new StatusException(sprintf("BackendIMAP->SendMail(): Could not open message id '%s' in folder id '%s' to be replied: %s", $reply, $parent, imap_last_error()), SYNC_COMMONSTATUS_ITEMNOTFOUND); } $mobj2 = new Mail_mimeDecode($origmail); // receive only body $body .= $this->getBody($mobj2->decode(array('decode_headers' => false, 'decode_bodies' => true, 'include_bodies' => true, 'charset' => 'utf-8'))); // unset mimedecoder & origmail - free memory unset($mobj2); unset($origmail); } // encode the body to base64 if it was sent originally in base64 by the pda // contrib - chunk base64 encoded body if ($body_base64 && !$sm->forwardflag) { $body = chunk_split(base64_encode($body)); } // forward if ($sm->forwardflag && $parent) { $this->imap_reopenFolder($parent); // receive entire mail (header + body) $origmail = @imap_fetchheader($this->mbox, $forward, FT_UID) . @imap_body($this->mbox, $forward, FT_PEEK | FT_UID); if (!$origmail) { throw new StatusException(sprintf("BackendIMAP->SendMail(): Could not open message id '%s' in folder id '%s' to be forwarded: %s", $forward, $parent, imap_last_error()), SYNC_COMMONSTATUS_ITEMNOTFOUND); } if (!defined('IMAP_INLINE_FORWARD') || IMAP_INLINE_FORWARD === false) { // contrib - chunk base64 encoded body if ($body_base64) { $body = chunk_split(base64_encode($body)); } //use original boundary if it's set $boundary = $org_boundary ? $org_boundary : false; // build a new mime message, forward entire old mail as file list($aheader, $body) = $this->mail_attach("forwarded_message.eml", strlen($origmail), $origmail, $body, $forward_h_ct, $forward_h_cte, $boundary); // add boundary headers $headers .= "\n" . $aheader; } else { $mobj2 = new Mail_mimeDecode($origmail); $mess2 = $mobj2->decode(array('decode_headers' => true, 'decode_bodies' => true, 'include_bodies' => true, 'charset' => 'utf-8')); if (!$use_orgbody) { $nbody = $body; } else { $nbody = $repl_body; } $nbody .= "\r\n\r\n"; $nbody .= "-----Original Message-----\r\n"; if (isset($mess2->headers['from'])) { $nbody .= "From: " . $mess2->headers['from'] . "\r\n"; } if (isset($mess2->headers['to']) && strlen($mess2->headers['to']) > 0) { $nbody .= "To: " . $mess2->headers['to'] . "\r\n"; } if (isset($mess2->headers['cc']) && strlen($mess2->headers['cc']) > 0) { $nbody .= "Cc: " . $mess2->headers['cc'] . "\r\n"; } if (isset($mess2->headers['date'])) { $nbody .= "Sent: " . $mess2->headers['date'] . "\r\n"; } if (isset($mess2->headers['subject'])) { $nbody .= "Subject: " . $mess2->headers['subject'] . "\r\n"; } $nbody .= "\r\n"; $nbody .= $this->getBody($mess2); if ($body_base64) { // contrib - chunk base64 encoded body $nbody = chunk_split(base64_encode($nbody)); if ($use_orgbody) { // contrib - chunk base64 encoded body $repl_body = chunk_split(base64_encode($repl_body)); } } if ($use_orgbody) { ZLog::Write(LOGLEVEL_DEBUG, "BackendIMAP->SendMail(): -------------------"); ZLog::Write(LOGLEVEL_DEBUG, "BackendIMAP->SendMail(): old:\n'{$repl_body}'\nnew:\n'{$nbody}'\nund der body:\n'{$body}'"); //$body is quoted-printable encoded while $repl_body and $nbody are plain text, //so we need to decode $body in order replace to take place $body = str_replace($repl_body, $nbody, quoted_printable_decode($body)); } else { $body = $nbody; } if (isset($mess2->parts)) { $attached = false; if ($org_boundary) { $att_boundary = $org_boundary; // cut end boundary from body $body = substr($body, 0, strrpos($body, "--{$att_boundary}--")); } else { $att_boundary = strtoupper(md5(uniqid(time()))); // add boundary headers $headers .= "\n" . "Content-Type: multipart/mixed; boundary={$att_boundary}"; $multipartmixed = true; } foreach ($mess2->parts as $part) { if (isset($part->disposition) && ($part->disposition == "attachment" || $part->disposition == "inline")) { if (isset($part->d_parameters['filename'])) { $attname = $part->d_parameters['filename']; } else { if (isset($part->ctype_parameters['name'])) { $attname = $part->ctype_parameters['name']; } else { if (isset($part->headers['content-description'])) { $attname = $part->headers['content-description']; } else { $attname = "unknown attachment"; } } } // ignore html content if ($part->ctype_primary == "text" && $part->ctype_secondary == "html") { continue; } // if ($use_orgbody || $attached) { $body .= $this->enc_attach_file($att_boundary, $attname, strlen($part->body), $part->body, $part->ctype_primary . "/" . $part->ctype_secondary); } else { $encmail = $body; $attached = true; $body = $this->enc_multipart($att_boundary, $body, $forward_h_ct, $forward_h_cte); $body .= $this->enc_attach_file($att_boundary, $attname, strlen($part->body), $part->body, $part->ctype_primary . "/" . $part->ctype_secondary); } } } if ($multipartmixed && strpos(strtolower($mess2->headers['content-type']), "alternative") !== false) { //this happens if a multipart/alternative message is forwarded //then it's a multipart/mixed message which consists of: //1. text/plain part which was written on the mobile //2. multipart/alternative part which is the original message $body = "This is a message with multiple parts in MIME format.\n--" . $att_boundary . "\nContent-Type: {$forward_h_ct}\nContent-Transfer-Encoding: {$forward_h_cte}\n\n" . ($body_base64 ? chunk_split(base64_encode($message->body)) : rtrim($message->body)) . "\n--" . $att_boundary . "\nContent-Type: {$mess2->headers['content-type']}\n\n" . @imap_body($this->mbox, $forward, FT_PEEK | FT_UID) . "\n\n"; } $body .= "--{$att_boundary}--\n\n"; } unset($mobj2); } // unset origmail - free memory unset($origmail); } // remove carriage-returns from body $body = str_replace("\r\n", "\n", $body); if (!$multipartmixed) { if (!empty($forward_h_ct)) { $headers .= "\nContent-Type: {$forward_h_ct}"; } if (!empty($forward_h_cte)) { $headers .= "\nContent-Transfer-Encoding: {$forward_h_cte}"; } // if body was quoted-printable, convert it again if (isset($message->headers["content-transfer-encoding"]) && strtolower($message->headers["content-transfer-encoding"]) == "quoted-printable") { $body = quoted_printable_encode($body); } } // more debugging ZLog::Write(LOGLEVEL_DEBUG, "BackendIMAP->SendMail(): parsed message: " . print_r($message, 1)); ZLog::Write(LOGLEVEL_DEBUG, "BackendIMAP->SendMail(): headers: {$headers}"); /* BEGIN fmbiete's contribution r1528, ZP-320 */ if (isset($message->headers["subject"])) { ZLog::Write(LOGLEVEL_DEBUG, "BackendIMAP->SendMail(): subject: {$message->headers["subject"]}"); } else { ZLog::Write(LOGLEVEL_DEBUG, "BackendIMAP->SendMail(): subject: no subject set. Set to empty."); $message->headers["subject"] = ""; // added by mku ZP-330 } /* END fmbiete's contribution r1528, ZP-320 */ ZLog::Write(LOGLEVEL_DEBUG, "BackendIMAP->SendMail(): body: {$body}"); if (!defined('IMAP_USE_IMAPMAIL') || IMAP_USE_IMAPMAIL == true) { // changed by mku ZP-330 $send = @imap_mail($toaddr, $message->headers["subject"], $body, $headers, $ccaddr, $bccaddr); } else { if (!empty($ccaddr)) { $headers .= "\nCc: {$ccaddr}"; } if (!empty($bccaddr)) { $headers .= "\nBcc: {$bccaddr}"; } // changed by mku ZP-330 $send = @mail($toaddr, $message->headers["subject"], $body, $headers, $envelopefrom); } // email sent? if (!$send) { throw new StatusException(sprintf("BackendIMAP->SendMail(): The email could not be sent. Last IMAP-error: %s", imap_last_error()), SYNC_COMMONSTATUS_MAILSUBMISSIONFAILED); } // add message to the sent folder // build complete headers $headers .= "\nTo: {$toaddr}"; $headers .= "\nSubject: " . $message->headers["subject"]; // changed by mku ZP-330 if (!defined('IMAP_USE_IMAPMAIL') || IMAP_USE_IMAPMAIL == true) { if (!empty($ccaddr)) { $headers .= "\nCc: {$ccaddr}"; } if (!empty($bccaddr)) { $headers .= "\nBcc: {$bccaddr}"; } } ZLog::Write(LOGLEVEL_DEBUG, "BackendIMAP->SendMail(): complete headers: {$headers}"); $asf = false; if ($this->sentID) { $asf = $this->addSentMessage($this->sentID, $headers, $body); } else { if (IMAP_SENTFOLDER) { $asf = $this->addSentMessage(IMAP_SENTFOLDER, $headers, $body); ZLog::Write(LOGLEVEL_DEBUG, sprintf("BackendIMAP->SendMail(): Outgoing mail saved in configured 'Sent' folder '%s': %s", IMAP_SENTFOLDER, Utils::PrintAsString($asf))); } else { ZLog::Write(LOGLEVEL_DEBUG, "BackendIMAP->SendMail(): No Sent mailbox set"); if ($this->addSentMessage("INBOX.Sent", $headers, $body)) { ZLog::Write(LOGLEVEL_DEBUG, "BackendIMAP->SendMail(): Outgoing mail saved in 'INBOX.Sent'"); $asf = true; } else { if ($this->addSentMessage("Sent", $headers, $body)) { ZLog::Write(LOGLEVEL_DEBUG, "BackendIMAP->SendMail(): Outgoing mail saved in 'Sent'"); $asf = true; } else { if ($this->addSentMessage("Sent Items", $headers, $body)) { ZLog::Write(LOGLEVEL_DEBUG, "BackendIMAP->SendMail():IMAP-SendMail: Outgoing mail saved in 'Sent Items'"); $asf = true; } } } } } if (!$asf) { ZLog::Write(LOGLEVEL_ERROR, "BackendIMAP->SendMail(): The email could not be saved to Sent Items folder. Check your configuration."); } return $send; }
function SendMail($rfc822, $forward = false, $reply = false, $parent = false) { debugLog("IMAP-SendMail: for: {$forward} reply: {$reply} parent: {$parent} RFC822: \n" . $rfc822); $mobj = new Mail_mimeDecode($rfc822); $message = $mobj->decode(array('decode_headers' => false, 'decode_bodies' => true, 'include_bodies' => true, 'charset' => 'utf-8')); $Mail_RFC822 = new Mail_RFC822(); $toaddr = $ccaddr = $bccaddr = ""; if (isset($message->headers["to"])) { $toaddr = $this->parseAddr($Mail_RFC822->parseAddressList($message->headers["to"])); } if (isset($message->headers["cc"])) { $ccaddr = $this->parseAddr($Mail_RFC822->parseAddressList($message->headers["cc"])); } if (isset($message->headers["bcc"])) { $bccaddr = $this->parseAddr($Mail_RFC822->parseAddressList($message->headers["bcc"])); } // save some headers when forwarding mails (content type & transfer-encoding) $headers = ""; $forward_h_ct = ""; $forward_h_cte = ""; $envelopefrom = ""; $use_orgbody = false; // clean up the transmitted headers // remove default headers because we are using imap_mail $changedfrom = false; $returnPathSet = false; $body_base64 = false; $org_charset = ""; $org_boundary = false; $multipartmixed = false; foreach ($message->headers as $k => $v) { if ($k == "subject" || $k == "to" || $k == "cc" || $k == "bcc") { continue; } if ($k == "content-type") { // if the message is a multipart message, then we should use the sent body if (preg_match("/multipart/i", $v)) { $use_orgbody = true; $org_boundary = $message->ctype_parameters["boundary"]; } // save the original content-type header for the body part when forwarding if ($forward && !$use_orgbody) { $forward_h_ct = $v; continue; } // set charset always to utf-8 $org_charset = $v; $v = preg_replace("/charset=([A-Za-z0-9-\"']+)/", "charset=\"utf-8\"", $v); } if ($k == "content-transfer-encoding") { // if the content was base64 encoded, encode the body again when sending if (trim($v) == "base64") { $body_base64 = true; } // save the original encoding header for the body part when forwarding if ($forward) { $forward_h_cte = $v; continue; } } // check if "from"-header is set, do nothing if it's set // else set it to IMAP_DEFAULTFROM if ($k == "from") { if (trim($v)) { $changedfrom = true; $envelopefrom = "-f{$v}"; } elseif (!trim($v) && IMAP_DEFAULTFROM) { $changedfrom = true; if (IMAP_DEFAULTFROM == 'username') { $v = $this->_username; } else { if (IMAP_DEFAULTFROM == 'domain') { $v = $this->_domain; } else { $v = $this->_username . IMAP_DEFAULTFROM; } } $envelopefrom = "-f{$v}"; } } // check if "Return-Path"-header is set if ($k == "return-path") { $returnPathSet = true; if (!trim($v) && IMAP_DEFAULTFROM) { if (IMAP_DEFAULTFROM == 'username') { $v = $this->_username; } else { if (IMAP_DEFAULTFROM == 'domain') { $v = $this->_domain; } else { $v = $this->_username . IMAP_DEFAULTFROM; } } } } // all other headers stay if ($headers) { $headers .= "\n"; } $headers .= ucfirst($k) . ": " . $v; } // set "From" header if not set on the device if (IMAP_DEFAULTFROM && !$changedfrom) { if (IMAP_DEFAULTFROM == 'username') { $v = $this->_username; } else { if (IMAP_DEFAULTFROM == 'domain') { $v = $this->_domain; } else { $v = $this->_username . IMAP_DEFAULTFROM; } } if ($headers) { $headers .= "\n"; } $headers .= 'From: ' . $v; $envelopefrom = "-f{$v}"; } // set "Return-Path" header if not set on the device if (IMAP_DEFAULTFROM && !$returnPathSet) { if (IMAP_DEFAULTFROM == 'username') { $v = $this->_username; } else { if (IMAP_DEFAULTFROM == 'domain') { $v = $this->_domain; } else { $v = $this->_username . IMAP_DEFAULTFROM; } } if ($headers) { $headers .= "\n"; } $headers .= 'Return-Path: ' . $v; } // if this is a multipart message with a boundary, we must use the original body if ($use_orgbody) { list(, $body) = $mobj->_splitBodyHeader($rfc822); $repl_body = $this->getBody($message); } else { $body = $this->getBody($message); } // reply if ($reply && $parent) { $this->imap_reopenFolder($parent); // receive entire mail (header + body) to decode body correctly $origmail = @imap_fetchheader($this->_mbox, $reply, FT_UID) . @imap_body($this->_mbox, $reply, FT_PEEK | FT_UID); $mobj2 = new Mail_mimeDecode($origmail); // receive only body $body .= $this->getBody($mobj2->decode(array('decode_headers' => false, 'decode_bodies' => true, 'include_bodies' => true, 'charset' => 'utf-8'))); // unset mimedecoder & origmail - free memory unset($mobj2); unset($origmail); } // encode the body to base64 if it was sent originally in base64 by the pda // contrib - chunk base64 encoded body if ($body_base64 && !$forward) { $body = chunk_split(base64_encode($body)); } // forward if ($forward && $parent) { $this->imap_reopenFolder($parent); // receive entire mail (header + body) $origmail = @imap_fetchheader($this->_mbox, $forward, FT_UID) . @imap_body($this->_mbox, $forward, FT_PEEK | FT_UID); if (!defined('IMAP_INLINE_FORWARD') || IMAP_INLINE_FORWARD === false) { // contrib - chunk base64 encoded body if ($body_base64) { $body = chunk_split(base64_encode($body)); } //use original boundary if it's set $boundary = $org_boundary ? $org_boundary : false; // build a new mime message, forward entire old mail as file list($aheader, $body) = $this->mail_attach("forwarded_message.eml", strlen($origmail), $origmail, $body, $forward_h_ct, $forward_h_cte, $boundary); // add boundary headers $headers .= "\n" . $aheader; } else { $mobj2 = new Mail_mimeDecode($origmail); $mess2 = $mobj2->decode(array('decode_headers' => true, 'decode_bodies' => true, 'include_bodies' => true, 'charset' => 'utf-8')); if (!$use_orgbody) { $nbody = $body; } else { $nbody = $repl_body; } $nbody .= "\r\n\r\n"; $nbody .= "-----Original Message-----\r\n"; if (isset($mess2->headers['from'])) { $nbody .= "From: " . $mess2->headers['from'] . "\r\n"; } if (isset($mess2->headers['to']) && strlen($mess2->headers['to']) > 0) { $nbody .= "To: " . $mess2->headers['to'] . "\r\n"; } if (isset($mess2->headers['cc']) && strlen($mess2->headers['cc']) > 0) { $nbody .= "Cc: " . $mess2->headers['cc'] . "\r\n"; } if (isset($mess2->headers['date'])) { $nbody .= "Sent: " . $mess2->headers['date'] . "\r\n"; } if (isset($mess2->headers['subject'])) { $nbody .= "Subject: " . $mess2->headers['subject'] . "\r\n"; } $nbody .= "\r\n"; $nbody .= $this->getBody($mess2); if ($body_base64) { // contrib - chunk base64 encoded body $nbody = chunk_split(base64_encode($nbody)); if ($use_orgbody) { // contrib - chunk base64 encoded body $repl_body = chunk_split(base64_encode($repl_body)); } } if ($use_orgbody) { debugLog("-------------------"); debugLog("old:\n'{$repl_body}'\nnew:\n'{$nbody}'\nund der body:\n'{$body}'"); //$body is quoted-printable encoded while $repl_body and $nbody are plain text, //so we need to decode $body in order replace to take place $body = str_replace($repl_body, $nbody, quoted_printable_decode($body)); } else { $body = $nbody; } if (isset($mess2->parts)) { $attached = false; if ($org_boundary) { $att_boundary = $org_boundary; // cut end boundary from body $body = substr($body, 0, strrpos($body, "--{$att_boundary}--")); } else { $att_boundary = strtoupper(md5(uniqid(time()))); // add boundary headers $headers .= "\n" . "Content-Type: multipart/mixed; boundary={$att_boundary}"; $multipartmixed = true; } foreach ($mess2->parts as $part) { if (isset($part->disposition) && ($part->disposition == "attachment" || $part->disposition == "inline")) { if (isset($part->d_parameters['filename'])) { $attname = $part->d_parameters['filename']; } else { if (isset($part->ctype_parameters['name'])) { $attname = $part->ctype_parameters['name']; } else { if (isset($part->headers['content-description'])) { $attname = $part->headers['content-description']; } else { $attname = "unknown attachment"; } } } // ignore html content if ($part->ctype_primary == "text" && $part->ctype_secondary == "html") { continue; } // if ($use_orgbody || $attached) { $body .= $this->enc_attach_file($att_boundary, $attname, strlen($part->body), $part->body, $part->ctype_primary . "/" . $part->ctype_secondary); } else { $encmail = $body; $attached = true; $body = $this->enc_multipart($att_boundary, $body, $forward_h_ct, $forward_h_cte); $body .= $this->enc_attach_file($att_boundary, $attname, strlen($part->body), $part->body, $part->ctype_primary . "/" . $part->ctype_secondary); } } } if ($multipartmixed && strpos(strtolower($mess2->headers['content-type']), "alternative") !== false) { //this happens if a multipart/alternative message is forwarded //then it's a multipart/mixed message which consists of: //1. text/plain part which was written on the mobile //2. multipart/alternative part which is the original message $body = "This is a message with multiple parts in MIME format.\n--" . $att_boundary . "\nContent-Type: {$forward_h_ct}\nContent-Transfer-Encoding: {$forward_h_cte}\n\n" . ($body_base64 ? chunk_split(base64_encode($message->body)) : rtrim($message->body)) . "\n--" . $att_boundary . "\nContent-Type: {$mess2->headers['content-type']}\n\n" . @imap_body($this->_mbox, $forward, FT_PEEK | FT_UID) . "\n\n"; } $body .= "--{$att_boundary}--\n\n"; } unset($mobj2); } // unset origmail - free memory unset($origmail); } // remove carriage-returns from body $body = str_replace("\r\n", "\n", $body); if (!$multipartmixed) { if (!empty($forward_h_ct)) { $headers .= "\nContent-Type: {$forward_h_ct}"; } if (!empty($forward_h_cte)) { $headers .= "\nContent-Transfer-Encoding: {$forward_h_cte}"; } //if body was quoted-printable, convert it again if (isset($message->headers["content-transfer-encoding"]) && strtolower($message->headers["content-transfer-encoding"]) == "quoted-printable") { $body = quoted_printable_encode($body); } } //advanced debugging //debugLog("IMAP-SendMail: parsed message: ". print_r($message,1)); //debugLog("IMAP-SendMail: headers: $headers"); //debugLog("IMAP-SendMail: subject: {$message->headers["subject"]}"); //debugLog("IMAP-SendMail: body: $body"); if (!defined('IMAP_USE_IMAPMAIL') || IMAP_USE_IMAPMAIL == true) { $send = @imap_mail($toaddr, $message->headers["subject"], $body, $headers, $ccaddr, $bccaddr); } else { if (!empty($ccaddr)) { $headers .= "\nCc: {$ccaddr}"; } if (!empty($bccaddr)) { $headers .= "\nBcc: {$bccaddr}"; } $send = @mail($toaddr, $message->headers["subject"], $body, $headers, $envelopefrom); } // email sent? if (!$send) { debugLog("The email could not be sent. Last-IMAP-error: " . imap_last_error()); } // add message to the sent folder // build complete headers $headers .= "\nTo: {$toaddr}"; $headers .= "\nSubject: " . $message->headers["subject"]; if (!defined('IMAP_USE_IMAPMAIL') || IMAP_USE_IMAPMAIL == true) { if (!empty($ccaddr)) { $headers .= "\nCc: {$ccaddr}"; } if (!empty($bccaddr)) { $headers .= "\nBcc: {$bccaddr}"; } } //debugLog("IMAP-SendMail: complete headers: $headers"); $asf = false; if ($this->_sentID) { $asf = $this->addSentMessage($this->_sentID, $headers, $body); } else { if (IMAP_SENTFOLDER) { $asf = $this->addSentMessage(IMAP_SENTFOLDER, $headers, $body); debugLog("IMAP-SendMail: Outgoing mail saved in configured 'Sent' folder '" . IMAP_SENTFOLDER . "': " . ($asf ? "success" : "failed")); } else { debugLog("IMAP-SendMail: No Sent mailbox set"); if ($this->addSentMessage("INBOX.Sent", $headers, $body)) { debugLog("IMAP-SendMail: Outgoing mail saved in 'INBOX.Sent'"); $asf = true; } else { if ($this->addSentMessage("Sent", $headers, $body)) { debugLog("IMAP-SendMail: Outgoing mail saved in 'Sent'"); $asf = true; } else { if ($this->addSentMessage("Sent Items", $headers, $body)) { debugLog("IMAP-SendMail: Outgoing mail saved in 'Sent Items'"); $asf = true; } } } } } // unset mimedecoder - free memory unset($mobj); return $send && $asf; }
function SendMail($rfc822, $forward = false, $reply = false, $parent = false) { debugLog("IMAP-SendMail: " . $rfc822 . "for: {$forward} reply: {$reply} parent: {$parent}"); $mobj = new Mail_mimeDecode($rfc822); $message = $mobj->decode(array('decode_headers' => true, 'decode_bodies' => true, 'include_bodies' => true, 'input' => $rfc822, 'crlf' => "\n", 'charset' => 'utf-8')); $toaddr = $ccaddr = $bccaddr = ""; if (isset($message->headers["to"])) { $toaddr = $this->parseAddr(Mail_RFC822::parseAddressList($message->headers["to"])); } if (isset($message->headers["cc"])) { $ccaddr = $this->parseAddr(Mail_RFC822::parseAddressList($message->headers["cc"])); } if (isset($message->headers["bcc"])) { $bccaddr = $this->parseAddr(Mail_RFC822::parseAddressList($message->headers["bcc"])); } // save some headers when forwarding mails (content type & transfer-encoding) $headers = ""; $forward_h_ct = ""; $forward_h_cte = ""; $use_orgbody = false; // clean up the transmitted headers // remove default headers because we are using imap_mail $changedfrom = false; foreach ($message->headers as $k => $v) { if ($k == "subject" || $k == "to" || $k == "cc" || $k == "bcc") { continue; } // save the original type & encoding headers for the body part if ($forward && $k == "content-type") { $forward_h_ct = $v; continue; } if ($forward && $k == "content-transfer-encoding") { $forward_h_cte = $v; } // if the message is a multipart message, then we should use the sent body if (!$forward && $k == "content-type" && preg_match("/multipart/i", $v)) { $use_orgbody = true; } // check if "from"-header is set if ($k == "from" && !empty($this->_config['IMAP_FORCEFROM'])) { $changedfrom = true; $v = $this->_config['IMAP_FORCEFROM']; $v = str_replace('%u', $this->_username, $v); $v = str_replace('%d', $this->_domain, $v); } // all other headers stay if ($headers) { $headers .= "\n"; } $headers .= ucfirst($k) . ": " . $v; } if (!empty($this->_config['IMAP_FORCEFROM']) && !$changedfrom) { $v = $this->_config['IMAP_FORCEFROM']; $v = str_replace('%u', $this->_username, $v); $v = str_replace('%d', $this->_domain, $v); if ($headers) { $headers .= "\n"; } $headers .= 'From: ' . $v; } // if this is a multipart message with a boundary, we must use the original body if ($use_orgbody) { list(, $body) = $mobj->_splitBodyHeader($rfc822); } else { $body = $this->getBody($message); } // reply if (isset($reply) && isset($parent) && $reply && $parent) { $this->imap_reopenFolder($parent); $origmail = @imap_body($this->_mbox, $reply, FT_PEEK | FT_UID); $mobj2 = new Mail_mimeDecode($origmail); // receive only body $body .= $this->getBody($mobj2->decode(array('decode_headers' => true, 'decode_bodies' => true, 'include_bodies' => true, 'input' => $origmail, 'crlf' => "\n", 'charset' => 'utf-8'))); } // forward if (isset($forward) && isset($parent) && $forward && $parent) { $this->imap_reopenFolder($parent); // receive entire mail (header + body) $origmail = @imap_fetchheader($this->_mbox, $forward, FT_PREFETCHTEXT | FT_UID) . @imap_body($this->_mbox, $forward, FT_PEEK | FT_UID); // build a new mime message, forward entire old mail as file list($aheader, $body) = $this->mail_attach("forwarded_message.eml", strlen($origmail), $origmail, $body, $forward_h_ct, $forward_h_cte); // add boundary headers $headers .= "\n" . $aheader; } //advanced debugging //debugLog("IMAP-SendMail: headers: $headers"); //debugLog("IMAP-SendMail: body: $body"); $send = @imap_mail($toaddr, $message->headers["subject"], $body, $headers, $ccaddr, $bccaddr); // add message to the sent folder // build complete headers $cheaders = "To: " . $toaddr . "\n"; $cheaders .= "Subject: " . $message->headers["subject"] . "\n"; $cheaders .= "Cc: " . $ccaddr . "\n"; $cheaders .= $headers; $asf = false; if ($this->_sentID) { $asf = $this->addSentMessage($this->_sentID, $cheaders, $body); } else { debugLog("IMAP-SendMail: No Sent mailbox set"); if (isset($this->_config['IMAP_FOLDERS'][SYNC_FOLDER_TYPE_SENTMAIL]) && !empty($this->_config['IMAP_FOLDERS'][SYNC_FOLDER_TYPE_SENTMAIL])) { if (is_array($this->_config['IMAP_FOLDERS'][SYNC_FOLDER_TYPE_SENTMAIL])) { foreach ($this->_config['IMAP_FOLDERS'][SYNC_FOLDER_TYPE_SENTMAIL] as $s) { if ($this->addSentMessage($s, $cheaders, $body)) { $asf = true; break; } } } else { if ($this->addSentMessage($this->_config['IMAP_FOLDERS'][SYNC_FOLDER_TYPE_SENTMAIL], $cheaders, $body)) { $asf = true; } } } } return $send && $asf; }
function get_raw_header_and_body($msg_uid = false) { $msg_uid or $msg_uid = $this->msg_uid; static $cache; if ($msg_uid) { if ($cache[$msg_uid]) { return $cache[$msg_uid]; } $header = imap_fetchheader($this->connection, $msg_uid, FT_UID); $body = imap_body($this->connection, $msg_uid, FT_UID); $cache[$msg_uid] = array($header, $body); return $cache[$msg_uid]; } else { if ($this->msg_text) { return Mail_mimeDecode::_splitBodyHeader($this->msg_text); } } }