You can use ezcMail together with the other classes derived from ezcMailPart to build email messages. When the mail is built, use the Transport classes to send the mail. This example builds and sends a simple text mail message: $mail = new ezcMail; $mail->from = new ezcMailAddress( 'sender@example.com', 'Adrian Ripburger' ); $mail->addTo( new ezcMailAddress( 'receiver@example.com', 'Maureen Corley' ) ); $mail->subject = "Hi"; $mail->body = new ezcMailText( "I just mail to say I love you!" ); $transport = new ezcMailMtaTransport(); $transport->send( $mail ); By default, the ezcMail class will generate a mail with the Bcc header inside, and leave it to the SMTP server to strip the Bcc header. This can pose a problem with some SMTP servers which do not strip the Bcc header (issue #16154: Bcc headers are not stripped when using SMTP). Use the option stripBccHeader from {@link ezcMailOptions} to delete the Bcc header from the mail before it is sent. Example: $options = new ezcMailOptions(); $options->stripBccHeader = true; // default value is false $mail = new ezcMail( $options ); You can also derive your own mail classes from this class if you have special requirements. An example of this is the ezcMailComposer class which is a convenience class to send simple mail structures and HTML mail. There are several headers you can set on the mail object to achieve various effects: - Reply-To - Set this to an email address if you want people to reply to an address other than the from address. - Errors-To - If the mail can not be delivered the error message will be sent to this address.
Наследование: extends ezcMailPart
 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');
     }
 }
Пример #2
0
 public function createEntry(ezcMail $mail)
 {
     $this->entry = new qaBlogEntry();
     $this->entry->setSubject($mail->subject);
     $walkContext = new ezcMailPartWalkContext(array($this, 'walkPart'));
     $walkContext->filter = array('ezcMailText', 'ezcMailFile');
     $mail->walkParts($walkContext, $mail);
     return $this->entry;
 }
Пример #3
0
 /**
  * Sends the mail $mail using the PHP mail method.
  *
  * Note that a message may not arrive at the destination even though
  * it was accepted for delivery.
  *
  * @throws ezcMailTransportException
  *         if the mail was not accepted for delivery by the MTA.
  * @param ezcMail $mail
  */
 public function send(ezcMail $mail)
 {
     $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}";
     }
     $success = mail(ezcMailTools::composeEmailAddresses($mail->to), $mail->getHeader('Subject'), $mail->generateBody(), $headers, $additionalParameters);
     if ($success === false) {
         throw new ezcMailTransportException('The email could not be sent by sendmail');
     }
 }
Пример #4
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;
 }
 /**
  * Sends the ezcMail $mail using the SMTP protocol.
  *
  * If you want to send several emails use keepConnection() to leave the
  * connection to the server open between each mail.
  *
  * @throws ezcMailTransportException
  *         if the mail could not be sent
  * @throws ezcBaseFeatureNotFoundException
  *         if trying to use SSL and the openssl extension is not installed
  * @param ezcMail $mail
  */
 public function send(ezcMail $mail)
 {
     // sanity check the e-mail
     // need at least one recepient
     if (count($mail->to) + count($mail->cc) + count($mail->bcc) < 1) {
         throw new ezcMailTransportException("Can not send e-mail with no 'to' recipients.");
     }
     try {
         // open connection unless we are connected already.
         if ($this->status != self::STATUS_AUTHENTICATED) {
             $this->connect();
         }
         if (isset($mail->returnPath)) {
             $this->cmdMail($mail->returnPath->email);
         } else {
             $this->cmdMail($mail->from->email);
         }
         // each recepient must be listed here.
         // this controls where the mail is actually sent as SMTP does not
         // read the headers itself
         foreach ($mail->to as $address) {
             $this->cmdRcpt($address->email);
         }
         foreach ($mail->cc as $address) {
             $this->cmdRcpt($address->email);
         }
         foreach ($mail->bcc as $address) {
             $this->cmdRcpt($address->email);
         }
         // done with the from and recipients, lets send the mail itself
         $this->cmdData();
         // A '.' on a line ends the mail. Make sure this does not happen in
         // the data we want to send.  also called transparancy in the RFC,
         // section 4.5.2
         $data = $mail->generate();
         $data = str_replace(self::CRLF . '.', self::CRLF . '..', $data);
         if ($data[0] == '.') {
             $data = '.' . $data;
         }
         $this->sendData($data);
         $this->sendData('.');
         if ($this->getReplyCode($error) !== '250') {
             throw new ezcMailTransportSmtpException("Error: {$error}");
         }
     } catch (ezcMailTransportSmtpException $e) {
         throw new ezcMailTransportException($e->getMessage());
         // TODO: reset connection here.pin
     }
     // close connection unless we should keep it
     if ($this->keepConnection === false) {
         try {
             $this->disconnect();
         } catch (Exception $e) {
             // Eat! We don't care anyway since we are aborting the connection
         }
     }
 }
Пример #6
0
 /**
  * Returns true if the property $name is set, otherwise false.
  *
  * @param string $name
  * @return bool
  * @ignore
  */
 public function __isset($name)
 {
     switch ($name) {
         case 'plainText':
         case 'htmlText':
         case 'charset':
         case 'encoding':
             return isset($this->properties[$name]);
         case 'options':
             return isset($this->options);
         default:
             return parent::__isset($name);
     }
 }
Пример #7
0
 public function testMail4()
 {
     $mail = new ezcMail();
     $mail->from = new ezcMailAddress('*****@*****.**', 'Norwegian characters: æøå', 'iso-8859-1');
     $mail->addTo(new ezcMailAddress('*****@*****.**', 'More norwegian characters: æøå', 'iso-8859-1'));
     $mail->subject = 'Oslo ligger sør i Norge og har vært landets hovedstad i over 600 år.';
     $mail->subjectCharset = 'iso-8859-1';
     $mail->body = new ezcMailText('Oslo be grunnlagt rundt 1048 av Harald Hardråde.', 'iso-8859-1');
     $transport = new ezcMailMtaTransport();
     //        $transport->send( $mail );
 }
Пример #8
0
 public function testIsSet()
 {
     $mail = new ezcMail();
     $mail->generateBody();
     $mail->generateHeaders();
     $this->assertEquals(true, isset($mail->headers));
     $this->assertEquals(false, isset($mail->contentDisposition));
     $this->assertEquals(true, isset($mail->to));
     $this->assertEquals(true, isset($mail->cc));
     $this->assertEquals(true, isset($mail->bcc));
     $this->assertEquals(false, isset($mail->from));
     $this->assertEquals(false, isset($mail->subject));
     $this->assertEquals(true, isset($mail->subjectCharset));
     $this->assertEquals(false, isset($mail->body));
     $this->assertEquals(false, isset($mail->messageId));
     $this->assertEquals(false, isset($mail->messageID));
     $this->assertEquals(true, isset($mail->timestamp));
     $this->assertEquals(false, isset($mail->no_such_property));
 }
Пример #9
0
 public function testReplyToReply()
 {
     $mail = new ezcMail();
     $mail->addTo(new ezcMailAddress('*****@*****.**', 'Fræderik Hølljen', 'ISO-8859-1'));
     $address = new ezcMailAddress('*****@*****.**', 'Reply Går', 'ISO-8859-1');
     $mail->setHeader('Reply-To', ezcMailTools::composeEmailAddress($address));
     // $mail->setHeader( 'Reply-To', '*****@*****.**' );
     $reply = ezcMailTools::replyToMail($mail, new ezcMailAddress('*****@*****.**', 'Reply Går', 'ISO-8859-1'));
     $this->assertEquals($reply->to, array(new ezcMailAddress('*****@*****.**', "Reply GÃ¥r", 'utf-8')));
 }
Пример #10
0
<?php

require_once 'tutorial_autoload.php';
// Create a new mail object
$mail = new ezcMail();
// Specify the "from" mail address
$mail->from = new ezcMailAddress('*****@*****.**', 'Norwegian characters: æøå', 'iso-8859-1');
// Add one "to" mail address (multiple can be added)
$mail->addTo(new ezcMailAddress('*****@*****.**', 'More norwegian characters: æøå', 'iso-8859-1'));
// Specify the subject of the mail
$mail->subject = 'Oslo ligger sør i Norge og har vært landets hovedstad i over 600 år.';
// Specify the charset of the subject
$mail->subjectCharset = 'iso-8859-1';
// Add a header with the default charset (us-ascii)
$mail->setHeader('X-Related-City', 'Moscow');
// Add a header with a custom charset (iso-8859-5)
$mail->setHeader('X-Related-Movie', 'James Bond - Å leve og la dø', 'iso-8859-1');
// Specify the body as a text part, also specifying it's charset
$mail->body = new ezcMailText('Oslo be grunnlagt rundt 1048 av Harald Hardråde.', 'iso-8859-1');
// Create a new MTA transport object
$transport = new ezcMailMtaTransport();
// Use the MTA transport to send the created mail object
$transport->send($mail);
Пример #11
0
<?php

require_once 'tutorial_autoload.php';
// Create a new mail object
$mail = new ezcMail();
// Specify the "from" mail address
$mail->from = new ezcMailAddress('*****@*****.**', 'Boston Low');
// Add one "to" mail address (multiple can be added)
$mail->addTo(new ezcMailAddress('*****@*****.**', 'Maggie Robbins'));
// Specify the subject of the mail
$mail->subject = "This is the subject of the example mail";
// Specify the body text of the mail as a ezcMailText object
$mail->body = new ezcMailText("This is the body of the example mail.");
// Create a new MTA transport object
$transport = new ezcMailMtaTransport();
// Use the MTA transport to send the created mail object
$transport->send($mail);
Пример #12
0
 public function testMultipartReportFetchParts()
 {
     $mail = new ezcMail();
     $mail->from = new ezcMailAddress('*****@*****.**', 'Frederik Holljen');
     $mail->addTo(new ezcMailAddress('*****@*****.**', 'Frederik Holljen'));
     $mail->subject = "Report";
     $mail->subjectCharset = 'iso-8859-1';
     $delivery = new ezcMailDeliveryStatus();
     $delivery->message["Reporting-MTA"] = "dns; www.brssolutions.com";
     $lastRecipient = $delivery->createRecipient();
     $delivery->recipients[$lastRecipient]["Action"] = "failed";
     $mail->body = new ezcMailMultipartReport(new ezcMailText("Dette er body ßßæøååå", "iso-8859-1"), $delivery, new ezcMailText("The content initially sent"));
     $this->assertEquals("delivery-status", $mail->body->reportType);
     $msg = $mail->generate();
     $set = new ezcMailVariableSet($msg);
     $parser = new ezcMailParser();
     $mail = $parser->parseMail($set);
     $mail = $mail[0];
     $parts = $mail->fetchParts(null, true);
     $expected = array('ezcMailText', 'ezcMailDeliveryStatus', 'ezcMailText');
     $this->assertEquals(3, count($parts));
     for ($i = 0; $i < count($parts); $i++) {
         $this->assertEquals($expected[$i], get_class($parts[$i]));
     }
 }
Пример #13
0
 /**
  * Processes file attachments.
  *
  * @param ezcMail $mail
  */
 protected function processFiles(ezcMail $mail)
 {
     $context = new ezcMailPartWalkContext(array($this, 'addFile'));
     $context->filter = array('ezcMailFile');
     $mail->walkParts($context, $mail);
 }
Пример #14
0
<?php

require_once 'tutorial_autoload.php';
// Create a new mail object
$mail = new ezcMail();
// Specify the "from" mail address
$mail->from = new ezcMailAddress('*****@*****.**', 'Largo LaGrande');
// Add one "to" mail address (multiple can be added)
$mail->addTo(new ezcMailAddress('*****@*****.**', 'Wally B. Feed'));
// Specify the subject of the mail
$mail->subject = "This is the subject of the mail with a mail digest.";
// Create a text part to be added to the mail
$textPart = new ezcMailText("This is the body of the mail with a mail digest.");
// Specify the body of the mail as a multipart-mixed of the text part and a RFC822 digest object
// where $digest is an ezcMail object
// and RFC822Digest is the class from the previous example
$mail->body = new ezcMailMultipartMixed($textPart, new RFC822Digest($digest));
// Create a new MTA transport object
$transport = new ezcMailMtaTransport();
// Use the MTA transport to send the created mail object
$transport->send($mail);
Пример #15
0
 public function testTagInHeadersAndBody()
 {
     $imap = new ezcMailImapTransport(self::$server, self::$port);
     $imap->authenticate(self::$user, self::$password);
     $imap->createMailbox("Guybrush");
     $mail = new ezcMail();
     $mail->from = new ezcMailAddress('*****@*****.**', 'From');
     $mail->addTo(new ezcMailAddress('*****@*****.**', 'To'));
     $mail->subject = "A0000 A0001 A0002 A0003 A0004 A0005 A0006 A0007";
     $mail->body = new ezcMailText("A0000\nA0001\nA0002\nA0003\nA0004\nA0005\nA0006\nA0007");
     $data = $mail->generate();
     $imap->append("Guybrush", $data);
     $imap->append("Guybrush", $data, array('Answered'));
     $imap->selectMailbox("Guybrush");
     $set = $imap->fetchAll();
     $parser = new ezcMailParser();
     $mail = $parser->parseMail($set);
     $mail = $mail[0];
     $imap->selectMailbox("Inbox");
     $imap->deleteMailbox("Guybrush");
     $this->assertEquals('A0000 A0001 A0002 A0003 A0004 A0005 A0006 A0007', $mail->subject);
 }
Пример #16
0
 public function testContentDispositionLongHeader()
 {
     $mail = new ezcMail();
     $mail->from = new ezcMailAddress('*****@*****.**');
     $mail->subject = "яверасфăîţâşåæøåöä";
     $mail->addTo(new ezcMailAddress('*****@*****.**'));
     $file = new ezcMailFile(dirname(__FILE__) . "/parts/data/fly.jpg");
     $file->contentDisposition = new ezcMailContentDispositionHeader('attachment', 'яверасфăîţâşåæøåöäabcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz abcdefghijklmnopqrstuvwxyz abcdefghijklmnopqrstuvwxyz abcdefghijklmnopqrstuvwxyz.jpg');
     $mail->body = new ezcMailMultipartMixed(new ezcMailText('xxx'), $file);
     $msg = $mail->generate();
     $set = new ezcMailVariableSet($msg);
     $parser = new ezcMailParser();
     $mail = $parser->parseMail($set);
     $parts = $mail[0]->fetchParts();
     // for issue #13038, displayFileName was added to contentDisposition
     $file->contentDisposition->displayFileName = 'яверасфăîţâşåæøåöäabcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz abcdefghijklmnopqrstuvwxyz abcdefghijklmnopqrstuvwxyz abcdefghijklmnopqrstuvwxyz.jpg';
     $this->assertEquals($file->contentDisposition, $parts[1]->contentDisposition);
 }
Пример #17
0
<?php

require_once '/home/dotxp/dev/PHP/zetacomponents/trunk/Base/src/ezc_bootstrap.php';
$imapOptions = new ezcMailImapTransportOptions();
$imapOptions->ssl = true;
$imap = new ezcMailImapTransport('example.com', 993, $imapOptions);
$imap->authenticate('*****@*****.**', 'foo23bar');
$imap->selectMailbox('Inbox');
$mailSet = $imap->fetchAll();
$parser = new ezcMailParser();
$retMails = $parser->parseMail($mailSet);
$mail = new ezcMail();
$mail->from = new ezcMailAddress('*****@*****.**');
$mail->addTo(new ezcMailAddress('*****@*****.**', 'Any Body'));
$mail->subject = 'Daily digest';
$digest = new ezcMailMultipartDigest();
foreach ($retMails as $retMail) {
    $digest->appendPart(new ezcMailRfc822Digest($retMail));
}
$mail->body = $digest;
$transport = new ezcMailMtaTransport();
$transport->send($mail);
Пример #18
0
 /**
  * Returns a new mail object that is a reply to the current object.
  *
  * The new mail will have the correct to, cc, bcc and reference headers set.
  * It will not have any body set.
  *
  * By default the reply will only be sent to the sender of the original mail.
  * If $type is set to REPLY_ALL, all the original recipients will be included
  * in the reply.
  *
  * Use $subjectPrefix to set the prefix to the subject of the mail. The default
  * is to prefix with 'Re: '.
  *
  * @param ezcMail $mail
  * @param ezcMailAddress $from
  * @param int $type REPLY_SENDER or REPLY_ALL
  * @param string $subjectPrefix
  * @param string $mailClass
  * @return ezcMail
  */
 public static function replyToMail(ezcMail $mail, ezcMailAddress $from, $type = self::REPLY_SENDER, $subjectPrefix = "Re: ", $mailClass = "ezcMail")
 {
     $reply = new $mailClass();
     $reply->from = $from;
     // To = Reply-To if set
     if ($mail->getHeader('Reply-To') != '') {
         $reply->to = ezcMailTools::parseEmailAddresses($mail->getHeader('Reply-To'));
     } else {
         $reply->to = array($mail->from);
     }
     if ($type == self::REPLY_ALL) {
         // Cc = Cc + To - your own address
         $cc = array();
         foreach ($mail->to as $address) {
             if ($address->email != $from->email) {
                 $cc[] = $address;
             }
         }
         foreach ($mail->cc as $address) {
             if ($address->email != $from->email) {
                 $cc[] = $address;
             }
         }
         $reply->cc = $cc;
     }
     $reply->subject = $subjectPrefix . $mail->subject;
     if ($mail->getHeader('Message-Id')) {
         // In-Reply-To = Message-Id
         $reply->setHeader('In-Reply-To', $mail->getHeader('Message-ID'));
         // References = References . Message-Id
         if ($mail->getHeader('References') != '') {
             $reply->setHeader('References', $mail->getHeader('References') . ' ' . $mail->getHeader('Message-ID'));
         } else {
             $reply->setHeader('References', $mail->getHeader('Message-ID'));
         }
     } else {
         $reply->setHeader('References', $mail->getHeader('References'));
     }
     return $reply;
 }
Пример #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'));