vendor/swiftmailer/doc/plugins.rst
author ymh <ymh.work@gmail.com>
Sat, 24 Sep 2011 15:40:41 +0200
changeset 0 7f95f8617b0b
permissions -rwxr-xr-x
first commit
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
     1
Plugins
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
     2
=======
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
     3
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
     4
Plugins are provided with Swift Mailer and can be used to extend the behavior
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
     5
of the library in ways that simple class inheritance would be more complex.
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
     6
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
     7
AntiFlood Plugin
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
     8
----------------
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
     9
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    10
Many SMTP servers have limits on the number of messages that may be sent
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    11
during any single SMTP connection. The AntiFlood plugin provides a way to stay
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    12
within this limit while still managing a large number of emails.
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    13
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    14
A typical limit for a single connection is 100 emails. If the server you
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    15
connect to imposes such a limit, it expects you to disconnect after that
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    16
number of emails has been sent. You could manage this manually within a loop,
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    17
but the AntiFlood plugin provides the necessary wrapper code so that you don't
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    18
need to worry about this logic.
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    19
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    20
Regardless of limits imposed by the server, it's usually a good idea to be
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    21
conservative with the resources of the SMTP server. Sending will become
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    22
sluggish if the server is being over-used so using the AntiFlood plugin will
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    23
not be a bad idea even if no limits exist.
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    24
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    25
The AntiFlood plugin's logic is basically to disconnect and the immediately
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    26
re-connect with the SMTP server every X number of emails sent, where X is a
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    27
number you specify to the plugin.
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    28
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    29
You can also specify a time period in seconds that Swift Mailer should pause
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    30
for between the disconnect/re-connect process. It's a good idea to pause for a
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    31
short time (say 30 seconds every 100 emails) simply to give the SMTP server a
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    32
chance to process its queue and recover some resources.
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    33
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    34
Using the AntiFlood Plugin
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    35
~~~~~~~~~~~~~~~~~~~~~~~~~~
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    36
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    37
The AntiFlood Plugin -- like all plugins -- is added with the Mailer
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    38
class' ``registerPlugin()`` method. It takes two constructor
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    39
parameters: the number of emails to pause after, and optionally the number of
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    40
seconds to pause for.
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    41
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    42
To use the AntiFlood plugin:
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    43
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    44
* Create an instance of the Mailer using any Transport you choose.
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    45
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    46
* Create an instance of the ``Swift_Plugins_AntiFloodPlugin`` class, passing
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    47
  in one or two constructor parameters.
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    48
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    49
* Register the plugin using the Mailer's ``registerPlugin()`` method.
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    50
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    51
* Continue using Swift Mailer to send messages as normal.
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    52
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    53
When Swift Mailer sends messages it will count the number of messages that
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    54
have been sent since the last re-connect. Once the number hits your specified
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    55
threshold it will disconnect and re-connect, optionally pausing for a
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    56
specified amount of time.
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    57
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    58
.. code-block:: php
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    59
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    60
    require_once 'lib/swift_required.php';
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    61
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    62
    //Create the Mailer using any Transport
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    63
    $mailer = Swift_Mailer::newInstance(
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    64
      Swift_SmtpTransport::newInstance('smtp.example.org', 25)
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    65
    );
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    66
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    67
    //Use AntiFlood to re-connect after 100 emails
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    68
    $mailer->registerPlugin(new Swift_Plugins_AntiFloodPlugin(100));
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    69
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    70
    //Or specify a time in seconds to pause for (30 secs)
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    71
    $mailer->registerPlugin(new Swift_Plugins_AntiFloodPlugin(100, 30));
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    72
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    73
    //Continue sending as normal
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    74
    for ($lotsOfRecipients as $recipient) {
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    75
      ...
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    76
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    77
      $mailer->send( ... );
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    78
    }
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    79
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    80
Throttler Plugin
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    81
----------------
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    82
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    83
If your SMTP server has restrictions in place to limit the rate at which you
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    84
send emails, then your code will need to be aware of this rate-limiting. The
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    85
Throttler plugin makes Swift Mailer run at a rate-limited speed.
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    86
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    87
Many shared hosts don't open their SMTP servers as a free-for-all. Usually
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    88
they have policies in place (probably to discourage spammers) that only allow
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    89
you to send a fixed number of emails per-hour/day.
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    90
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    91
The Throttler plugin supports two modes of rate-limiting and with each, you
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    92
will need to do that math to figure out the values you want. The plugin can
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    93
limit based on the number of emails per minute, or the number of
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    94
bytes-transferred per-minute.
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    95
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    96
Using the Throttler Plugin
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    97
~~~~~~~~~~~~~~~~~~~~~~~~~~
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    98
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    99
The Throttler Plugin -- like all plugins -- is added with the Mailer
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   100
class' ``registerPlugin()`` method. It has two required
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   101
constructor parameters that tell it how to do its rate-limiting.
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   102
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   103
To use the Throttler plugin:
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   104
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   105
* Create an instance of the Mailer using any Transport you choose.
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   106
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   107
* Create an instance of the ``Swift_Plugins_ThrottlerPlugin`` class, passing
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   108
  the number of emails, or bytes you wish to limit by, along with the mode
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   109
  you're using.
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   110
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   111
* Register the plugin using the Mailer's ``registerPlugin()`` method.
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   112
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   113
* Continue using Swift Mailer to send messages as normal.
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   114
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   115
When Swift Mailer sends messages it will keep track of the rate at which
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   116
sending messages is occuring. If it realises that sending is happening too
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   117
fast, it will cause your program to ``sleep()`` for enough time
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   118
to average out the rate.
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   119
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   120
.. code-block:: php
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   121
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   122
    require_once 'lib/swift_required.php';
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   123
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   124
    //Create the Mailer using any Transport
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   125
    $mailer = Swift_Mailer::newInstance(
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   126
      Swift_SmtpTransport::newInstance('smtp.example.org', 25)
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   127
    );
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   128
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   129
    //Rate limit to 100 emails per-minute
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   130
    $mailer->registerPlugin(new Swift_Plugins_ThrottlerPlugin(
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   131
      100, Swift_Plugins_ThrottlerPlugin::MESSAGES_PER_MINUTE
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   132
    ));
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   133
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   134
    //Rate limit to 10MB per-minute
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   135
    $mailer->registerPlugin(new Swift_Plugins_ThrottlerPlugin(
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   136
      1024 * 1024 * 10, Swift_Plugins_ThrottlerPlugin::BYTES_PER_MINUTE
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   137
    ));
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   138
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   139
    //Continue sending as normal
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   140
    for ($lotsOfRecipients as $recipient) {
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   141
      ...
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   142
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   143
      $mailer->send( ... );
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   144
    }
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   145
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   146
Logger Plugin
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   147
~~~~~~~~~~~~~
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   148
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   149
The Logger plugins helps with debugging during the process of sending. It can
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   150
help to identify why an SMTP server is rejecting addresses, or any other
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   151
hard-to-find problems that may arise.
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   152
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   153
The Logger plugin comes in two parts. There's the plugin itself, along with
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   154
one of a number of possible Loggers that you may choose to use. For example,
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   155
the logger may output messages directly in realtime, or it may capture
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   156
messages in an array.
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   157
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   158
One other notable feature is the way in which the Logger plugin changes
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   159
Exception messages. If Exceptions are being thrown but the error message does
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   160
not provide conclusive information as to the source of the problem (such as an
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   161
ambiguous SMTP error) the Logger plugin includes the entire SMTP transcript in
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   162
the error message so that debugging becomes a simpler task.
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   163
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   164
There are a few available Loggers included with Swift Mailer, but writing your
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   165
own implementation is incredibly simple and is achieved by creating a short
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   166
class that implements the ``Swift_Plugins_Logger`` interface.
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   167
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   168
* ``Swift_Plugins_Loggers_ArrayLogger``: Keeps a collection of log messages
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   169
  inside an array. The array content can be cleared or dumped out to the
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   170
  screen.
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   171
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   172
* ``Swift_Plugins_Loggers_EchoLogger``: Prints output to the screen in
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   173
  realtime. Handy for very rudimentary debug output.
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   174
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   175
Using the Logger Plugin
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   176
.......................
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   177
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   178
The Logger Plugin -- like all plugins -- is added with the Mailer
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   179
class' ``registerPlugin()`` method. It accepts an instance of
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   180
``Swift_Plugins_Logger`` in its constructor.
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   181
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   182
To use the Logger plugin:
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   183
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   184
* Create an instance of the Mailer using any Transport you choose.
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   185
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   186
* Create an instance of the a Logger implementation of
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   187
  ``Swift_Plugins_Logger``.
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   188
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   189
* Create an instance of the ``Swift_Plugins_LoggerPlugin`` class, passing the
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   190
  created Logger instance to its constructor.
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   191
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   192
* Register the plugin using the Mailer's ``registerPlugin()`` method.
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   193
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   194
* Continue using Swift Mailer to send messages as normal.
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   195
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   196
* Dump the contents of the log with the logger's ``dump()`` method.
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   197
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   198
When Swift Mailer sends messages it will keep a log of all the interactions
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   199
with the underlying Transport being used. Depending upon the Logger that has
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   200
been used the behaviour will differ, but all implementations offer a way to
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   201
get the contents of the log.
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   202
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   203
.. code-block:: php
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   204
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   205
    require_once 'lib/swift_required.php';
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   206
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   207
    //Create the Mailer using any Transport
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   208
    $mailer = Swift_Mailer::newInstance(
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   209
     Swift_SmtpTransport::newInstance('smtp.example.org', 25)
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   210
    );
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   211
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   212
    //To use the ArrayLogger
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   213
    $logger = new Swift_Plugins_Loggers_ArrayLogger();
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   214
    $mailer->registerPlugin(new Swift_Plugins_LoggerPlugin($logger));
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   215
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   216
    //Or to use the Echo Logger
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   217
    $logger = new Swift_Plugins_Loggers_EchoLogger();
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   218
    $mailer->registerPlugin(new Swift_Plugins_LoggerPlugin($logger));
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   219
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   220
    //Continue sending as normal
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   221
    for ($lotsOfRecipients as $recipient) {
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   222
     ...
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   223
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   224
     $mailer->send( ... );
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   225
    }
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   226
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   227
    // Dump the log contents
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   228
    // NOTE: The EchoLogger dumps in realtime so dump() does nothing for it
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   229
    echo $logger->dump();
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   230
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   231
Decorator Plugin
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   232
~~~~~~~~~~~~~~~~
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   233
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   234
Often there's a need to send the same message to multiple recipients, but with
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   235
tiny variations such as the recipient's name being used inside the message
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   236
body. The Decorator plugin aims to provide a solution for allowing these small
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   237
differences.
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   238
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   239
The decorator plugin works by intercepting the sending process of Swift
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   240
Mailer, reading the email address in the To: field and then looking up a set
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   241
of replacements for a template.
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   242
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   243
While the use of this plugin is simple, it is probably the most commonly
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   244
misunderstood plugin due to the way in which it works. The typical mistake
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   245
users make is to try registering the plugin multiple times (once for each
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   246
recipient) -- inside a loop for example. This is incorrect.
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   247
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   248
The Decorator plugin should be registered just once, but containing the list
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   249
of all recipients prior to sending. It will use this list of recipients to
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   250
find the required replacements during sending.
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   251
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   252
Using the Decorator Plugin
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   253
..........................
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   254
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   255
To use the Decorator plugin, simply create an associative array of
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   256
replacements based on email addresses and then use the mailer's
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   257
``registerPlugin()`` method to add the plugin.
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   258
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   259
First create an associative array of replacements based on the email addresses
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   260
you'll be sending the message to.
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   261
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   262
.. note::
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   263
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   264
    The replacements array becomes a 2-dimensional array whose keys are the
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   265
    email addresses and whose values are an associative array of replacements
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   266
    for that email address. The curly braces used in this example can be any
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   267
    type of syntax you choose, provided they match the placeholders in your
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   268
    email template.
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   269
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   270
    .. code-block:: php
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   271
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   272
        $replacements = array();
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   273
        foreach ($users as $user) {
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   274
          $replacements[$user['email']] = array(
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   275
            '{username}'=>$user['username'],
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   276
            '{password}'=>$user['password']
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   277
          );
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   278
        }
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   279
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   280
Now create an instance of the Decorator plugin using this array of
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   281
replacements and then register it with the Mailer. Do this only once!
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   282
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   283
.. code-block:: php
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   284
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   285
    $decorator = new Swift_Plugins_DecoratorPlugin($replacements);
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   286
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   287
    $mailer->registerPlugin($decorator);
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   288
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   289
When you create your message, replace elements in the body (and/or the subject
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   290
line) with your placeholders.
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   291
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   292
.. code-block:: php
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   293
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   294
    $message = Swift_Message::newInstance()
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   295
      ->setSubject('Important notice for {username}')
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   296
      ->setBody(
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   297
        "Hello {username}, we have reset your password to {password}\n" .
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   298
        "Please log in and change it at your earliest convenience."
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   299
      )
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   300
      ;
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   301
  
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   302
    foreach ($users as $user) {
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   303
      $message->addTo($user['email']);
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   304
    }
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   305
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   306
When you send this message to each of your recipients listed in your
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   307
``$replacements`` array they will receive a message customized
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   308
for just themselves. For example, the message used above when received may
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   309
appear like this to one user:
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   310
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   311
.. code-block:: text
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   312
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   313
    Subject: Important notice for smilingsunshine2009
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   314
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   315
    Hello smilingsunshine2009, we have reset your password to rainyDays
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   316
    Please log in and change it at your earliest convenience.
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   317
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   318
While another use may receive the message as:
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   319
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   320
.. code-block:: text
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   321
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   322
    Subject: Important notice for billy-bo-bob
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   323
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   324
    Hello billy-bo-bob, we have reset your password to dancingOctopus
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   325
    Please log in and change it at your earliest convenience.
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   326
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   327
While the decorator plugin provides a means to solve this problem, there are
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   328
various ways you could tackle this problem without the need for a plugin.
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   329
We're trying to come up with a better way ourselves and while we have several
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   330
(obvious) ideas we don't quite have the perfect solution to go ahead and
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   331
implement it. Watch this space.
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   332
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   333
Providing Your Own Replacements Lookup for the Decorator
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   334
........................................................
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   335
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   336
Filling an array with replacements may not be the best solution for providing
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   337
replacement information to the decorator. If you have a more elegant algorithm
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   338
that performs replacement lookups on-the-fly you may provide your own
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   339
implementation.
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   340
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   341
Providing your own replacements lookup implementation for the Decorator is
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   342
simply a matter of passing an instance of
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   343
``Swift_Plugins_Decorator_Replacements`` to the decorator
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   344
plugin's constructor, rather than passing in an array.
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   345
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   346
The Replacements interface is very simple to implement since it has just one
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   347
method: ``getReplacementsFor($address)``.
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   348
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   349
Imagine you want to look up replacements from a database on-the-fly, you might
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   350
provide an implementation that does this. You need to create a small class.
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   351
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   352
.. code-block:: php
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   353
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   354
    class DbReplacements implements Swift_Plugins_Decorator_Replacements {
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   355
      public function getReplacementsFor($address) {
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   356
        $sql = sprintf(
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   357
          "SELECT * FROM user WHERE email = '%s'",
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   358
          mysql_real_escape_string($address)
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   359
        );
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   360
    
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   361
        $result = mysql_query($sql);
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   362
    
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   363
        if ($row = mysql_fetch_assoc($result)) {
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   364
          return array(
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   365
            '{username}'=>$row['username'],
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   366
            '{password}'=>$row['password']
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   367
          );
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   368
        }
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   369
      }
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   370
    }
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   371
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   372
Now all you need to do is pass an instance of your class into the Decorator
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   373
plugin's constructor instead of passing an array.
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   374
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   375
.. code-block:: php
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   376
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   377
    $decorator = new Swift_Plugins_DecoratorPlugin(new DbReplacements());
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   378
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   379
    $mailer->registerPlugin($decorator);
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   380
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   381
For each message sent, the plugin will call your class'
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   382
``getReplacementsFor()`` method to find the array of replacements
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   383
it needs.
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   384
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   385
.. note::
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   386
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   387
    If your lookup algorithm is case sensitive, you should transform the
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   388
    ``$address`` argument as appropriate -- for example by passing it
7f95f8617b0b first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   389
    through ``strtolower()``.