messages.rst 37KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977
  1. Creating Messages
  2. =================
  3. Creating messages in Swift Mailer is done by making use of the various MIME
  4. entities provided with the library. Complex messages can be quickly created
  5. with very little effort.
  6. Quick Reference for Creating a Message
  7. ---------------------------------------
  8. You can think of creating a Message as being similar to the steps you perform
  9. when you click the Compose button in your mail client. You give it a subject,
  10. specify some recipients, add any attachments and write your message.
  11. To create a Message:
  12. * Call the ``newInstance()`` method of ``Swift_Message``.
  13. * Set your sender address (``From:``) with ``setFrom()`` or ``setSender()``.
  14. * Set a subject line with ``setSubject()``.
  15. * Set recipients with ``setTo()``, ``setCc()`` and/or ``setBcc()``.
  16. * Set a body with ``setBody()``.
  17. * Add attachments with ``attach()``.
  18. .. code-block:: php
  19. require_once 'lib/swift_required.php';
  20. //Create the message
  21. $message = Swift_Message::newInstance()
  22. //Give the message a subject
  23. ->setSubject('Your subject')
  24. //Set the From address with an associative array
  25. ->setFrom(array('john@doe.com' => 'John Doe'))
  26. //Set the To addresses with an associative array
  27. ->setTo(array('receiver@domain.org', 'other@domain.org' => 'A name'))
  28. //Give it a body
  29. ->setBody('Here is the message itself')
  30. //And optionally an alternative body
  31. ->addPart('<q>Here is the message itself</q>', 'text/html')
  32. //Optionally add any attachments
  33. ->attach(Swift_Attachment::fromPath('my-document.pdf'))
  34. ;
  35. Message Basics
  36. --------------
  37. A message is a container for anything you want to send to somebody else. There
  38. are several basic aspects of a message that you should know.
  39. An e-mail message is made up of several relatively simple entities that are
  40. combined in different ways to achieve different results. All of these entities
  41. have the same fundamental outline but serve a different purpose. The Message
  42. itself can be defined as a MIME entity, an Attachment is a MIME entity, all
  43. MIME parts are MIME entities -- and so on!
  44. The basic units of each MIME entity -- be it the Message itself, or an
  45. Attachment -- are its Headers and its body:
  46. .. code-block:: text
  47. Header-Name: A header value
  48. Other-Header: Another value
  49. The body content itself
  50. The Headers of a MIME entity, and its body must conform to some strict
  51. standards defined by various RFC documents. Swift Mailer ensures that these
  52. specifications are followed by using various types of object, including
  53. Encoders and different Header types to generate the entity.
  54. The Structure of a Message
  55. ~~~~~~~~~~~~~~~~~~~~~~~~~~
  56. Of all of the MIME entities, a message -- ``Swift_Message``
  57. is the largest and most complex. It has many properties that can be updated
  58. and it can contain other MIME entities -- attachments for example --
  59. nested inside it.
  60. A Message has a lot of different Headers which are there to present
  61. information about the message to the recipients' mail client. Most of these
  62. headers will be familiar to the majority of users, but we'll list the basic
  63. ones. Although it's possible to work directly with the Headers of a Message
  64. (or other MIME entity), the standard Headers have accessor methods provided to
  65. abstract away the complex details for you. For example, although the Date on a
  66. message is written with a strict format, you only need to pass a UNIX
  67. timestamp to ``setDate()``.
  68. +-------------------------------+------------------------------------------------------------------------------------------------------------------------------------+---------------------------------------------+
  69. | Header | Description | Accessors |
  70. +===============================+====================================================================================================================================+=============================================+
  71. | ``Message-ID`` | Identifies this message with a unique ID, usually containing the domain name and time generated | ``getId()`` / ``setId()`` |
  72. +-------------------------------+------------------------------------------------------------------------------------------------------------------------------------+---------------------------------------------+
  73. | ``Return-Path`` | Specifies where bounces should go (Swift Mailer reads this for other uses) | ``getReturnPath()`` / ``setReturnPath()`` |
  74. +-------------------------------+------------------------------------------------------------------------------------------------------------------------------------+---------------------------------------------+
  75. | ``From`` | Specifies the address of the person who the message is from. This can be multiple addresses if multiple people wrote the message. | ``getFrom()`` / ``setFrom()`` |
  76. +-------------------------------+------------------------------------------------------------------------------------------------------------------------------------+---------------------------------------------+
  77. | ``Sender`` | Specifies the address of the person who physically sent the message (higher precedence than ``From:``) | ``getSender()`` / ``setSender()`` |
  78. +-------------------------------+------------------------------------------------------------------------------------------------------------------------------------+---------------------------------------------+
  79. | ``To`` | Specifies the addresses of the intended recipients | ``getTo()`` / ``setTo()`` |
  80. +-------------------------------+------------------------------------------------------------------------------------------------------------------------------------+---------------------------------------------+
  81. | ``Cc`` | Specifies the addresses of recipients who will be copied in on the message | ``getCc()`` / ``setCc()`` |
  82. +-------------------------------+------------------------------------------------------------------------------------------------------------------------------------+---------------------------------------------+
  83. | ``Bcc`` | Specifies the addresses of recipients who the message will be blind-copied to. Other recipients will not be aware of these copies. | ``getBcc()`` / ``setBcc()`` |
  84. +-------------------------------+------------------------------------------------------------------------------------------------------------------------------------+---------------------------------------------+
  85. | ``Reply-To`` | Specifies the address where replies are sent to | ``getReplyTo()`` / ``setReplyTo()`` |
  86. +-------------------------------+------------------------------------------------------------------------------------------------------------------------------------+---------------------------------------------+
  87. | ``Subject`` | Specifies the subject line that is displayed in the recipients' mail client | ``getSubject()`` / ``setSubject()`` |
  88. +-------------------------------+------------------------------------------------------------------------------------------------------------------------------------+---------------------------------------------+
  89. | ``Date`` | Specifies the date at which the message was sent | ``getDate()`` / ``setDate()`` |
  90. +-------------------------------+------------------------------------------------------------------------------------------------------------------------------------+---------------------------------------------+
  91. | ``Content-Type`` | Specifies the format of the message (usually text/plain or text/html) | ``getContentType()`` / ``setContentType()`` |
  92. +-------------------------------+------------------------------------------------------------------------------------------------------------------------------------+---------------------------------------------+
  93. | ``Content-Transfer-Encoding`` | Specifies the encoding scheme in the message | ``getEncoder()`` / ``setEncoder()`` |
  94. +-------------------------------+------------------------------------------------------------------------------------------------------------------------------------+---------------------------------------------+
  95. Working with a Message Object
  96. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  97. Although there are a lot of available methods on a message object, you only
  98. need to make use of a small subset of them. Usually you'll use
  99. ``setSubject()``, ``setTo()`` and
  100. ``setFrom()`` before setting the body of your message with
  101. ``setBody()``.
  102. Calling methods is simple. You just call them like functions, but using the
  103. object operator "``<![CDATA[->]]>``" to do so. If you've created
  104. a message object and called it ``$message`` then you'd set a
  105. subject on it like so:
  106. .. code-block:: php
  107. require_once 'lib/swift_required.php';
  108. $message = Swift_Message::newInstance();
  109. $message->setSubject('My subject');
  110. All MIME entities (including a message) have a ``toString()``
  111. method that you can call if you want to take a look at what is going to be
  112. sent. For example, if you ``<![CDATA[echo
  113. $message->toString();]]>`` you would see something like this:
  114. .. code-block:: bash
  115. Message-ID: <1230173678.4952f5eeb1432@swift.generated>
  116. Date: Thu, 25 Dec 2008 13:54:38 +1100
  117. Subject: Example subject
  118. From: Chris Corbyn <chris@w3style.co.uk>
  119. To: Receiver Name <recipient@example.org>
  120. MIME-Version: 1.0
  121. Content-Type: text/plain; charset=utf-8
  122. Content-Transfer-Encoding: quoted-printable
  123. Here is the message
  124. We'll take a closer look at the methods you use to create your message in the
  125. following sections.
  126. Adding Content to Your Message
  127. ------------------------------
  128. Rich content can be added to messages in Swift Mailer with relative ease by
  129. calling methods such as setSubject(), setBody(), addPart() and attach().
  130. Setting the Subject Line
  131. ~~~~~~~~~~~~~~~~~~~~~~~~
  132. The subject line, displayed in the recipients' mail client can be set with the
  133. setSubject() method, or as a parameter to Swift_Message::newInstance().
  134. To set the subject of your Message:
  135. * Call the ``setSubject()`` method of the Message, or specify it at the time
  136. you create the message.
  137. .. code-block:: php
  138. // Pass it as a parameter when you create the message
  139. $message = Swift_Message::newInstance('My amazing subject');
  140. // Or set it after like this
  141. $message->setSubject('My amazing subject');
  142. Setting the Body Content
  143. ~~~~~~~~~~~~~~~~~~~~~~~~
  144. The body of the message -- seen when the user opens the message --
  145. is specified by calling the setBody() method. If an alternative body is to be
  146. included addPart() can be used.
  147. The body of a message is the main part that is read by the user. Often people
  148. want to send a message in HTML format (``text/html``), other
  149. times people want to send in plain text (``text/plain``), or
  150. sometimes people want to send both versions and allow the recipient to chose
  151. how they view the message.
  152. As a rule of thumb, if you're going to send a HTML email, always include a
  153. plain-text equivalent of the same content so that users who prefer to read
  154. plain text can do so.
  155. To set the body of your Message:
  156. * Call the ``setBody()`` method of the Message, or specify it at the time you
  157. create the message.
  158. * Add any alternative bodies with ``addPart()``.
  159. If the recipient's mail client offers preferences for displaying text vs. HTML then
  160. the mail client will present that part to the user where available. In other cases
  161. the mail client will display the "best" part it can - usually HTML if you've included
  162. HTML.
  163. .. code-block:: php
  164. //Pass it as a parameter when you create the message
  165. $message = Swift_Message::newInstance('Subject here', 'My amazing body');
  166. //Or set it after like this
  167. $message->setBody('My <em>amazing</em> body', 'text/html');
  168. //Add alternative parts with addPart()
  169. $message->addPart('My amazing body in plain text', 'text/plain');
  170. Attaching Files
  171. ---------------
  172. Attachments are downloadable parts of a message and can be added by calling
  173. the attach() method on the message. You can add attachments that exist on
  174. disk, or you can create attachments on-the-fly.
  175. Attachments are actually an interesting area of Swift Mailer and something
  176. that could put a lot of power at your fingertips if you grasp the concept
  177. behind the way a message is held together.
  178. Although we refer to files sent over e-mails as "attachments" -- because
  179. they're attached to the message -- lots of other parts of the message are
  180. actually "attached" even if we don't refer to these parts as attachments.
  181. File attachments are created by the ``Swift_Attachment`` class
  182. and then attached to the message via the ``attach()`` method on
  183. it. For all of the "every day" MIME types such as all image formats, word
  184. documents, PDFs and spreadsheets you don't need to explicitly set the
  185. content-type of the attachment, though it would do no harm to do so. For less
  186. common formats you should set the content-type -- which we'll cover in a
  187. moment.
  188. Attaching Existing Files
  189. ~~~~~~~~~~~~~~~~~~~~~~~~
  190. Files that already exist, either on disk or at a URL can be attached to a
  191. message with just one line of code, using Swift_Attachment::fromPath().
  192. You can attach files that exist locally, or if your PHP installation has
  193. ``allow_url_fopen`` turned on you can attach files from other
  194. websites.
  195. To attach an existing file:
  196. * Create an attachment with ``Swift_Attachment::fromPath()``.
  197. * Add the attachment to the message with ``attach()``.
  198. The attachment will be presented to the recipient as a downloadable file with
  199. the same filename as the one you attached.
  200. .. code-block:: php
  201. //Create the attachment
  202. // * Note that you can technically leave the content-type parameter out
  203. $attachment = Swift_Attachment::fromPath('/path/to/image.jpg', 'image/jpeg');
  204. //Attach it to the message
  205. $message->attach($attachment);
  206. //The two statements above could be written in one line instead
  207. $message->attach(Swift_Attachment::fromPath('/path/to/image.jpg'));
  208. //You can attach files from a URL if allow_url_fopen is on in php.ini
  209. $message->attach(Swift_Attachment::fromPath('http://site.tld/logo.png'));
  210. Setting the Filename
  211. ~~~~~~~~~~~~~~~~~~~~
  212. Usually you don't need to explicitly set the filename of an attachment because
  213. the name of the attached file will be used by default, but if you want to set
  214. the filename you use the setFilename() method of the Attachment.
  215. To change the filename of an attachment:
  216. * Call its ``setFilename()`` method.
  217. The attachment will be attached in the normal way, but meta-data sent inside
  218. the email will rename the file to something else.
  219. .. code-block:: php
  220. //Create the attachment and call its setFilename() method
  221. $attachment = Swift_Attachment::fromPath('/path/to/image.jpg')
  222. ->setFilename('cool.jpg');
  223. //Because there's a fluid interface, you can do this in one statement
  224. $message->attach(
  225. Swift_Attachment::fromPath('/path/to/image.jpg')->setFilename('cool.jpg')
  226. );
  227. Attaching Dynamic Content
  228. ~~~~~~~~~~~~~~~~~~~~~~~~~
  229. Files that are generated at runtime, such as PDF documents or images created
  230. via GD can be attached directly to a message without writing them out to disk.
  231. Use the standard Swift_Attachment::newInstance() method.
  232. To attach dynamically created content:
  233. * Create your content as you normally would.
  234. * Create an attachment with ``Swift_Attachment::newInstance()``, specifying
  235. the source data of your content along with a name and the content-type.
  236. * Add the attachment to the message with ``attach()``.
  237. The attachment will be presented to the recipient as a downloadable file
  238. with the filename and content-type you specify.
  239. .. note::
  240. If you would usually write the file to disk anyway you should just attach
  241. it with ``Swift_Attachment::fromPath()`` since this will use less memory:
  242. .. code-block: php
  243. //Create your file contents in the normal way, but don't write them to disk
  244. $data = create_my_pdf_data();
  245. //Create the attachment with your data
  246. $attachment = Swift_Attachment::newInstance($data, 'my-file.pdf', 'application/pdf');
  247. //Attach it to the message
  248. $message->attach($attachment);
  249. //You can alternatively use method chaining to build the attachment
  250. $attachment = Swift_Attachment::newInstance()
  251. ->setFilename('my-file.pdf')
  252. ->setContentType('application/pdf')
  253. ->setBody($data)
  254. ;
  255. Changing the Disposition
  256. ~~~~~~~~~~~~~~~~~~~~~~~~
  257. Attachments just appear as files that can be saved to the Desktop if desired.
  258. You can make attachment appear inline where possible by using the
  259. setDisposition() method of an attachment.
  260. To make an attachment appear inline:
  261. * Call its ``setDisposition()`` method.
  262. The attachment will be displayed within the email viewing window if the mail
  263. client knows how to display it.
  264. .. note::
  265. If you try to create an inline attachment for a non-displayable file type
  266. such as a ZIP file, the mail client should just present the attachment as
  267. normal:
  268. .. code-block:: php
  269. //Create the attachment and call its setDisposition() method
  270. $attachment = Swift_Attachment::fromPath('/path/to/image.jpg')
  271. ->setDisposition('inline');
  272. //Because there's a fluid interface, you can do this in one statement
  273. $message->attach(
  274. Swift_Attachment::fromPath('/path/to/image.jpg')->setDisposition('inline')
  275. );
  276. Embedding Inline Media Files
  277. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  278. Often people want to include an image or other content inline with a HTML
  279. message. It's easy to do this with HTML linking to remote resources, but this
  280. approach is usually blocked by mail clients. Swift Mailer allows you to embed
  281. your media directly into the message.
  282. Mail clients usually block downloads from remote resources because this
  283. technique was often abused as a mean of tracking who opened an email. If
  284. you're sending a HTML email and you want to include an image in the message
  285. another approach you can take is to embed the image directly.
  286. Swift Mailer makes embedding files into messages extremely streamlined. You
  287. embed a file by calling the ``embed()`` method of the message,
  288. which returns a value you can use in a ``src`` or
  289. ``href`` attribute in your HTML.
  290. Just like with attachments, it's possible to embed dynamically generated
  291. content without having an existing file available.
  292. The embedded files are sent in the email as a special type of attachment that
  293. has a unique ID used to reference them within your HTML attributes. On mail
  294. clients that do not support embedded files they may appear as attachments.
  295. Although this is commonly done for images, in theory it will work for any
  296. displayable (or playable) media type. Support for other media types (such as
  297. video) is dependent on the mail client however.
  298. Embedding Existing Files
  299. ........................
  300. Files that already exist, either on disk or at a URL can be embedded in a
  301. message with just one line of code, using Swift_EmbeddedFile::fromPath().
  302. You can embed files that exist locally, or if your PHP installation has
  303. ``allow_url_fopen`` turned on you can embed files from other
  304. websites.
  305. To embed an existing file:
  306. * Create a message object with ``Swift_Message::newInstance()``.
  307. * Set the body as HTML, and embed a file at the correct point in the message with ``embed()``.
  308. The file will be displayed with the message inline with the HTML wherever its ID
  309. is used as a ``src`` attribute.
  310. .. note::
  311. ``Swift_Image`` and ``Swift_EmbeddedFile`` are just aliases of one
  312. another. ``Swift_Image`` exists for semantic purposes.
  313. .. note::
  314. You can embed files in two stages if you prefer. Just capture the return
  315. value of ``embed()`` in a variable and use that as the ``src`` attribute.
  316. .. code-block: php
  317. //Create the message
  318. $message = Swift_Message::newInstance('My subject');
  319. //Set the body
  320. $message->setBody(
  321. '<html>' .
  322. ' <head></head>' .
  323. ' <body>' .
  324. ' Here is an image <img src="' . //Embed the file
  325. $message->embed(Swift_Image::fromPath('image.png')) .
  326. '" alt="Image" />' .
  327. ' Rest of message' .
  328. ' </body>' .
  329. '</html>',
  330. 'text/html' //Mark the content-type as HTML
  331. );
  332. //You can embed files from a URL if allow_url_fopen is on in php.ini
  333. $message->setBody(
  334. '<html>' .
  335. ' <head></head>' .
  336. ' <body>' .
  337. ' Here is an image <img src="' .
  338. $message->embed(Swift_Image::fromPath('http://site.tld/logo.png')) .
  339. '" alt="Image" />' .
  340. ' Rest of message' .
  341. ' </body>' .
  342. '</html>',
  343. 'text/html'
  344. );
  345. // If placing the embed() code inline becomes cumbersome
  346. // it's easy to do this in two steps
  347. $cid = $message->embed(Swift_Image::fromPath('image.png'));
  348. $message->setBody(
  349. '<html>' .
  350. ' <head></head>' .
  351. ' <body>' .
  352. ' Here is an image <img src="' . $cid . '" alt="Image" />' .
  353. ' Rest of message' .
  354. ' </body>' .
  355. '</html>',
  356. 'text/html' //Mark the content-type as HTML
  357. );
  358. Embedding Dynamic Content
  359. .........................
  360. Images that are generated at runtime, such as images created via GD can be
  361. embedded directly to a message without writing them out to disk. Use the
  362. standard Swift_Image::newInstance() method.
  363. To embed dynamically created content:
  364. * Create a message object with ``Swift_Message::newInstance()``.
  365. * Set the body as HTML, and embed a file at the correct point in the message
  366. with ``embed()``. You will need to specify a filename and a content-type.
  367. The file will be displayed with the message inline with the HTML wherever its ID
  368. is used as a ``src`` attribute.
  369. .. note::
  370. ``Swift_Image`` and ``Swift_EmbeddedFile`` are just aliases of one
  371. another. ``Swift_Image`` exists for semantic purposes.
  372. .. note::
  373. You can embed files in two stages if you prefer. Just capture the return
  374. value of ``embed()`` in a variable and use that as the ``src`` attribute.
  375. .. code-block:: php
  376. //Create your file contents in the normal way, but don't write them to disk
  377. $img_data = create_my_image_data();
  378. //Create the message
  379. $message = Swift_Message::newInstance('My subject');
  380. //Set the body
  381. $message->setBody(
  382. '<html>' .
  383. ' <head></head>' .
  384. ' <body>' .
  385. ' Here is an image <img src="' . //Embed the file
  386. $message->embed(Swift_Image::newInstance($img_data, 'image.jpg', 'image/jpeg')) .
  387. '" alt="Image" />' .
  388. ' Rest of message' .
  389. ' </body>' .
  390. '</html>',
  391. 'text/html' //Mark the content-type as HTML
  392. );
  393. // If placing the embed() code inline becomes cumbersome
  394. // it's easy to do this in two steps
  395. $cid = $message->embed(Swift_Image::newInstance($img_data, 'image.jpg', 'image/jpeg'));
  396. $message->setBody(
  397. '<html>' .
  398. ' <head></head>' .
  399. ' <body>' .
  400. ' Here is an image <img src="' . $cid . '" alt="Image" />' .
  401. ' Rest of message' .
  402. ' </body>' .
  403. '</html>',
  404. 'text/html' //Mark the content-type as HTML
  405. );
  406. Adding Recipients to Your Message
  407. ---------------------------------
  408. Recipients are specified within the message itself via setTo(), setCc() and
  409. setBcc(). Swift Mailer reads these recipients from the message when it gets
  410. sent so that it knows where to send the message to.
  411. Message recipients are one of three types:
  412. * ``To:`` recipients -- the primary recipients (required)
  413. * ``Cc:`` recipients -- receive a copy of the message (optional)
  414. * ``Bcc:`` recipients -- hidden from other recipients (optional)
  415. Each type can contain one, or several addresses. It's possible to list only
  416. the addresses of the recipients, or you can personalize the address by
  417. providing the real name of the recipient.
  418. .. sidebar:: Syntax for Addresses
  419. If you only wish to refer to a single email address (for example your ``From:``
  420. address) then you can just use a string.
  421. .. code-block:: php
  422. $message->setFrom('some@address.tld');
  423. If you want to include a name then you must use an associative array.
  424. .. code-block:: php
  425. $message->setFrom(array('some@address.tld' => 'The Name'));
  426. If you want to include multiple addresses then you must use an array.
  427. .. code-block:: php
  428. $message->setTo(array('some@address.tld', 'other@address.tld'));
  429. You can mix personalized (addresses with a name) and non-personalized
  430. addresses in the same list by mixing the use of associative and non-associative
  431. array syntax.
  432. .. code-block:: php
  433. $message->setTo(array(
  434. 'recipient-with-name@example.org' => 'Recipient Name One',
  435. 'no-name@example.org', //Note that this is not a key-value pair
  436. 'named-recipient@example.org' => 'Recipient Name Two'
  437. ));
  438. Setting ``To:`` Recipients
  439. ~~~~~~~~~~~~~~~~~~~~~~~~~~
  440. ``To:`` recipients are required in a message and are set with the
  441. ``setTo()`` or ``addTo()`` methods of the message.
  442. To set ``To:`` recipients, create the message object using either
  443. ``new Swift_Message( ... )`` or
  444. ``Swift_Message::newInstance( ... )``, then call the
  445. ``setTo()`` method with a complete array of addresses, or use the
  446. ``addTo()`` method to iteratively add recipients.
  447. The ``setTo()`` method accepts input in various formats as described earlier in
  448. this chapter. The ``addTo()`` method takes either one or two parameters. The
  449. first being the email address and the second optional parameter being the name
  450. of the recipient.
  451. ``To:`` recipients are visible in the message headers and will be
  452. seen by the other recipients.
  453. .. note::
  454. Multiple calls to ``setTo()`` will not add new recipients -- each
  455. call overrides the previous calls. If you want to iteratively add
  456. recipients, use the ``addTo()`` method.
  457. .. code-block:: php
  458. //Using setTo() to set all recipients in one go
  459. $message->setTo(array(
  460. 'person1@example.org',
  461. 'person2@otherdomain.org' => 'Person 2 Name',
  462. 'person3@example.org',
  463. 'person4@example.org',
  464. 'person5@example.org' => 'Person 5 Name'
  465. ));
  466. //Using addTo() to add recipients iteratively
  467. $message->addTo('person1@example.org');
  468. $message->addTo('person2@example.org', 'Person 2 Name');
  469. Setting ``Cc:`` Recipients
  470. ~~~~~~~~~~~~~~~~~~~~~~~~~~
  471. ``Cc:`` recipients are set with the
  472. ``setCc()`` or ``addCc()`` methods of the message.
  473. To set ``Cc:`` recipients, create the message object using either
  474. ``new Swift_Message( ... )`` or
  475. ``Swift_Message::newInstance( ... )``, then call the
  476. ``setCc()`` method with a complete array of addresses, or use the
  477. ``addCc()`` method to iteratively add recipients.
  478. The ``setCc()`` method accepts input in various formats as described earlier in
  479. this chapter. The ``addCc()`` method takes either one or two parameters. The
  480. first being the email address and the second optional parameter being the name
  481. of the recipient.
  482. ``Cc:`` recipients are visible in the message headers and will be
  483. seen by the other recipients.
  484. .. note::
  485. Multiple calls to ``setCc()`` will not add new recipients -- each
  486. call overrides the previous calls. If you want to iteratively add Cc:
  487. recipients, use the ``addCc()`` method.
  488. .. code-block:: php
  489. //Using setCc() to set all recipients in one go
  490. $message->setCc(array(
  491. 'person1@example.org',
  492. 'person2@otherdomain.org' => 'Person 2 Name',
  493. 'person3@example.org',
  494. 'person4@example.org',
  495. 'person5@example.org' => 'Person 5 Name'
  496. ));
  497. //Using addCc() to add recipients iteratively
  498. $message->addCc('person1@example.org');
  499. $message->addCc('person2@example.org', 'Person 2 Name');
  500. Setting ``Bcc:`` Recipients
  501. ~~~~~~~~~~~~~~~~~~~~~~~~~~~
  502. ``Bcc:`` recipients receive a copy of the message without anybody
  503. else knowing it, and are set with the ``setBcc()`` or
  504. ``addBcc`` methods of the message.
  505. To set ``Bcc:`` recipients, create the message object using
  506. either ``new Swift_Message( ... )`` or
  507. ``Swift_Message::newInstance( ... )``, then call the
  508. ``setBcc()`` method with a complete array of addresses, or use
  509. the ``addBcc()`` method to iteratively add recipients.
  510. The ``setBcc()`` method accepts input in various formats as described earlier in
  511. this chapter. The ``addBcc()`` method takes either one or two parameters. The
  512. first being the email address and the second optional parameter being the name
  513. of the recipient.
  514. Only the individual ``Bcc:`` recipient will see their address in
  515. the message headers. Other recipients (including other ``Bcc:``
  516. recipients) will not see the address.
  517. .. note::
  518. Multiple calls to ``setBcc()`` will not add new recipients -- each
  519. call overrides the previous calls. If you want to iteratively add Bcc:
  520. recipients, use the ``addBcc()`` method.
  521. .. code-block:: php
  522. //Using setBcc() to set all recipients in one go
  523. $message->setBcc(array(
  524. 'person1@example.org',
  525. 'person2@otherdomain.org' => 'Person 2 Name',
  526. 'person3@example.org',
  527. 'person4@example.org',
  528. 'person5@example.org' => 'Person 5 Name'
  529. ));
  530. //Using addBcc() to add recipients iteratively
  531. $message->addBcc('person1@example.org');
  532. $message->addBcc('person2@example.org', 'Person 2 Name');
  533. Specifying Sender Details
  534. -------------------------
  535. An email must include information about who sent it. Usually this is managed
  536. by the ``From:`` address, however there are other options.
  537. The sender information is contained in three possible places:
  538. * ``From:`` -- the address(es) of who wrote the message (required)
  539. * ``Sender:`` -- the address of the single person who sent the message
  540. (optional)
  541. * ``Return-Path:`` -- the address where bounces should go to (optional)
  542. You must always include a ``From:`` address by using
  543. ``setFrom()`` on the message. Swift Mailer will use this as the
  544. default ``Return-Path:`` unless otherwise specified.
  545. The ``Sender:`` address exists because the person who actually
  546. sent the email may not be the person who wrote the email. It has a higher
  547. precedence than the ``From:`` address and will be used as the
  548. ``Return-Path:`` unless otherwise specified.
  549. Setting the ``From:`` Address
  550. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  551. A ``From:`` address is required and is set with the
  552. ``setFrom()`` method of the message.
  553. ``From:`` addresses specify who actually wrote the email, and
  554. usually who sent it.
  555. What most people probably don't realise is that you can have more than one
  556. ``From:`` address if more than one person wrote the email --
  557. for example if an email was put together by a committee.
  558. To set the ``From:`` address(es):
  559. * Call the ``setFrom()`` method on the Message.
  560. The ``From:`` address(es) are visible in the message headers and
  561. will be seen by the recipients.
  562. .. note::
  563. If you set multiple ``From:`` addresses then you absolutely must set a
  564. ``Sender:`` address to indicate who physically sent the message.
  565. .. code-block:: php
  566. //Set a single From: address
  567. $message->setFrom('your@address.tld');
  568. //Set a From: address including a name
  569. $message->setFrom(array('your@address.tld' => 'Your Name'));
  570. //Set multiple From: addresses if multiple people wrote the email
  571. $message->setFrom(array(
  572. 'person1@example.org' => 'Sender One',
  573. 'person2@example.org' => 'Sender Two'
  574. ));
  575. Setting the ``Sender:`` Address
  576. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  577. A ``Sender:`` address specifies who sent the message and is set
  578. with the ``setSender()`` method of the message.
  579. To set the ``Sender:`` address:
  580. * Call the ``setSender()`` method on the Message.
  581. The ``Sender:`` address is visible in the message headers and
  582. will be seen by the recipients.
  583. This address will be used as the ``Return-Path:`` unless
  584. otherwise specified.
  585. .. note::
  586. If you set multiple ``From:`` addresses then you absolutely must set a
  587. ``Sender:`` address to indicate who physically sent the message.
  588. You must not set more than one sender address on a message because it's not
  589. possible for more than one person to send a single message.
  590. .. code-block:: php
  591. $message->setSender('your@address.tld');
  592. Setting the ``Return-Path:`` (Bounce) Address
  593. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  594. The ``Return-Path:`` address specifies where bounce notifications should
  595. be sent and is set with the ``setReturnPath()`` method of the message.
  596. You can only have one ``Return-Path:`` and it must not include
  597. a personal name.
  598. To set the ``Return-Path:`` address:
  599. * Call the ``setReturnPath()`` method on the Message.
  600. Bouce notifications will be sent to this address.
  601. .. code-block:: php
  602. $message->setReturnPath('bounces@address.tld');
  603. Requesting a Read Receipt
  604. -------------------------
  605. It is possible to request a read-receipt to be sent to an address when the
  606. email is opened. To request a read receipt set the address with
  607. ``setReadReceiptTo()``.
  608. To request a read receipt:
  609. * Set the address you want the receipt to be sent to with the
  610. ``setReadReceiptTo()`` method on the Message.
  611. When the email is opened, if the mail client supports it a notification will be sent to this address.
  612. .. note::
  613. Read receipts won't work for the majority of recipients since many mail
  614. clients auto-disable them. Those clients that will send a read receipt
  615. will make the user aware that one has been requested.
  616. .. code-block:: php
  617. $message->setReadReceiptTo('your@address.tld');
  618. Setting the Character Set
  619. -------------------------
  620. The character set of the message (and it's MIME parts) is set with the
  621. setCharset() method. You can also change the global default of UTF-8 by
  622. working with the Swift_Preferences class.
  623. Swift Mailer will default to the UTF-8 character set unless otherwise
  624. overridden. UTF-8 will work in most instances since it includes all of the
  625. standard US keyboard characters in addition to most international characters.
  626. It is absolutely vital however that you know what character set your message
  627. (or it's MIME parts) are written in otherwise your message may be received
  628. completely garbled.
  629. There are two places in Swift Mailer where you can change the character set:
  630. * In the Swift_Preferences class
  631. * On each individual message and/or MIME part
  632. To set the character set of your Message:
  633. * Change the global UTF-8 setting by calling
  634. ``Swift_Preferences::setCharset()``; or
  635. * Call the ``setCharset()`` method on the message or the MIME part.
  636. .. code-block:: php
  637. //Approach 1: Change the global setting (suggested)
  638. Swift_Preferences::getInstance()->setCharset('iso-8859-2');
  639. //Approach 2: Call the setCharset() method of the message
  640. $message = Swift_Message::newInstance()
  641. ->setCharset('iso-8859-2');
  642. //Apprach 3: Specify the charset when setting the body
  643. $message->setBody('My body', 'text/html', 'iso-8859-2');
  644. //Approach 4: Specify the charset for each part added
  645. $message->addPart('My part', 'text/plain', 'iso-8859-2');
  646. Setting the Line Length
  647. -----------------------
  648. The length of lines in a message can be changed by using the
  649. ``setMaxLineLength()`` method on the message. It should be kept
  650. to less than 1000 characters.
  651. Swift Mailer defaults to using 78 characters per line in a message. This is
  652. done for historical reasons and so that the message can be easily viewed in
  653. plain-text terminals.
  654. To change the maximum length of lines in your Message:
  655. * Call the ``setMaxLineLength()`` method on the Message.
  656. Lines that are longer than the line length specified will be wrapped between
  657. words.
  658. .. note::
  659. You should never set a maximum length longer than 1000 characters
  660. according to RFC 2822. Doing so could have unspecified side-effects such
  661. as truncating parts of your message when it is transported between SMTP
  662. servers.
  663. .. code-block:: php
  664. $message->setMaxLineLength(1000);
  665. Setting the Message Priority
  666. ----------------------------
  667. You can change the priority of the message with
  668. ``setPriority()``. Setting the priority will not change the way
  669. your email is sent -- it is purely an indicative setting for the
  670. recipient.
  671. The priority of a message is an indication to the recipient what significance
  672. it has. Swift Mailer allows you to set the priority by calling the
  673. ``setPriority`` method. This method takes an integer value
  674. between 1 and 5:
  675. * Highest
  676. * High
  677. * Normal
  678. * Low
  679. * Lowest
  680. To set the message priority:
  681. * Set the priority as an integer between 1 and 5 with the ``setPriority()``
  682. method on the Message.
  683. .. code-block:: php
  684. //Indicate "High" priority
  685. $message->setPriority(2);