Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
 
3
/*
4
 * This file is part of SwiftMailer.
5
 * (c) 2009 Fabien Potencier <fabien.potencier@gmail.com>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
 
11
/**
12
 * Stores Messages on the filesystem.
13
 * @package Swift
14
 * @author  Fabien Potencier
15
 */
16
class Swift_FileSpool extends Swift_ConfigurableSpool
17
{
18
  /** The spool directory */
19
  private $_path;
20
 
21
  /**
22
   * Create a new FileSpool.
23
   * @param string $path
24
   */
25
  public function __construct($path)
26
  {
27
    $this->_path = $path;
28
 
29
    if (!file_exists($this->_path))
30
    {
31
      mkdir($this->_path, 0777, true);
32
    }
33
  }
34
 
35
  /**
36
   * Tests if this Spool mechanism has started.
37
   *
38
   * @return boolean
39
   */
40
  public function isStarted()
41
  {
42
    return true;
43
  }
44
 
45
  /**
46
   * Starts this Spool mechanism.
47
   */
48
  public function start()
49
  {
50
  }
51
 
52
  /**
53
   * Stops this Spool mechanism.
54
   */
55
  public function stop()
56
  {
57
  }
58
 
59
  /**
60
   * Queues a message.
61
   * @param Swift_Mime_Message $message The message to store
62
   */
63
  public function queueMessage(Swift_Mime_Message $message)
64
  {
65
    $ser = serialize($message);
66
 
67
    file_put_contents($this->_path.'/'.md5($ser.uniqid()).'.message', $ser);
68
  }
69
 
70
  /**
71
   * Sends messages using the given transport instance.
72
   *
73
   * @param Swift_Transport $transport         A transport instance
74
   * @param string[]        &$failedRecipients An array of failures by-reference
75
   *
76
   * @return int The number of sent emails
77
   */
78
  public function flushQueue(Swift_Transport $transport, &$failedRecipients = null)
79
  {
80
    if (!$transport->isStarted())
81
    {
82
      $transport->start();
83
    }
84
 
85
    $failedRecipients = (array) $failedRecipients;
86
    $count = 0;
87
    $time = time();
88
    foreach (new DirectoryIterator($this->_path) as $file)
89
    {
90
      $file = $file->getRealPath();
91
 
92
      if (!strpos($file, '.message'))
93
      {
94
        continue;
95
      }
96
 
97
      $message = unserialize(file_get_contents($file));
98
 
99
      $count += $transport->send($message, $failedRecipients);
100
 
101
      unlink($file);
102
 
103
      if ($this->getMessageLimit() && $count >= $this->getMessageLimit())
104
      {
105
        break;
106
      }
107
 
108
      if ($this->getTimeLimit() && (time() - $time) >= $this->getTimeLimit())
109
      {
110
        break;
111
      }
112
    }
113
 
114
    return $count;
115
  }
116
}