/** * Return formatted and quoted address to insert into SMTP headers * @return string */ function toString() { # PHP's mail() implementation under Windows is somewhat s***e, and # can't handle "Joe Bloggs <*****@*****.**>" format email addresses, # so don't bother generating them if ($this->address) { if ($this->name != '' && !wfIsWindows()) { global $wgEnotifUseRealName; $name = $wgEnotifUseRealName && $this->realName ? $this->realName : $this->name; $quoted = UserMailer::quotedPrintable($name); if (strpos($quoted, '.') !== false || strpos($quoted, ',') !== false) { $quoted = '"' . $quoted . '"'; } return "{$quoted} <{$this->address}>"; } else { return $this->address; } } }
public static function send($headers, $to, $from, $subject, $body, $priority = 0, $attachments = null) { global $wgEnotifMaxRecips, $wgSMTP; wfProfileIn(__METHOD__); require_once 'Mail2.php'; require_once 'Mail2/mime.php'; $logContext = array_merge($headers, ['issue' => 'SOC-910', 'method' => __METHOD__, 'to' => $to, 'subject' => $subject]); WikiaLogger::instance()->info('Queuing email for SendGrid', $logContext); wfSuppressWarnings(); $headers['Subject'] = UserMailer::quotedPrintable($subject); // Add a header for the server-name (helps us route where SendGrid will send bounces). if (!empty($_SERVER) && isset($_SERVER['SERVER_NAME'])) { $headers["X-ServerName"] = $_SERVER['SERVER_NAME']; } try { $mail_object =& Mail2::factory(WikiaSendgridMailer::$factory, $wgSMTP); } catch (Exception $e) { $logContext['errorMessage'] = $e->getMessage(); WikiaLogger::instance()->info('Failed to create mail object', $logContext); wfDebug("PEAR::Mail factory failed: " . $e->getMessage() . "\n"); wfRestoreWarnings(); wfProfileOut(__METHOD__); return $e->getMessage(); } $email_body_txt = $email_body_html = ""; if (is_array($body)) { if (isset($body['text'])) { $email_body_txt = $body['text']; } if (isset($body['html'])) { $email_body_html = $body['html']; } } else { $email_body_txt = $body; } $mime = new Mail_mime(); $mime->setTXTBody($email_body_txt); $params = array('head_charset' => 'UTF-8', 'html_charset' => 'UTF-8', 'text_charset' => 'UTF-8', 'text_encoding' => 'quoted-printable', 'html_encoding' => 'quoted-printable'); # send email with attachements if (!empty($attachments)) { if (!is_array($attachments)) { $attachments = array($attachments); } foreach ($attachments as $file) { if (!is_array($file)) { $magic = MimeMagic::singleton(); $mimeType = $magic->guessMimeType($file); $ext_file = end(explode('.', $file)); $file = array('file' => $file, 'ext' => $ext_file, 'mime' => $mimeType); } $filename = $file['file']; $ext_filename = $file['ext']; if (!file_exists($filename)) { continue; } $name = $filename; #basename( $filename ); if ($ext_filename) { $name = $filename . "." . $ext_filename; } $mime->addAttachment($filename, $file['mime'], $name); } } # Old version (1.16 MW with Wikia changes) of sendHTML method if ($email_body_html) { $mime->setHTMLBody($email_body_html); //do not ever try to call these lines in reverse order } $body = $mime->get($params); $headers = $mime->headers($headers); wfDebug("Sending mail via WikiaSendgridMailer::send\n"); $chunks = array_chunk((array) $to, $wgEnotifMaxRecips); foreach ($chunks as $chunk) { $headers['To'] = $chunk; $status = self::sendWithPear($mail_object, $chunk, $headers, $body); if (!$status->isOK()) { $logContext['errorMessage'] = $status->getMessage(); WikiaLogger::instance()->info('Failed to create mail object', $logContext); wfRestoreWarnings(); wfProfileOut(__METHOD__); return $status->getMessage(); } } wfProfileOut(__METHOD__); # return false to return Status::newGood() in UserMailer::send method return false; }
/** * @covers UserMailer::quotedPrintable */ public function testQuotedPrintable() { $this->assertEquals("=?UTF-8?Q?=C4=88u=20legebla=3F?=", UserMailer::quotedPrintable("Ĉu legebla?", "UTF-8")); }