示例#1
0
 /**
  * Write data to the cache
  * @param string The cache key
  * @param string The data to write
  */
 function write($key, $data)
 {
     $handle = @fopen($GLOBALS["_SWIFT_FILECACHE_SAVE_PATH_"] . "/" . $this->prefix . $key, "ab");
     if (false === $handle || false === fwrite($handle, $data)) {
         Swift_ClassLoader::load("Swift_FileException");
         Swift_ClassLoader::load("Swift_Errors");
         Swift_Errors::trigger(new Swift_FileException("Disk Caching failed.  Tried to write to file at [" . $GLOBALS["_SWIFT_FILECACHE_SAVE_PATH_"] . "/" . $this->prefix . $key . "] but failed.  Check the permissions, or don't use disk caching."));
         return;
     }
     fclose($handle);
 }
示例#2
0
 /**
  * Set data for the image
  * This overrides setData() in Swift_Message_Attachment
  * @param Swift_File The data to set, as a file
  * @throws Swift_Message_MimeException If the image cannot be used, or the file is not
  */
 function setData(&$data, $read_filename = true)
 {
     if (!is_a($data, "Swift_File")) {
         trigger_error("Parameter 1 of " . __CLASS__ . "::" . __FUNCTION__ . " must be instance of Swift_File");
         return;
     }
     parent::setData($data, $read_filename);
     $img_data = @getimagesize($data->getPath());
     if (!$img_data) {
         Swift_Errors::trigger(new Swift_Message_MimeException("Cannot use file '" . $data->getPath() . "' as image since getimagesize() was unable to detect a file format. " . "Try using Swift_Message_EmbeddedFile instead"));
         return;
     }
     $type = image_type_to_mime_type($img_data[2]);
     $this->setContentType($type);
     if (!$this->getFileName()) {
         $this->setFileName($data->getFileName());
     }
 }
 /**
  * Get the value for a given attribute on a given header
  * @param string The name of the main header
  * @param string The name of the attribute
  * @return string
  * @throws Swift_Message_MimeException If no header is set
  */
 function getAttribute($header, $name)
 {
     if (!$this->has($header)) {
         Swift_Errors::trigger(new Swift_Message_MimeException("Cannot locate attribute '" . $name . "' for header '" . $header . "' as the header does not exist. " . "Consider using Swift_Message_Headers->has() to check."));
         return;
     }
     $name = strtolower($name);
     $lheader = strtolower($header);
     if ($this->hasAttribute($header, $name)) {
         return $this->attributes[$lheader][$name];
     }
 }
示例#4
0
文件: Swift.php 项目: jvinet/pronto
 /**
  * Throws an exception if the response code wanted does not match the one returned
  * @param Swift_Event_ResponseEvent The full response from the service
  * @param int The 3 digit response code wanted
  * @throws Swift_BadResponseException If the code does not match
  */
 function assertCorrectResponse(&$response, $codes)
 {
     if (!is_a($response, "Swift_Events_ResponseEvent")) {
         trigger_error("Swift::assertCorrectResponse expects parameter 1 to be of type Swift_Events_ResponseEvent.");
         return;
     }
     $codes = (array) $codes;
     if (!in_array($response->getCode(), $codes)) {
         $log =& Swift_LogContainer::getLog();
         $error = "Expected response code(s) [" . implode(", ", $codes) . "] but got response [" . $response->getString() . "]";
         if ($log->hasLevel(SWIFT_LOG_ERRORS)) {
             $log->add($error, SWIFT_LOG_ERROR);
         }
         Swift_Errors::trigger(new Swift_BadResponseException($error));
         return false;
     }
     return true;
 }
 /**
  * Run a batch send in a fail-safe manner.
  * This operates as Swift::batchSend() except it deals with errors itself.
  * @param Swift_Message To send
  * @param Swift_RecipientList Recipients (To: only)
  * @param Swift_Address The sender's address
  * @return int The number sent to
  */
 function send(&$message, &$recipients, $sender, $modified_subject = array())
 {
     $sent = 0;
     $successive_fails = 0;
     set_error_handler(array(&$this, "handleError"));
     foreach ($recipients->getTo() as $recipient) {
         $loop = true;
         $tries = 0;
         while ($loop && $tries < $this->getMaxTries()) {
             $tries++;
             $loop = false;
             $this->copyMessageHeaders($message);
             //BOF:mod
             if (!empty($modified_subject)) {
                 $temp = $modified_subject[$recipient->getAddress()];
                 if (!empty($temp)) {
                     $message->setSubject($temp);
                 }
             }
             //EOF:mod
             $sent += $n = $this->swift->send($message, $recipient, $sender, $modified_subject);
             if (!$n) {
                 $this->addFailedRecipient($recipient->getAddress());
             }
             if ($this->doRestart) {
                 $successive_fails++;
                 $this->restoreMessageHeaders($message);
                 if (($max = $this->getMaxSuccessiveFailures()) && $successive_fails > $max) {
                     restore_error_handler();
                     Swift_Errors::trigger(new Swift_Exception("Too many successive failures. BatchMailer is configured to allow no more than " . $max . " successive failures."));
                     return;
                 }
                 $loop = true;
                 //Give it one more shot
                 if ($t = $this->getSleepTime()) {
                     sleep($t);
                 }
                 $this->forceRestartSwift();
             } else {
                 $successive_fails = 0;
             }
         }
     }
     restore_error_handler();
     return $sent;
 }
示例#6
0
 /**
  * Establish an open file handle on the file if the file is not yet opened
  * @throws Swift_FileException If the file cannot be opened for reading
  */
 function createHandle()
 {
     if ($this->handle === null) {
         if (!($this->handle = fopen($this->path, "rb"))) {
             Swift_Errors::trigger(new Swift_FileException("Unable to open file '" . $this->path . " for reading.  Check the file permissions."));
             return;
         }
     }
 }
示例#7
0
 /**
  * Try to start the connection
  * @throws Swift_ConnectionException Upon failure to start
  */
 function start()
 {
     $log =& Swift_LogContainer::getLog();
     if ($log->hasLevel(SWIFT_LOG_EVERYTHING)) {
         $log->add("Trying to start a sendmail process.");
     }
     if (!$this->getPath() || !$this->getFlags()) {
         Swift_Errors::trigger(new Swift_ConnectionException("Sendmail cannot be started without a path to the binary including flags."));
         return;
     }
     if ($log->hasLevel(SWIFT_LOG_EVERYTHING)) {
         $log->add("Trying to stat the executable '" . $this->getPath() . "'.");
     }
     if (!@lstat($this->getPath())) {
         Swift_Errors::trigger(new Swift_ConnectionException("Sendmail cannot be seen with lstat().  The command given [" . $this->getCommand() . "] does not appear to be valid."));
         return;
     }
     $pipes_spec = array(array("pipe", "r"), array("pipe", "w"), array("pipe", "w"));
     $i = count($GLOBALS["_SWIFT_PROC"]);
     $GLOBALS["_SWIFT_PROC"][$i] = proc_open($this->getCommand(), $pipes_spec, $this->pipes);
     $this->proc =& $GLOBALS["_SWIFT_PROC"][$i];
     if (!$this->isAlive()) {
         Swift_Errors::trigger(new Swift_ConnectionException("The sendmail process failed to start.  Please verify that the path exists and PHP has permission to execute it."));
         return;
     }
 }
示例#8
0
 /**
  * Write a command to the server (leave off trailing CRLF)
  * @param string The command to send
  * @throws Swift_ConnectionException Upon failure to write
  */
 function write($command, $end = "\r\n")
 {
     if ($this->active === null) {
         Swift_Errors::trigger(new Swift_ConnectionException("None of the connections set have been started"));
         return;
     }
     return $this->connections[$this->active]->write($command, $end);
 }
示例#9
0
 function doMail($to, $subject, $message, $headers, $params)
 {
     $original_from = @ini_get("sendmail_from");
     @ini_set("sendmail_from", $this->returnPath);
     $headers = $headers->build();
     if (!ini_get("safe_mode")) {
         $success = mail($to, $subject, $message, $headers, $params);
     } else {
         $success = mail($to, $subject, $message, $headers);
     }
     if (!$success) {
         @ini_set("sendmail_from", $original_from);
         Swift_Errors::trigger(new Swift_ConnectionException("Sending failed using mail() as PHP's default mail() function returned boolean FALSE."));
         return;
     }
     @ini_set("sendmail_from", $original_from);
 }
示例#10
0
 /**
  * Try to close the connection
  * @throws Swift_ConnectionException Upon failure to close
  */
 function stop()
 {
     $log =& Swift_LogContainer::getLog();
     if ($log->hasLevel(SWIFT_LOG_EVERYTHING)) {
         $log->add("Closing down SMTP connection.");
     }
     if ($this->handle) {
         if (!fclose($this->handle)) {
             Swift_Errors::trigger(new Swift_ConnectionException("The SMTP connection could not be closed for an unknown reason." . $this->smtpErrors()));
             return;
         }
         $this->handle = null;
     }
 }
示例#11
0
 /**
  * Try to start the connection
  * @throws Swift_ConnectionException Upon failure to start
  */
 function start()
 {
     $log =& Swift_LogContainer::getLog();
     $fail_messages = array();
     foreach ($this->connections as $id => $conn) {
         Swift_Errors::expect($e, "Swift_ConnectionException");
         //
         $this->connections[$id]->start();
         if (!$e) {
             if ($this->connections[$id]->isAlive()) {
                 Swift_Errors::clear("Swift_ConnectionException");
                 $this->active = $id;
                 return true;
             }
             if ($log->hasLevel(SWIFT_LOG_EVERYTHING)) {
                 $log->add("Connection (" . $id . ") failed. Will try next connection if available.");
             }
             Swift_Errors::trigger(new Swift_ConnectionException("The connection started but reported that it was not active"));
         }
         $fail_messages[] = $id . ": " . $e->getMessage();
         $e = null;
     }
     $failure = implode("<br />", $fail_messages);
     Swift_Errors::trigger(new Swift_ConnectionException($failure));
 }
示例#12
0
 /**
  * Get a child document, identified by $id
  * @param string The identifier for this child
  * @return Swift_Message_Mime The child document
  * @throws Swift_Message_MimeException If no such child exists
  */
 function &getChild($id)
 {
     if ($this->hasChild($id)) {
         return $this->children[$id];
     } else {
         Swift_Errors::trigger(new Swift_Message_MimeException("Cannot retrieve child part identified by '" . $id . "' as it does not exist.  Consider using hasChild() to check."));
     }
 }
 /**
  * Write a command to the remote socket
  * @param string the command to send (without CRLF)
  * @throws Swift_ConnectionException If the command cannot be written
  */
 function write($command)
 {
     if (false !== fwrite($this->handle, $command . "\r\n")) {
         Swift_Errors::trigger(new Swift_ConnectionException("Data could not be written to the POP3 connection."));
         return;
     }
 }
示例#14
0
 /**
  * Remove a nested MIME part
  * @param string The ID of the attached part
  * @throws Swift_Message_MimeException If no such part exists
  */
 function detach($id)
 {
     if (Swift_Errors::halted()) {
         return;
     }
     Swift_Errors::expect($e, "Swift_Message_MimeException");
     //try
     switch (true) {
         case array_key_exists($id, $this->references["alternative"]):
             $parentRef =& $this->getReference("parent", "alternative");
             $parentRef->removeChild($id);
             unset($this->references["alternative"][$id]);
             break;
         case array_key_exists($id, $this->references["related"]):
             $parentRef =& $this->getReference("parent", "related");
             $parentRef->removeChild($id);
             unset($this->references["related"][$id]);
             break;
         case array_key_exists($id, $this->references["mixed"]):
             $parentRef =& $this->getReference("parent", "mixed");
             $parentRef->removeChild($id);
             unset($this->references["mixed"][$id]);
             break;
         default:
             trigger_error("Unable to detach part identified by ID '" . $id . "' since it's not registered.");
             break;
     }
     $this->postDetachFixStructure();
     $this->fixContentType();
     //catch
     if ($e) {
         Swift_Errors::trigger(new Swift_Message_MimeException("Something went wrong whilst trying to move some MIME parts during a detach(). " . "The MIME component threw an exception:<br />" . $e->getMessage()));
     } else {
         Swift_Errors::clear("Swift_Message_MimeException");
     }
 }