/** * @param $mbox plain text format */ function insertMailsFromMbox($mbox) { if (!$mbox) { echo "Blank mbox. End."; return; } foreach (preg_split('/\\nFrom .+?\\n/', $mbox) as $r) { $e = new PlancakeEmailParser($r); $from = $e->getFieldDecoded('from'); $name = ""; if (preg_match("/(.*) at (.*) \\((.*)\\)/", $from, $matches)) { $from = "{$matches['1']}@{$matches['2']}"; $name = $matches[3]; } $subject = $e->getSubject(); $subject = preg_replace('/^\\[Talk-cz\\] */', '', $subject); dibi::query("INSERT INTO mailarchive", array("msgid" => $e->getHeader('message-id'), "replyid" => $e->getHeader('in-reply-to'), "date" => date("Y-m-d H:i:s", strtotime($e->getHeader('date'))), "from" => $from, "name" => $name, "subject" => $subject, "text" => $e->getPlainBody())); echo "."; } }
#!/usr/bin/php <?php require __DIR__ . '/../vendor/autoload.php'; $message = file_get_contents("php://stdin"); $config = new \Doctrine\DBAL\Configuration(); //.. $connectionParams = array('dbname' => 'mailcatcher', 'path' => __DIR__ . '/mailcatcher.sqlite', 'driver' => 'pdo_sqlite'); $parser = new PlancakeEmailParser($message); $conn = \Doctrine\DBAL\DriverManager::getConnection($connectionParams, $config); $conn->insert('sent_mails', array('sender' => 'localhost', 'recipient' => implode(', ', $parser->getTo()), 'subject' => $parser->getSubject(), 'content' => $parser->getHTMLBody(), 'sent' => time()));
} else { return preg_replace(ModerationConfig::BodyClearPattern, "", $msg->getHtmlBody()); } } $config = JFactory::getConfig(); $live_site = $config->get("live_site"); echo "<style>" . file_get_contents(ModerationConfig::CssFile) . "</style>"; $dirs = array(ModerationConfig::GetInboxDir() . "/new", ModerationConfig::GetInboxDir() . "/cur"); foreach ($dirs as $dir) { $files = scandir($dir); foreach ($files as $file) { if (is_dir("{$dir}/{$file}")) { continue; } $raw = "ctc-id: " . str_replace("-", "", MakeGuid()) . "\n" . file_get_contents("{$dir}/{$file}"); $msg = new PlancakeEmailParser($raw); $msgid = preg_replace('/,.*$/', "", $file); $from = $msg->getHeader("From"); $subject = $msg->getHeader("Subject"); $ctcid = $msg->getHeader("ctc-id"); $body = GetHtmlFromMessage($msg); $css = ModerationConfig::GetCss(); $step2Url = $live_site . "/" . ModerationConfig::Step2Url; $step2ImageUrl = $live_site . "/" . ModerationConfig::Step2DirectUrl; $lists = SqlResultArray($con, "select listid, listname from ctcweb9_ctc.mailchimp_lists"); $headers = "MIME-Version: 1.0\r\n" . "Content-type: text/html;charset=UTF-8\r\n" . "From: <" . ModerationConfig::SrcName . "@" . ModerationConfig::SrcDomain . ">\r\n"; $sender = ModerationConfig::SrcName . "@" . ModerationConfig::SrcDomain; echo "\t<table>\n\t\t\t<tr><th>msgid</th><td>{$msgid}</td>\n\t\t\t<tr><th>ctcid</th><td>{$ctcid}</td>\n\t\t\t<tr><th>Body</th><td>{$body}</td>"; $moderators = SqlResultArray($con, "\n\t\t\tselect memberid, primaryemail, firstname, lastname\n\t\t\tfrom ctcweb9_ctc.members m\n\t\t\tjoin ctcweb9_ctc.members_roles mr on mr.memberid = m.id \n\t\t\tjoin ctcweb9_ctc.roles r on r.id = mr.roleid and r.role = " . SqlVal(ModerationConfig::ModeratorRoleName) . ""); foreach ($moderators as $moderator) { $modid = $moderator["memberid"];
<?php // run this as: // php run_tests.php require_once "../PlancakeEmailParser.php"; $emails = glob('./emails/*'); echo "\r\n\r\n\r\n"; foreach ($emails as $email) { echo "Email {$email} \r\n"; $emailParser = new PlancakeEmailParser(file_get_contents($email)); echo "subject: " . $emailParser->getSubject() . "\r\n"; echo "body: " . $emailParser->getBody() . "\r\n"; echo "\r\n\r\n\r\n"; }
/** * @see sfTask */ protected function executeTask($env, $arguments = array(), $options = array()) { /** * Getting the directory where the emails are stored */ $inboxUser = sfConfig::get('app_emailToInbox_inboxUser'); $emailDomain = sfConfig::get('app_emailToInbox_mailServerDomain'); $newEmailPath = $arguments['emailFileAbsolutePath']; // there are some regular Plancake inbox email address that are // use just for spam $spamAccounts = array(); $spamAccounts[] = 'niki_5436'; // this will be interpreted as inbox_niki_5436@plancakebox.com $spamAccounts[] = 'niki.jones_15c522'; $this->log(''); $this->log(''); $this->log("parsing the email at " . $newEmailPath); $mailParser = new PlancakeEmailParser(file_get_contents($newEmailPath)); $plancakeSubjectOK = false; $plancakeRecipientOK = false; $emailTo = array(); $emailSubject = ''; $emailCc = $mailParser->getCc(); try { $emailTo = $mailParser->getTo(); } catch (Exception $e) { $this->handleFault("couldn't retrieve the 'to' header of the email", $newEmailPath); return; } try { $emailSubject = $mailParser->getSubject(); $plancakeSubjectOK = true; $this->log("got the subject of the email: " . $emailSubject); } catch (Exception $e) { $this->handleFault("couldn't retrieve the subject of the email", $newEmailPath); return; } $emailRecipients = array_merge($emailTo, $emailCc); $emailRecipients = implode(', ', $emailRecipients); $deliveredToHeader = $mailParser->getHeader('Delivered-To'); $emailRecipients = $deliveredToHeader . ', ' . $emailRecipients; $this->log("all recipients of the email: " . $emailRecipients); $internalEmail = false; // to flag an email sent to the catchall address $spamEmail = false; if (preg_match('/' . $inboxUser . "@{$emailDomain}/", $emailRecipients, $matches)) { $internalEmail = true; $this->log("discarding the email as it is an internal one"); if (is_file($newEmailPath)) { unlink($newEmailPath); } return; } if (preg_match("/inbox_([^@]+)@{$emailDomain}/i", $emailRecipients, $matches)) { // found Plancake Inbox address! $plancakeInbox = $matches[1]; if (in_array($plancakeInbox, $spamAccounts)) { $spamEmail = true; $this->handleFault("discarding the email because it is from a spammer", $newEmailPath); return; } else { $emailRecipient = 'inbox_' . $plancakeInbox . "@{$emailDomain}"; $plancakeRecipientOK = true; $this->log("got the Plancake recipient of the email: " . $emailRecipient); } } else { $this->handleFault("couldn't find a Plancake recipient for the email", $newEmailPath); return; } /** * Sorting the email into the database */ if ($plancakeRecipientOK && $plancakeSubjectOK) { $this->log('well done. For this email we got both the recipient and the subject. I can now create the task for the user.'); $emailRecipientWithoutDomain = str_replace("@{$emailDomain}", '', $emailRecipient); $c = new Criteria(); $c->add(PcPlancakeEmailAddressPeer::EMAIL, $emailRecipientWithoutDomain, Criteria::EQUAL); $plancakeEmail = PcPlancakeEmailAddressPeer::doSelectOne($c); if (is_object($plancakeEmail)) { // everything's OK $userId = $plancakeEmail->getUserId(); $user = PcUserPeer::retrieveByPk($userId); PcUserPeer::setLoggedInUser($user); // check whether there is a note for the task $note = $this->extractNote($mailParser->getPlainBody()); if (strlen($note)) { $this->log("note: {$note}"); } if (!strlen($emailSubject)) { $emailSubject = 'Something went wrong with a task you sent via email. Please contact us.'; } PcTaskPeer::createOrEdit($emailSubject, $user->getInbox()->getId(), 0, '', false, $note); $this->log('the email has successfully become a task for the user.'); } else { // something wrong $this->handleFault('no email user', $newEmailPath); $this->log('couldn\'t create a task from the email - the Plancake address is not in the system :-(.'); } } else { if ((!$plancakeRecipientOK || !$plancakeSubjectOK) && !$internalEmail && !$spamEmail) { // something wrong $this->handleFault('email parsing', $newEmailPath); $this->log("counldn't find both the recipient and the subject of the email. Nothing to do."); } } $this->log("deleting the email from the hard disk."); if (is_file($newEmailPath)) { unlink($newEmailPath); } $this->log(''); $this->log(''); }
/** * Gets the plaintext body. * * @return string */ public function getPlainBody() { return $this->email->getPlainBody(); }
public function handleIncomingMail($emailBuffer) { $pep = new PlancakeEmailParser($emailBuffer); $body = $pep->getBody(); if (empty($body) || !$body) { $body = $pep->getHtmlBody(); } $vcalendarStart = strpos($body, "BEGIN:VCALENDAR"); $vcalendarEnd = strpos($body, "END:VCALENDAR", $vcalendarStart); $vcalendarBody = substr($body, $vcalendarStart, $vcalendarEnd - $vcalendarStart); echo "subject: " . $pep->getSubject() . "\r\n"; echo "to:" . $pep->getTo()[0] . "\r\n"; echo "body: " . $body . "\r\n"; echo "vcalendarBody: " . $vcalendarBody . "\r\n"; $ical = new vCalendar($vcalendarBody); $this->handle_remote_attendee_reply($ical); }
<?php // run this as: // php run_tests.php function printBarrier() { echo "\r\n\r\n\r\n"; } function printnl($message) { echo "{$message}\r\n"; } require_once dirname(__DIR__) . DIRECTORY_SEPARATOR . "PlancakeEmailParser.php"; $emails = glob(__DIR__ . DIRECTORY_SEPARATOR . "emails" . DIRECTORY_SEPARATOR . "*"); printBarrier(); foreach ($emails as $email) { printnl("Email {$email}"); $emailParser = new PlancakeEmailParser(file_get_contents($email)); printnl("subject: " . $emailParser->getSubject()); printnl("body: " . $emailParser->getBody()); printBarrier(); }
$cTotal = @imap_num_msg($inst); $list = @pop3_list($inst); $stat = @pop3_stat($inst); $errRes[] = '* Mailbox Connection OK!'; $errRes[] = '* Total Record: ' . $cTotal; $bounceApp = new lethe(); if (!isset($stat['Unread']) || $stat['Unread'] <= 0 || !isset($stat) || !isset($list)) { $stat['Unread'] = 0; $errRes[] = '* Mailbox Empty or There No Unread Mail Found!'; } # Fetch if ($stat['Unread'] > 0) { foreach ($list as $row) { $msgHead = imap_fetchheader($inst, $row['msgno'], FT_UID); $msgBody = imap_fetchbody($inst, $row['msgno'], FT_UID); $emailParser = new PlancakeEmailParser($msgHead); # Check Encoding $chkEnc = $emailParser->getHeader('Content-Transfer-Encoding'); if (!empty($chkEnc)) { $msgBody = bodyDecoding($msgBody, $emailParser->getHeader('Content-Transfer-Encoding')); } # Get Lethe Campaign ID if (preg_match('/^X-Lethe-ID:(.*)/im', $msgBody, $matches)) { $letheID = $matches[1]; } else { $letheID = ''; } # Get Lethe Receiver if (preg_match('/^X-Lethe-Receiver:(.*)/im', $msgBody, $matches)) { $letheReceiver = $matches[1]; } else {
if ($action == "list") { $captionimg = $captionweb = "List the emails"; $subject = $body = $msgid = $raw = $ctcaction = ""; $list = array(); if (!$isImg) { $templist = array(array("name" => "Unmoderated", "folder" => ModerationConfig::GetUnmoderatedDir() . "/cur"), array("name" => "Moderated", "folder" => ModerationConfig::GetModeratedDir() . "/cur")); foreach ($templist as &$folder) { $files = scandir($folder["folder"]); $count = 0; foreach ($files as $file) { if (is_dir("{$folder['folder']}/{$file}") || preg_match("/\\.edited\\.html\$/", $file)) { continue; } $count++; $raw = file_get_contents("{$folder['folder']}/{$file}"); $msg = new PlancakeEmailParser($raw); $classname = explode(" ", $msg->getHeader("ctc-action")); $folder["emails"][] = array("msgid" => preg_replace('/,.*$/', "", $file), "ctcid" => $msg->getHeader("ctc-id"), "ctcaction" => $msg->getHeader("ctc-action"), "classname" => $classname[0] == "NOT" || $classname[0] == "" ? "warning" : $classname[0], "subject" => $msg->getHeader("Subject"), "from" => $msg->getHeader("From"), "date" => $msg->getHeader("Date")); } if ($count !== 0) { unset($folder["folder"]); $list[] = $folder; } } } } else { if ($msg == null) { $action = "deleted"; $captionimg = $captionweb = "This message does not exist"; $subject = $body = $msgid = $raw = $ctcaction = ""; } else {
public function emailParseBody($content) { $emailParser = new PlancakeEmailParser($content); // You can use some predefined methods to retrieve headers... $emailTo = $emailParser->getTo(); $emailSubject = $emailParser->getSubject(); $emailCc = $emailParser->getCc(); // ... or you can use the 'general purpose' method getHeader() $emailDeliveredToHeader = $emailParser->getHeader('Delivered-To'); $emailBody = $emailParser->getPlainBody(); return $emailBody; }