function sendMail(ezcMail $mail)
 {
     $separator = "/";
     $mail->appendExcludeHeaders(array('to', 'subject'));
     $headers = rtrim($mail->generateHeaders());
     // rtrim removes the linebreak at the end, mail doesn't want it.
     if (count($mail->to) + count($mail->cc) + count($mail->bcc) < 1) {
         throw new ezcMailTransportException('No recipient addresses found in message header.');
     }
     $additionalParameters = "";
     if (isset($mail->returnPath)) {
         $additionalParameters = "-f{$mail->returnPath->email}";
     }
     $sys = eZSys::instance();
     $fname = time() . '-' . rand() . '.mail';
     $qdir = eZSys::siteDir() . eZSys::varDirectory() . $separator . 'mailq';
     $data = $headers . ezcMailTools::lineBreak();
     $data .= ezcMailTools::lineBreak();
     $data .= $mail->generateBody();
     $data = preg_replace('/(\\r\\n|\\r|\\n)/', "\r\n", $data);
     $success = eZFile::create($fname, $qdir, $data);
     if ($success === false) {
         throw new ezcMailTransportException('The email could not be sent by sendmail');
     }
 }
Esempio n. 2
0
 public function testFoldAny76LongWord()
 {
     $reference = "Thisisalongwordthatismorethan76characterslong.Let'sseehowthisishandledbyourlittlefolder That was the first space.";
     ezcMailHeaderFolder::setLimit(ezcMailHeaderFolder::SOFT_LIMIT);
     $folded = ezcMailHeaderFolder::foldAny($reference);
     $exploded = explode(ezcMailTools::lineBreak(), $folded);
     $this->assertEquals(2, count($exploded));
     $this->assertEquals(87, strlen($exploded[0]));
     $this->assertEquals(26, strlen($exploded[1]));
 }
Esempio n. 3
0
 /**
  * Override of original {@link ezcMail::generateHeaders()}.
  * Allows headers customization
  *
  * @return string The mail headers
  */
 public function generateHeaders()
 {
     // Workaround for encoded email addresses.
     // When encoded, email addresses (at least the name param) have more characters
     // By default, line length is set to 76 characters, after what a new line is created with $lineBreak.
     // This operation is done during encoding via iconv (see ezcMailTools::composeEmailAddress()).
     // Problem is that this operation is done a 2nd time in ezcMailPart::generateHeaders().
     // Following code ensures that there is no double $lineBreak introduced
     // by this process because it potentially breaks headers
     $lineBreak = ezcMailTools::lineBreak();
     $headers = str_replace("{$lineBreak}{$lineBreak}", $lineBreak, parent::generateHeaders());
     return $headers;
 }
Esempio n. 4
0
 /**
  * Returns the complete mail part including both the header and the body
  * as a string.
  *
  * @return string
  */
 public function generate()
 {
     return $this->generateHeaders() . ezcMailTools::lineBreak() . $this->generateBody();
 }
Esempio n. 5
0
 public function testFoldingAddresses()
 {
     $this->mail->from = new ezcMailAddress('*****@*****.**');
     $addresses = array('*****@*****.**', '*****@*****.**', '*****@*****.**', '*****@*****.**', '*****@*****.**', '*****@*****.**', '*****@*****.**');
     foreach ($addresses as $address) {
         $this->mail->addBcc(new ezcMailAddress($address));
     }
     $expected = "From: from@ez.no" . ezcMailTools::lineBreak() . "To: " . ezcMailTools::lineBreak() . "Bcc: nospam1@ez.no, nospam2@ez.no, nospam3@ez.no, nospam4@ez.no, nospam5@ez.no," . ezcMailTools::lineBreak() . " nospam6@ez.no, nospam7@ez.no" . ezcMailTools::lineBreak() . "Subject: " . ezcMailTools::lineBreak() . "MIME-Version: 1.0" . ezcMailTools::lineBreak() . "User-Agent: eZ Components";
     $return = $this->mail->generate();
     // cut away the Date and Message-ID headers as there is no way to predict what they will be
     $return = join(ezcMailTools::lineBreak(), array_slice(explode(ezcMailTools::lineBreak(), $return), 0, 7));
     $this->assertEquals($expected, $return);
 }
Esempio n. 6
0
 public function testEndline()
 {
     // defaul is \n\r as specified in RFC2045
     $this->assertEquals("\r\n", ezcMailTools::lineBreak());
     // now let's set it and check that it works
     ezcMailTools::setLineBreak("\n");
     $this->assertEquals("\n", ezcMailTools::lineBreak());
 }
Esempio n. 7
0
 /**
  * Test for issue #14487: Enable ezcMailComposer to specify encoding for text and html parts
  */
 public function testMailTextAndHtmlSetBase64Encoding()
 {
     $this->mail->from = new ezcMailAddress('*****@*****.**', 'Frederik Holljen');
     $this->mail->addTo(new ezcMailAddress('*****@*****.**', 'Frederik Holljen'));
     $this->mail->subject = "Alternative HTML/Text message.";
     $this->mail->plainText = "Plain text message. Your client should show the HTML message if it supports HTML mail.";
     $this->mail->htmlText = "<html><i><b>HTML message. Your client should show this if it supports HTML.</b></i></html>";
     $this->mail->encoding = ezcMail::BASE64;
     $this->mail->build();
     $message = $this->mail->generate();
     $this->parseAndCheckParts($message, array('ezcMailText', 'ezcMailText'));
     $expected = "PGh0bWw+PGk+PGI+SFRNTCBtZXNzYWdlLiBZb3VyIGNsaWVudCBzaG91bGQgc2hvdyB0aGlzIGlm" . ezcMailTools::lineBreak() . "IGl0IHN1cHBvcnRzIEhUTUwuPC9iPjwvaT48L2h0bWw+";
     // test if the $expected BASE64 encoded string is found somewhere in the generated mail (at position 759 usually)
     $this->assertGreaterThan(500, strpos($message, $expected));
 }
Esempio n. 8
0
 /**
  * Sets the correct stream filters for the attachment.
  *
  * $line should contain one line of data that should be written to file.
  * It is used to correctly determine the type of linebreak used in the mail.
  *
  * @param string $line
  */
 private function appendStreamFilters($line)
 {
     // append the correct decoding filter
     switch (strtolower($this->headers['Content-Transfer-Encoding'])) {
         case 'base64':
             stream_filter_append($this->fp, 'convert.base64-decode');
             break;
         case 'quoted-printable':
             // fetch the type of linebreak
             preg_match("/(\r\n|\r|\n)\$/", $line, $matches);
             $lb = count($matches) > 0 ? $matches[0] : ezcMailTools::lineBreak();
             $param = array('line-break-chars' => $lb);
             stream_filter_append($this->fp, 'convert.quoted-printable-decode', STREAM_FILTER_WRITE, $param);
             break;
         case '7bit':
         case '8bit':
             // do nothing here, file is already just binary
             break;
         default:
             // 7bit default
             break;
     }
 }
Esempio n. 9
0
 /**
  * Returns the contents of the file with the correct encoding.
  *
  * @return string
  */
 public function generateBody()
 {
     return chunk_split(base64_encode($this->contents), 76, ezcMailTools::lineBreak());
 }
Esempio n. 10
0
 /**
  * Returns the contents of the file with the correct encoding.
  *
  * The stream might become unusable after this if it doesn't support seek.
  *
  * @return string
  */
 public function generateBody()
 {
     $contents = stream_get_contents($this->stream);
     return chunk_split(base64_encode($contents), 76, ezcMailTools::lineBreak());
 }
Esempio n. 11
0
 public function testQuotedPrintableEncode()
 {
     $reference = "Content-Type: text/plain; charset=iso-8859-1" . ezcMailTools::lineBreak() . "Content-Transfer-Encoding: quoted-printable" . ezcMailTools::lineBreak() . ezcMailTools::lineBreak() . "=E6=F8=E5=0A=F8=E6=E5";
     $text = new ezcMailText("זרו\nרזו", "iso-8859-1", ezcMail::QUOTED_PRINTABLE);
     $this->assertEquals($reference, $text->generate());
 }
Esempio n. 12
0
 public function testMockSetHeaderWithEncodingNoCharsetReturnDefault()
 {
     $part = $this->getMock('ezcMailPart', array('setHeaderCharset', 'generateBody'), array());
     $part->expects($this->any())->method('setHeaderCharset')->will($this->returnValue(false));
     $part->expects($this->any())->method('generateBody')->will($this->returnValue(false));
     $part->setHeader("X-Related-Movie", 'James Bond - Шпион, который меня любил', 'iso-8859-5');
     $this->assertEquals('James Bond - Шпион, который меня любил', $part->getHeader('X-Related-Movie'));
     $expected = "X-Related-Movie: James Bond - Шпион, который меня любил" . ezcMailTools::lineBreak();
     $this->assertEquals($expected, $part->generateHeaders());
 }
Esempio n. 13
0
 /**
  * Returns the generated text body of this part as a string.
  *
  * @return string
  */
 public function generateBody()
 {
     switch ($this->encoding) {
         case ezcMail::BASE64:
             // leaves a \r\n to much at the end, but since it is base64 it will decode
             // properly so we just leave it
             return chunk_split(base64_encode($this->text), 76, ezcMailTools::lineBreak());
             break;
         case ezcMail::QUOTED_PRINTABLE:
             $text = preg_replace_callback('/[^\\x21-\\x3C\\x3E-\\x7E\\x09\\x20]/', function ($matches) {
                 return sprintf("=%02X", ord($matches[0]));
             }, $this->text);
             preg_match_all('/.{1,73}([^=]{0,2})?/', $text, $match);
             $text = implode('=' . ezcMailTools::lineBreak(), $match[0]);
             return $text;
             break;
         default:
             return preg_replace("/\r\n|\r|\n/", ezcMailTools::lineBreak(), $this->text);
     }
 }
Esempio n. 14
0
 /**
  * Returns the generated text for a section of the delivery-status part.
  *
  * @param ezcMailHeadersHolder $headers
  * @return string
  */
 private function addHeadersSection(ezcMailHeadersHolder $headers)
 {
     $result = "";
     foreach ($headers->getCaseSensitiveArray() as $header => $value) {
         $result .= $header . ": " . $value . ezcMailTools::lineBreak();
     }
     return $result;
 }
Esempio n. 15
0
 /**
  * Returns $text folded to the 998 character limit on any whitespace.
  *
  * The algorithm tries to minimize the number of comparisons by searching
  * backwards from the maximum number of allowed characters on a line.
  *
  * @param string $text
  * @return string
  */
 public static function foldAny($text)
 {
     // Don't fold unless we have to.
     if (strlen($text) <= self::$limit) {
         return $text;
     }
     // go to 998'th char.
     // search back to whitespace
     // fold
     $length = strlen($text);
     $folded = "";
     // find first occurence of whitespace searching backwards
     $search = 0;
     $previousFold = 0;
     while ($search + self::$limit < $length) {
         // search from the max possible length of the substring
         $search += self::$limit;
         while ($text[$search] != " " && $text[$search] != "\t" && $search > $previousFold) {
             $search--;
         }
         if ($search == $previousFold) {
             // continuous string of more than limit chars.
             // We will just have to continue searching forwards to the next whitespace instead
             // This is not confirming to standard.. but what can we do?
             $search += self::$limit;
             // back to where we started
             while ($search < $length && $text[$search] != " " && $text[$search] != "\t") {
                 $search++;
             }
         }
         // lets fold
         if ($folded === "") {
             $folded = substr($text, $previousFold, $search - $previousFold);
         } else {
             $folded .= ezcMailTools::lineBreak() . substr($text, $previousFold, $search - $previousFold);
         }
         $previousFold = $search;
     }
     // we need to append the rest if there is any
     if ($search < $length) {
         $folded .= ezcMailTools::lineBreak() . substr($text, $search);
     }
     return $folded;
 }
Esempio n. 16
0
 /**
  * Returns the contents of the file with the correct encoding.
  *
  * @return string
  */
 public function generateBody()
 {
     return chunk_split(base64_encode(file_get_contents($this->fileName)), 76, ezcMailTools::lineBreak());
 }
Esempio n. 17
0
 /**
  * Returns the generated body for all multipart types.
  *
  * @return string
  */
 public function generateBody()
 {
     $data = $this->noMimeMessage . ezcMailTools::lineBreak();
     foreach ($this->parts as $part) {
         $data .= ezcMailTools::lineBreak() . '--' . $this->boundary . ezcMailTools::lineBreak();
         $data .= $part->generate();
     }
     $data .= ezcMailTools::lineBreak() . '--' . $this->boundary . '--';
     return $data;
 }
Esempio n. 18
0
 /**
  * Sets the endLine $character(s) to use when generating mail.
  * The default is to use "\r\n" as specified by RFC 2045.
  *
  * @param string $characters
  */
 public static function setLineBreak($characters)
 {
     self::$lineBreak = $characters;
 }
Esempio n. 19
0
$mailAddresses = array(new ezcMailAddress('*****@*****.**', 'Jøhn Doe', 'ISO-8859-1'), new ezcMailAddress('*****@*****.**', 'Jane Doe'));
$addresses = '=?ISO-8859-1?B?SsO4aG4gRG9l?= <*****@*****.**>, Jane Doe <*****@*****.**';
// Convert ezcMailAddress to string representation
var_dump(ezcMailTools::composeEmailAddress($mailAddresses[0]));
var_dump(ezcMailTools::composeEmailAddresses($mailAddresses));
// Convert string to ezcMailAddress
var_dump(ezcMailTools::parseEmailAddress($addresses));
var_dump(ezcMailTools::parseEmailAddresses($addresses));
// Validate an email address (with a regular expression, without checking for MX records)
$isValid = ezcMailTools::validateEmailAddress('*****@*****.**');
// Validate an email address with MX records check.
// MX record checking does not work on Windows due to the lack of getmxrr()
// and checkdnsrr() PHP functions. The ezcBaseFunctionalityNotSupportedException
// is thrown in this case.
// set this to your mail server, it is used in a
// 'HELO SMTP' command to validate against MX records
ezcMailTools::$mxValidateServer = 'your.mail.server';
// set this to a mail address such as '*****@*****.**', it is used in a
// 'MAIL FROM' SMTP command to validate against MX records
ezcMailTools::$mxValidateAddress = '*****@*****.**';
$isValid = ezcMailTools::validateEmailAddress('*****@*****.**', true);
// Create a new mail object
$mail = new ezcMail();
$mail->from = $mailAddresses[1];
$mail->addTo($mailAddresses[0]);
$mail->subject = "Top secret";
// Use the lineBreak() method
$mail->body = new ezcMailText("Confidential" . ezcMailTools::lineBreak() . "DO NOT READ");
$mail->generate();
// Create a reply message to the previous mail object
$reply = ezcMailTools::replyToMail($mail, new ezcMailAddress('*****@*****.**', 'Reply Guy'));