PageRenderTime 49ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/source/manual/mail/sending.xml

https://bitbucket.org/solarphp/docs
XML | 295 lines | 257 code | 38 blank | 0 comment | 0 complexity | 60db551d96d6c2403eaeb3bf4c00b968 MD5 | raw file
Possible License(s): IPL-1.0, LGPL-2.0, Apache-2.0, 0BSD
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <sect1
  3. xml:id="mail.sending"
  4. xmlns="http://docbook.org/ns/docbook"
  5. xmlns:xlink="http://www.w3.org/1999/xlink"
  6. >
  7. <title>Sending Messages</title>
  8. <para>
  9. Now that we have a message to send, let's actually get it out there. To send the message,
  10. we need to pick what kind of "transport" we're going to use, then send the message through
  11. that transport.
  12. </para>
  13. <sect2 xml:id="mail.sending.transport">
  14. <title>Using a Transport</title>
  15. <para>
  16. Solar comes with two <literal>Solar_Mail_Transport</literal> adapters: the Phpmail one, which uses
  17. <link xlink:href="http://php.net/manual/en/function.mail.php">mail()</link>
  18. internally, and an SMTP factory-adapter one, which takes a little more setup but is a
  19. lot more robust.
  20. </para>
  21. <sect3 xml:id="mail.sending.transport.php-mail">
  22. <title>The PHP mail() Transport</title>
  23. <para>
  24. Easy one first: the mail()-based adapter.
  25. </para>
  26. <programlisting language="php"><![CDATA[
  27. <?php
  28. $mail = Solar::factory('Solar_Mail_Message');
  29. // .. build the message ...
  30. // build a transport and send the message
  31. $transport = Solar::factory('Solar_Mail_Transport', array(
  32. 'adapter' => 'Solar_Mail_Transport_Adapter_Phpmail'
  33. ));
  34. $transport->send($mail);
  35. ?>
  36. ]]></programlisting>
  37. </sect3>
  38. <sect3 xml:id="mail.sending.transport.solar-smtp">
  39. <title>The Solar_Smtp Transport</title>
  40. <para>
  41. The SMTP factory-adapter is a little more complicated, but still not too hard.
  42. We need to build an <link xlink:href="http://solarphp.com/apidoc/class.Solar_Smtp.Overview">SMTP connection object</link>,
  43. then tell the SMTP transport adapter to use it, then send the message.
  44. </para>
  45. <programlisting language="php"><![CDATA[
  46. <?php
  47. $mail = Solar::factory('Solar_Mail_Message');
  48. // ...
  49. // build an SMTP connection object
  50. $smtp = Solar::factory('Solar_Smtp', array(
  51. 'adapter' => 'Solar_Smtp_Adapter_NoAuth',
  52. 'host' => 'mail.example.com',
  53. ));
  54. // build a transport and send the message
  55. $transport = Solar::factory('Solar_Mail_Transport', array(
  56. 'adapter' => 'Solar_Mail_Transport_Adapter_Smtp',
  57. 'smtp' => $smtp,
  58. ));
  59. $transport->send($mail);
  60. ?>
  61. ]]></programlisting>
  62. <para>
  63. The SMTP connection object is itself a factory, and has adapters for various kinds of authentication:
  64. <itemizedlist>
  65. <listitem><para><link xlink:href="http://solarphp.com/apidoc/class.Solar_Smtp_Adapter_NoAuth.Overview">none (the default)</link></para></listitem>
  66. <listitem><para><link xlink:href="http://solarphp.com/apidoc/class.Solar_Smtp_Adapter_PlainAuth.Overview">plain</link></para></listitem>
  67. <listitem><para><link xlink:href="http://solarphp.com/apidoc/class.Solar_Smtp_Adapter_LoginAuth.Overview">login</link></para></listitem>
  68. <listitem><para><link xlink:href="http://solarphp.com/apidoc/class.Solar_Smtp_Adapter_CramMd5Auth.Overview">CRAM-MD5</link></para></listitem>
  69. </itemizedlist>
  70. </para>
  71. </sect3>
  72. </sect2>
  73. <sect2 xml:id="mail.sending.transport-di">
  74. <title>Transport Dependency-Injection</title>
  75. <para>
  76. Instead of building a transport every time you send an email, you can configure a
  77. transport for the mail message as a dependency injection. Then the mail message will
  78. be able to "send itself" using that injected transport. For <literal>Solar_Mail_Message</literal>,
  79. the config key for the transport injection is transport.
  80. </para>
  81. <sect3 xml:id="mail.sending.transport-di.config-array">
  82. <title>Injecting From Config Array</title>
  83. <para>
  84. The easiest way of dependency injection is to pass a config array for the transport.
  85. Here's an example of injecting the PHP <link xlink:href="http://php.net/manual/en/function.mail.php">mail()</link>
  86. adapter into the mail message.
  87. </para>
  88. <programlisting language="php"><![CDATA[
  89. <?php
  90. // create a message object, and inject a transport config
  91. $mail = Solar::factory('Solar_Mail_Message', array(
  92. 'transport' => array(
  93. 'adapter' => 'Solar_Mail_Transport_Adapter_Phpmail',
  94. )
  95. ));
  96. // ... compose the message ...
  97. // now tell the email to "send itself".
  98. // this uses the injected transport.
  99. $mail->send();
  100. ?>
  101. ]]></programlisting>
  102. <para>
  103. Here's a more complex example, using the <literal>Solar_Smtp</literal> factory-adapter transport with plain
  104. authentication. Note that the transport itself uses a dependency injection for the
  105. <literal>Solar_Smtp</literal> object under the 'smtp' key.
  106. </para>
  107. <programlisting language="php"><![CDATA[
  108. <?php
  109. // create a message object, and inject a transport config
  110. $mail = Solar::factory('Solar_Mail_Message', array(
  111. 'transport' => array(
  112. 'adapter' => 'Solar_Mail_Transport_Adapter_Smtp',
  113. 'smtp' => array(
  114. 'adapter' => 'Solar_Smtp_Adapter_PlainAuth',
  115. 'username' => 'pmjones',
  116. 'password' => 'secret',
  117. 'host' => 'mail.example.com',
  118. ),
  119. )
  120. ));
  121. // ... compose the message ...
  122. // now tell the email to "send itself".
  123. // this uses the injected transport.
  124. $mail->send();
  125. ?>
  126. ]]></programlisting>
  127. <para>
  128. This is quite convenient, especially since you can set up those arrays in your config
  129. file in advance, making them the default configs each time you create a <literal>Solar_Mail_Message</literal>
  130. object. In that case, you can use the class names instead of the full configuration arrays.
  131. </para>
  132. <programlisting language="php"><![CDATA[
  133. <?php
  134. $config['Solar_Mail_Message'] = array(
  135. 'transport' => 'Solar_Mail_Transport_Adapter_Smtp',
  136. );
  137. $config['Solar_Mail_Transport_Adapter_Smtp'] = array(
  138. 'transport' => 'Solar_Smtp_Adapter_PlainAuth',
  139. );
  140. $config['Solar_Smtp_Adapter_PlainAuth'] = array(
  141. 'adapter' => 'Solar_Smtp_Adapter_PlainAuth',
  142. 'username' => 'pmjones',
  143. 'password' => 'secret',
  144. 'host' => 'mail.example.com',
  145. );
  146. ?>
  147. ]]></programlisting>
  148. <para>
  149. Using the config-file-centered process instead of the logic-centered process means
  150. that you can have all your settings in one place for use throughout your application.
  151. You can even change adapters from the config file without changing any of the
  152. mail-sending code, as long as you keep the same registry names.
  153. </para>
  154. </sect3>
  155. <sect3 xml:id="mail.sending.transport-di.registry">
  156. <title>Injecting From the Registry</title>
  157. <para>
  158. The only problem with injection using a config array is that the dependency objects
  159. get created anew each time you create a <literal>Solar_Mail_Message</literal>.
  160. Sometimes it's better to use registry objects for the dependency, so you don't use
  161. up resources re-creating identical objects over and over again.
  162. </para>
  163. <para>
  164. You can register the transport and SMTP objects in your bootstrap file or
  165. <link xlink:href="http://solarphp.com/apidoc/class.Solar_Controller_Page._setup"><literal>Solar_Controller_Page::_setup()</literal></link>
  166. so they are available throughout the application:
  167. </para>
  168. <programlisting language="php"><![CDATA[
  169. <?php
  170. // build a Solar_Smtp object
  171. $smtp = Solar::factory('Solar_Smtp', array(
  172. 'adapter' => 'Solar_Smtp_Adapter_PlainAuth',
  173. 'username' => 'pmjones',
  174. 'password' => 'secret',
  175. 'host' => 'mail.example.com',
  176. ));
  177. // register it as 'smtp'
  178. Solar_Registry::set('smtp', $smtp);
  179. // build a Solar_Mail_Transport object with the SMTP object injected
  180. $transport = Solar::factory('Solar_Mail_Transport', array(
  181. 'adapter' => 'Solar_Mail_Transport_Adapter_Smtp',
  182. 'smtp' => 'smtp', // uses the registered 'smtp' object
  183. );
  184. // register the transport as 'mail-transport'
  185. Solar_Registry::set('mail-transport', $transport);
  186. ?>
  187. ]]></programlisting>
  188. <para>
  189. Now when you create a new mail message, you can tell it that there's a transport
  190. already available, and call <link xlink:href="http://solarphp.com/apidoc/class.Solar_Mail_Message.send">send()</link>
  191. directly on the message.
  192. </para>
  193. <programlisting language="php"><![CDATA[
  194. <?php
  195. $mail = Solar::factory('Solar_Mail_Message', array(
  196. 'transport' => 'mail-transport',
  197. ));
  198. // ... build the message, and then:
  199. $mail->send();
  200. ?>
  201. ]]></programlisting>
  202. <para>
  203. If you want to, you can put it all of this config information in the Solar config file.
  204. Then you can lazy-load the objects from the registry, and they will automatically have
  205. all the right settings. The various dependency objects will be lazy-loaded automatically for you.
  206. </para>
  207. <para>
  208. In your Solar.config.php file:
  209. </para>
  210. <programlisting language="php"><![CDATA[
  211. <?php
  212. // configure SMTP
  213. $config['Solar_Smtp'] = array(
  214. 'adapter' => 'Solar_Smtp_Adapter_PlainAuth',
  215. 'username' => 'pmjones',
  216. 'password' => 'secret',
  217. 'host' => 'mail.example.com',
  218. );
  219. // configure mail transport
  220. $config['Solar_Mail_Transport'] = array(
  221. 'adapter' => 'Solar_Mail_Transport_Adapter_Smtp',
  222. 'smtp' => 'smtp',
  223. );
  224. // tell all mail messages to use the registered 'mail-transport'
  225. $config['Solar_Mail_Message']['transport'] = 'mail-transport';
  226. ?>
  227. ]]></programlisting>
  228. <para>
  229. Then all you have to do in your setup logic is register class names instead of objects ...
  230. </para>
  231. <programlisting language="php"><![CDATA[
  232. <?php
  233. Solar_Registry::set('smtp', 'Solar_Smtp');
  234. Solar_Registry::set('mail-transport', 'Solar_Mail_Transport');
  235. ?>
  236. ]]></programlisting>
  237. <para>
  238. ... which will cause those objects to be created only at the moment they are first
  239. called as depndencies, and the same object will be reused every time you pull it from the registry.
  240. </para>
  241. <para>
  242. Now you can send an email message very simply:
  243. </para>
  244. <programlisting language="php"><![CDATA[
  245. <?php
  246. // create and build a message
  247. $mail = Solar::factory('Solar_Mail_Message');
  248. // ...
  249. // send the message; this creates the SMTP and mail-transport objects
  250. // from the config-file settings, and sends the message trough the
  251. // registered transport.
  252. $mail->send();
  253. ?>
  254. ]]></programlisting>
  255. </sect3>
  256. </sect2>
  257. </sect1>