Beispiel #1
0
 public function getEncoding()
 {
     if (!$this->encoding) {
         $this->encoding = Mime::isPrintable($this->getFieldValue(HeaderInterface::FORMAT_RAW)) ? 'ASCII' : 'UTF-8';
     }
     return $this->encoding;
 }
Beispiel #2
0
 public function getEncoding()
 {
     if (!$this->encoding) {
         $this->encoding = Mime::isPrintable($this->subject) ? 'ASCII' : 'UTF-8';
     }
     return $this->encoding;
 }
 public function getFieldValue($format = HeaderInterface::FORMAT_RAW)
 {
     $prepared = $this->type;
     if (empty($this->parameters)) {
         return $prepared;
     }
     $values = [$prepared];
     foreach ($this->parameters as $attribute => $value) {
         if (HeaderInterface::FORMAT_ENCODED === $format && !Mime::isPrintable($value)) {
             $this->encoding = 'UTF-8';
             $value = HeaderWrap::wrap($value, $this);
             $this->encoding = 'ASCII';
         }
         $values[] = sprintf('%s="%s"', $attribute, $value);
     }
     return implode(';' . Headers::FOLDING, $values);
 }
Beispiel #4
0
 /**
  * Send a mail using this transport
  *
  * @param  \Zend\Mail\Mail $mail
  * @access public
  * @return void
  * @throws \Zend\Mail\Transport\Exception if mail is empty
  */
 public function send(\Zend\Mail\Mail $mail)
 {
     $this->_isMultipart = false;
     $this->_mail = $mail;
     $this->_parts = $mail->getParts();
     $mime = $mail->getMime();
     // Build body content
     $this->_buildBody();
     // Determine number of parts and boundary
     $count = count($this->_parts);
     $boundary = null;
     if ($count < 1) {
         throw new Exception('Empty mail cannot be sent');
     }
     if ($count > 1) {
         // Multipart message; create new MIME object and boundary
         $mime = new Mime\Mime($this->_mail->getMimeBoundary());
         $boundary = $mime->boundary();
     } elseif ($this->_isMultipart) {
         // multipart/alternative -- grab boundary
         $boundary = $this->_parts[0]->boundary;
     }
     // Determine recipients, and prepare headers
     $this->recipients = implode(',', $mail->getRecipients());
     $this->_prepareHeaders($this->_getHeaders($boundary));
     // Create message body
     // This is done so that the same \Zend\Mail\Mail object can be used in
     // multiple transports
     $message = new Mime\Message();
     $message->setParts($this->_parts);
     $message->setMime($mime);
     $this->body = $message->generateMessage($this->EOL);
     // Send to transport!
     $this->_sendMail();
 }
Beispiel #5
0
 /**
  * Encode header fields
  *
  * Encodes header content according to RFC1522 if it contains non-printable
  * characters.
  *
  * @param  string $value
  * @return string
  */
 protected function _encodeHeader($value)
 {
     if (Mime\Mime::isPrintable($value) === false) {
         if ($this->getHeaderEncoding() === Mime\Mime::ENCODING_QUOTEDPRINTABLE) {
             $value = Mime\Mime::encodeQuotedPrintableHeader($value, $this->getCharset(), Mime\Mime::LINELENGTH, Mime\Mime::LINEEND);
         } else {
             $value = Mime\Mime::encodeBase64Header($value, $this->getCharset(), Mime\Mime::LINELENGTH, Mime\Mime::LINEEND);
         }
     }
     return $value;
 }
Beispiel #6
0
 /**
  * MIME-encode a value
  *
  * Performs quoted-printable encoding on a value, setting maximum
  * line-length to 998.
  *
  * @param  string $value
  * @param  string $encoding
  * @param  int    $lineLength maximum line-length, by default 998
  * @return string Returns the mime encode value without the last line ending
  */
 public static function mimeEncodeValue($value, $encoding, $lineLength = 998)
 {
     return Mime::encodeQuotedPrintableHeader($value, $encoding, $lineLength, Headers::EOL);
 }
Beispiel #7
0
    /**
     * Assertion that checks if a given mailing header string is RFC conform.
     *
     * @param  string $header
     * @return void
     */
    protected function assertMailHeaderConformsToRfc($header)
    {
        $this->numAssertions++;
        $parts = explode(Mime\Mime::LINEEND, $header);
        if(count($parts) > 0) {
            for($i = 0; $i < count($parts); $i++) {
                if(preg_match('/(=?[a-z0-9-_]+\?[q|b]{1}\?)/i', $parts[$i], $matches)) {
                    $dce = sprintf("=?%s", $matches[0]);
                    // Check that Delimiter, Charset, Encoding are at the front of the string
                    if(substr(trim($parts[$i]), 0, strlen($dce)) != $dce) {
                        $this->fail(sprintf(
                            "Header-Part '%s' in line '%d' has missing or malformated delimiter, charset, encoding information.",
                            $parts[$i],
                            $i+1
                        ));
                    }
                    // check that the encoded word is not too long.);
                    // this is only some kind of suggestion by the standard, in PHP its hard to hold it, so we do not enforce it here.
                    /*if(strlen($parts[$i]) > 75) {
                        $this->fail(sprintf(
                            "Each encoded-word is only allowed to be 75 chars long, but line %d is %s chars long: %s",
                            $i+1,
                            strlen($parts[$i]),
                            $parts[$i]
                        ));
                    }*/
                    // Check that the end-delmiter ?= is correctly placed
                    if(substr(trim($parts[$i]), -2, 2) != "?=") {
                        $this->fail(sprintf(
                            "Lines with an encoded-word have to end in ?=, but line %d does not: %s",
                            $i+1,
                            substr(trim($parts[$i]), -2, 2)
                        ));
                    }

                    // Check that only one encoded-word can be found per line.
                    if(substr_count($parts[$i], "=?") != 1) {
                        $this->fail(sprintf(
                            "Only one encoded-word is allowed per line in the header. It seems line %d contains more: %s",
                            $i+1,
                            $parts[$i]
                        ));
                    }

                    // Check that the encoded-text only contains US-ASCII chars, and no space
                    $encodedText = substr(trim($parts[$i]), strlen($dce), -2);
                    if(preg_match('/([\s]+)/', $encodedText)) {
                        $this->fail(sprintf(
                            "No whitespace characters allowed in encoded-text of line %d: %s",
                            $i+1,
                            $parts[$i]
                        ));
                    }
                    for($i = 0; $i < strlen($encodedText); $i++) {
                        if(ord($encodedText[$i]) > 127) {
                            $this->fail(sprintf(
                                "No non US-ASCII characters allowed, but line %d has them: %s",
                                 $i+1,
                                 $parts[$i]
                            ));
                        }
                    }
                } else if(Mime\Mime::isPrintable($parts[$i]) == false) {
                    $this->fail(sprintf(
                        "Encoded-word in line %d contains non printable characters.",
                        $i+1
                    ));
                }
            }
        }
    }
Beispiel #8
0
 /**
  * @group ZF-1688
  */
 public function testLineLengthInQuotedPrintableHeaderEncoding()
 {
     $subject = "Alle meine Entchen schwimmen in dem See, schwimmen in dem See, Köpfchen in das Wasser, Schwänzchen in die Höh!";
     $encoded = Mime\Mime::encodeQuotedPrintableHeader($subject, "UTF-8", 100);
     foreach(explode(Mime\Mime::LINEEND, $encoded) AS $line ) {
         if(strlen($line) > 100) {
             $this->fail("Line '".$line."' is ".strlen($line)." chars long, only 100 allowed.");
         }
     }
     $encoded = Mime\Mime::encodeQuotedPrintableHeader($subject, "UTF-8", 40);
     foreach(explode(Mime\Mime::LINEEND, $encoded) AS $line ) {
         if(strlen($line) > 40) {
             $this->fail("Line '".$line."' is ".strlen($line)." chars long, only 40 allowed.");
         }
     }
 }