/** * Send an email. * * This is a re-implementation of PHP's mail() function using App Engine * mail API. The function relies on mailparse extension to parse emails. * * @param string $to Receiver, or receivers of the mail. * @param string $subject Subject of the email to be sent. * @param string $message Message to be sent. * @param string $additional_headers optional * String to be inserted at the end of the email header. * @param string $additional_parameters optional * Additional flags to be passed to the mail program. This arugment is * added only to match the signature of PHP's mail() function. The value is * always ignored. * @return bool * TRUE if the message is sent successfully, otherwise return FALSE. * * @see http://php.net/mail */ public static function sendMail($to, $subject, $message, $additional_headers = null, $additional_parameters = null) { $raw_mail = "To: {$to}\r\nSubject: {$subject}\r\n"; if ($additional_headers != null) { $raw_mail .= trim($additional_headers); } $raw_mail .= "\r\n\r\n{$message}"; $mime = mailparse_msg_create(); mailparse_msg_parse($mime, $raw_mail); $root_part = mailparse_msg_get_part_data($mime); // Set sender address based on the following order // 1. "From" header in $additional_headers // 2. "sendmail_from" ini setting // 3. Default address "mailer@<app-id>.appspotmail.com $from = ini_get('sendmail_from'); if (isset($root_part['headers']['from'])) { $from = $root_part['headers']['from']; } if ($from === false || $from == "") { $from = sprintf(self::DEFAULT_SENDER_ADDRESS_FORMAT, AppIdentityService::getApplicationId()); syslog(LOG_WARNING, "mail(): Unable to determine sender's email address from the " . "'sendmail_from' directive in php.ini or from the 'From' " . "header. Falling back to the default {$from}."); } $email = new Message(); try { $email->setSender($from); $email->addTo($root_part['headers']['to']); if (isset($root_part['headers']['cc'])) { $email->AddCc($root_part['headers']['cc']); } if (isset($root_part['headers']['bcc'])) { $email->AddBcc($root_part['headers']['bcc']); } $email->setSubject($root_part['headers']['subject']); $parts = mailparse_msg_get_structure($mime); if (count($parts) > 1) { foreach ($parts as $part_id) { $part = mailparse_msg_get_part($mime, $part_id); self::parseMimePart($part, $raw_mail, $email); } } else { if ($root_part['content-type'] == 'text/plain') { $email->setTextBody($message); } else { if ($root_part['content-type'] == 'text/html') { $email->setHtmlBody($message); } } } $email->send(); } catch (\Exception $e) { trigger_error('mail(): ' . $e->getMessage(), E_USER_WARNING); return false; } return true; }
/** * @param string $text * * @return $this */ public function setText(string $text) { $this->resource = mailparse_msg_create(); $eaten = 0; $text_length = strlen($text); while ($eaten < $text_length) { mailparse_msg_parse($this->resource, substr($text, $eaten, 2082)); $eaten += 2082; } $this->text = $text; return $this; }
/** * Set the email text * @return Object MimeMailParser Instance * @param $data String */ public function setText($data) { // NOTE: This has been modified for Phabricator. If the input data does not // end in a newline, Mailparse fails to include the last line in the mail // body. This happens somewhere deep, deep inside the mailparse extension, // so adding a newline here seems like the most straightforward fix. if (!preg_match('/\\n\\z/', $data)) { $data = $data . "\n"; } $this->resource = mailparse_msg_create(); // does not parse incrementally, fast memory hog might explode mailparse_msg_parse($this->resource, $data); $this->data = $data; $this->parse(); return $this; }
/** * Set the email text * @return Object MimeMailParser Instance * @param $data String */ public function setText($data) { $this->resource = mailparse_msg_create(); // does not parse incrementally, fast memory hog might explode mailparse_msg_parse($this->resource, $data); $this->data = $data; $this->parse(); return $this; }
rewind($fpdest); fpassthru($fpdest); echo "\nExtract via user function\n"; mailparse_msg_extract_part_file($mime, $fp); echo "\nExtract whole part to output\n"; mailparse_msg_extract_whole_part_file($mime, $fp); echo "\nExtract part from string to output\n"; mailparse_msg_extract_part($mime, $text); fclose($fpdest); fclose($fp); $output = ob_get_contents(); ob_end_clean(); VS($output, "Extract to output\n" . "hello, this is some text hello.\n" . "blah blah blah.\n" . "Extract and return as string\n" . "-->\n" . "hello, this is some text hello.\n" . "blah blah blah.\n" . "\n" . "Extract to open file\n" . "\n" . "rewinding\n" . "hello, this is some text hello.\n" . "blah blah blah.\n" . "\n" . "Extract via user function\n" . "hello, this is some text hello.\n" . "blah blah blah.\n" . "\n" . "Extract whole part to output\n" . "To: fred@bloggs.com\n" . "Mime-Version: 1.0\n" . "Content-Type: text/plain\n" . "Subject: A simple MIME message\n" . "\n" . "hello, this is some text hello.\n" . "blah blah blah.\n" . "\n" . "Extract part from string to output\n" . "hello, this is some text hello.\n" . "blah blah blah.\n"); ////////////////////////////////////////////////////////////////////// $msg = "Received: from mail pickup service by hotmail.com with Microsoft\n" . "SMTPSVC;\n" . "Sat, 18 Feb 2006 22:58:14 -0800\n" . "Received: from 66.178.40.49 by BAY116-DAV8.phx.gbl with DAV;\n" . "Sun, 19 Feb 2006 06:58:13 +0000\n" . "\n" . "test"; $mail = mailparse_msg_create(); mailparse_msg_parse($mail, $msg); $arr = mailparse_msg_get_structure($mail); foreach ($arr as $first => $second) { $section = mailparse_msg_get_part($mail, $second); $info = mailparse_msg_get_part_data($section); $received = array("from mail pickup service by hotmail.com with Microsoft", "from 66.178.40.49 by BAY116-DAV8.phx.gbl with DAV;"); VS($info, array("headers" => array("received" => $received), "starting-pos" => 0, "starting-pos-body" => 200, "ending-pos" => 200, "ending-pos-body" => 200, "line-count" => 6, "body-line-count" => 0, "charset" => "us-ascii", "transfer-encoding" => "8bit", "content-type" => "text/plain", "content-base" => "/")); } ////////////////////////////////////////////////////////////////////// $addresses = array("\":sysmail\"@ Some-Group. Some-Org, Muhammed." . "(I am the greatest) Ali @(the)Vegas.WBA", "\"strange\":\":sysmail\"@ Some-Group. Some-Org, Muhammed." . "(I am the greatest) Ali @(the)Vegas.WBA;"); ob_start(); foreach ($addresses as $first => $second) { $parsed = mailparse_rfc822_parse_addresses($second); foreach ($parsed as $pfirst => $psecond) { $pair = $psecond;
#!/usr/local/bin/php <?php ob_start('EmailOutput'); function EmailOutput($str) { if (strlen(trim($str)) > 0) { mail('*****@*****.**', 'Output from incomingmail.php', $str, 'From: TwitApps <*****@*****.**>', '*****@*****.**'); } } $data = file_get_contents('php://stdin'); $msg = mailparse_msg_create(); if (!mailparse_msg_parse($msg, $data)) { mail('*****@*****.**', 'TwitApps incoming mail: Parse failed', $data, 'From: TwitApps <*****@*****.**>', '*****@*****.**'); } else { $message = mailparse_msg_get_part($msg, 1); $info = mailparse_msg_get_part_data($message); if (!$message or !$info) { mail('*****@*****.**', 'TwitApps incoming mail: Failed to get message or info', $data, 'From: TwitApps <*****@*****.**>', '*****@*****.**'); } else { ob_start(); mailparse_msg_extract_part($message, $data); $body = ob_get_clean(); $body = urldecode($body); $body = iconv($info['charset'], 'UTF-8', $body); $body = html_entity_decode($body, ENT_NOQUOTES, 'UTF-8'); //mail('*****@*****.**', $info['headers']['subject'], $body."\n\n========================================\n\n".$data, 'From: TwitApps <*****@*****.**>', '*****@*****.**'); switch (@$info['headers']['x-twitterrecipientscreenname']) { case 'ta_follows': require dirname(__FILE__) . '/../follows/cli/incoming_mail.php'; break; case 'ta_replies':