messages.rst 37KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965
  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
  130. ``attach()``.
  131. Setting the Subject Line
  132. ~~~~~~~~~~~~~~~~~~~~~~~~
  133. The subject line, displayed in the recipients' mail client can be set with the
  134. ``setSubject()`` method, or as a parameter to ``Swift_Message::newInstance()``.
  135. To set the subject of your Message:
  136. * Call the ``setSubject()`` method of the Message, or specify it at the time
  137. you create the message.
  138. .. code-block:: php
  139. // Pass it as a parameter when you create the message
  140. $message = Swift_Message::newInstance('My amazing subject');
  141. // Or set it after like this
  142. $message->setSubject('My amazing subject');
  143. Setting the Body Content
  144. ~~~~~~~~~~~~~~~~~~~~~~~~
  145. The body of the message -- seen when the user opens the message --
  146. is specified by calling the ``setBody()`` method. If an alternative body is to
  147. be included ``addPart()`` can be used.
  148. The body of a message is the main part that is read by the user. Often people
  149. want to send a message in HTML format (``text/html``), other
  150. times people want to send in plain text (``text/plain``), or
  151. sometimes people want to send both versions and allow the recipient to chose
  152. how they view the message.
  153. As a rule of thumb, if you're going to send a HTML email, always include a
  154. plain-text equivalent of the same content so that users who prefer to read
  155. plain text can do so.
  156. To set the body of your Message:
  157. * Call the ``setBody()`` method of the Message, or specify it at the time you
  158. create the message.
  159. * Add any alternative bodies with ``addPart()``.
  160. If the recipient's mail client offers preferences for displaying text vs. HTML
  161. then the mail client will present that part to the user where available. In
  162. other cases the mail client will display the "best" part it can - usually HTML
  163. if you've included HTML.
  164. .. code-block:: php
  165. // Pass it as a parameter when you create the message
  166. $message = Swift_Message::newInstance('Subject here', 'My amazing body');
  167. // Or set it after like this
  168. $message->setBody('My <em>amazing</em> body', 'text/html');
  169. // Add alternative parts with addPart()
  170. $message->addPart('My amazing body in plain text', 'text/plain');
  171. Attaching Files
  172. ---------------
  173. Attachments are downloadable parts of a message and can be added by calling
  174. the ``attach()`` method on the message. You can add attachments that exist on
  175. disk, or you can create attachments on-the-fly.
  176. Attachments are actually an interesting area of Swift Mailer and something
  177. that could put a lot of power at your fingertips if you grasp the concept
  178. behind the way a message is held together.
  179. Although we refer to files sent over e-mails as "attachments" -- because
  180. they're attached to the message -- lots of other parts of the message are
  181. actually "attached" even if we don't refer to these parts as attachments.
  182. File attachments are created by the ``Swift_Attachment`` class
  183. and then attached to the message via the ``attach()`` method on
  184. it. For all of the "every day" MIME types such as all image formats, word
  185. documents, PDFs and spreadsheets you don't need to explicitly set the
  186. content-type of the attachment, though it would do no harm to do so. For less
  187. common formats you should set the content-type -- which we'll cover in a
  188. moment.
  189. Attaching Existing Files
  190. ~~~~~~~~~~~~~~~~~~~~~~~~
  191. Files that already exist, either on disk or at a URL can be attached to a
  192. message with just one line of code, using ``Swift_Attachment::fromPath()``.
  193. You can attach files that exist locally, or if your PHP installation has
  194. ``allow_url_fopen`` turned on you can attach files from other
  195. websites.
  196. To attach an existing file:
  197. * Create an attachment with ``Swift_Attachment::fromPath()``.
  198. * Add the attachment to the message with ``attach()``.
  199. The attachment will be presented to the recipient as a downloadable file with
  200. the same filename as the one you attached.
  201. .. code-block:: php
  202. // Create the attachment
  203. // * Note that you can technically leave the content-type parameter out
  204. $attachment = Swift_Attachment::fromPath('/path/to/image.jpg', 'image/jpeg');
  205. // Attach it to the message
  206. $message->attach($attachment);
  207. // The two statements above could be written in one line instead
  208. $message->attach(Swift_Attachment::fromPath('/path/to/image.jpg'));
  209. // You can attach files from a URL if allow_url_fopen is on in php.ini
  210. $message->attach(Swift_Attachment::fromPath('http://site.tld/logo.png'));
  211. Setting the Filename
  212. ~~~~~~~~~~~~~~~~~~~~
  213. Usually you don't need to explicitly set the filename of an attachment because
  214. the name of the attached file will be used by default, but if you want to set
  215. the filename you use the ``setFilename()`` method of the Attachment.
  216. To change the filename of an attachment:
  217. * Call its ``setFilename()`` method.
  218. The attachment will be attached in the normal way, but meta-data sent inside
  219. the email will rename the file to something else.
  220. .. code-block:: php
  221. // Create the attachment and call its setFilename() method
  222. $attachment = Swift_Attachment::fromPath('/path/to/image.jpg')
  223. ->setFilename('cool.jpg');
  224. // Because there's a fluid interface, you can do this in one statement
  225. $message->attach(
  226. Swift_Attachment::fromPath('/path/to/image.jpg')->setFilename('cool.jpg')
  227. );
  228. Attaching Dynamic Content
  229. ~~~~~~~~~~~~~~~~~~~~~~~~~
  230. Files that are generated at runtime, such as PDF documents or images created
  231. via GD can be attached directly to a message without writing them out to disk.
  232. Use the standard ``Swift_Attachment::newInstance()`` method.
  233. To attach dynamically created content:
  234. * Create your content as you normally would.
  235. * Create an attachment with ``Swift_Attachment::newInstance()``, specifying
  236. the source data of your content along with a name and the content-type.
  237. * Add the attachment to the message with ``attach()``.
  238. The attachment will be presented to the recipient as a downloadable file
  239. with the filename and content-type you specify.
  240. .. note::
  241. If you would usually write the file to disk anyway you should just attach
  242. it with ``Swift_Attachment::fromPath()`` since this will use less memory:
  243. .. code-block:: php
  244. // Create your file contents in the normal way, but don't write them to disk
  245. $data = create_my_pdf_data();
  246. // Create the attachment with your data
  247. $attachment = Swift_Attachment::newInstance($data, 'my-file.pdf', 'application/pdf');
  248. // Attach it to the message
  249. $message->attach($attachment);
  250. // You can alternatively use method chaining to build the attachment
  251. $attachment = Swift_Attachment::newInstance()
  252. ->setFilename('my-file.pdf')
  253. ->setContentType('application/pdf')
  254. ->setBody($data)
  255. ;
  256. Changing the Disposition
  257. ~~~~~~~~~~~~~~~~~~~~~~~~
  258. Attachments just appear as files that can be saved to the Desktop if desired.
  259. You can make attachment appear inline where possible by using the
  260. ``setDisposition()`` method of an attachment.
  261. To make an attachment appear inline:
  262. * Call its ``setDisposition()`` method.
  263. The attachment will be displayed within the email viewing window if the mail
  264. client knows how to display it.
  265. .. note::
  266. If you try to create an inline attachment for a non-displayable file type
  267. such as a ZIP file, the mail client should just present the attachment as
  268. normal:
  269. .. code-block:: php
  270. // Create the attachment and call its setDisposition() method
  271. $attachment = Swift_Attachment::fromPath('/path/to/image.jpg')
  272. ->setDisposition('inline');
  273. // Because there's a fluid interface, you can do this in one statement
  274. $message->attach(
  275. Swift_Attachment::fromPath('/path/to/image.jpg')->setDisposition('inline')
  276. );
  277. Embedding Inline Media Files
  278. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  279. Often people want to include an image or other content inline with a HTML
  280. message. It's easy to do this with HTML linking to remote resources, but this
  281. approach is usually blocked by mail clients. Swift Mailer allows you to embed
  282. your media directly into the message.
  283. Mail clients usually block downloads from remote resources because this
  284. technique was often abused as a mean of tracking who opened an email. If
  285. you're sending a HTML email and you want to include an image in the message
  286. another approach you can take is to embed the image directly.
  287. Swift Mailer makes embedding files into messages extremely streamlined. You
  288. embed a file by calling the ``embed()`` method of the message,
  289. which returns a value you can use in a ``src`` or
  290. ``href`` attribute in your HTML.
  291. Just like with attachments, it's possible to embed dynamically generated
  292. content without having an existing file available.
  293. The embedded files are sent in the email as a special type of attachment that
  294. has a unique ID used to reference them within your HTML attributes. On mail
  295. clients that do not support embedded files they may appear as attachments.
  296. Although this is commonly done for images, in theory it will work for any
  297. displayable (or playable) media type. Support for other media types (such as
  298. video) is dependent on the mail client however.
  299. Embedding Existing Files
  300. ........................
  301. Files that already exist, either on disk or at a URL can be embedded in a
  302. message with just one line of code, using ``Swift_EmbeddedFile::fromPath()``.
  303. You can embed files that exist locally, or if your PHP installation has
  304. ``allow_url_fopen`` turned on you can embed files from other 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()``
  409. and ``setBcc()``. Swift Mailer reads these recipients from the message when it
  410. gets 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
  420. ``From:`` 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
  431. non-associative 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 ``Swift_Message::newInstance( ... )``,
  444. then call the ``setTo()`` method with a complete array of addresses, or use the
  445. ``addTo()`` method to iteratively add recipients.
  446. The ``setTo()`` method accepts input in various formats as described earlier in
  447. this chapter. The ``addTo()`` method takes either one or two parameters. The
  448. first being the email address and the second optional parameter being the name
  449. of the recipient.
  450. ``To:`` recipients are visible in the message headers and will be
  451. seen by the other recipients.
  452. .. note::
  453. Multiple calls to ``setTo()`` will not add new recipients -- each
  454. call overrides the previous calls. If you want to iteratively add
  455. recipients, use the ``addTo()`` method.
  456. .. code-block:: php
  457. // Using setTo() to set all recipients in one go
  458. $message->setTo(array(
  459. 'person1@example.org',
  460. 'person2@otherdomain.org' => 'Person 2 Name',
  461. 'person3@example.org',
  462. 'person4@example.org',
  463. 'person5@example.org' => 'Person 5 Name'
  464. ));
  465. // Using addTo() to add recipients iteratively
  466. $message->addTo('person1@example.org');
  467. $message->addTo('person2@example.org', 'Person 2 Name');
  468. Setting ``Cc:`` Recipients
  469. ~~~~~~~~~~~~~~~~~~~~~~~~~~
  470. ``Cc:`` recipients are set with the ``setCc()`` or ``addCc()`` methods of the
  471. message.
  472. To set ``Cc:`` recipients, create the message object using either
  473. ``new Swift_Message( ... )`` or ``Swift_Message::newInstance( ... )``, then call
  474. the ``setCc()`` method with a complete array of addresses, or use the
  475. ``addCc()`` method to iteratively add recipients.
  476. The ``setCc()`` method accepts input in various formats as described earlier in
  477. this chapter. The ``addCc()`` method takes either one or two parameters. The
  478. first being the email address and the second optional parameter being the name
  479. of the recipient.
  480. ``Cc:`` recipients are visible in the message headers and will be
  481. seen by the other recipients.
  482. .. note::
  483. Multiple calls to ``setCc()`` will not add new recipients -- each
  484. call overrides the previous calls. If you want to iteratively add Cc:
  485. recipients, use the ``addCc()`` method.
  486. .. code-block:: php
  487. // Using setCc() to set all recipients in one go
  488. $message->setCc(array(
  489. 'person1@example.org',
  490. 'person2@otherdomain.org' => 'Person 2 Name',
  491. 'person3@example.org',
  492. 'person4@example.org',
  493. 'person5@example.org' => 'Person 5 Name'
  494. ));
  495. // Using addCc() to add recipients iteratively
  496. $message->addCc('person1@example.org');
  497. $message->addCc('person2@example.org', 'Person 2 Name');
  498. Setting ``Bcc:`` Recipients
  499. ~~~~~~~~~~~~~~~~~~~~~~~~~~~
  500. ``Bcc:`` recipients receive a copy of the message without anybody else knowing
  501. it, and are set with the ``setBcc()`` or ``addBcc()`` methods of the message.
  502. To set ``Bcc:`` recipients, create the message object using either ``new
  503. Swift_Message( ... )`` or ``Swift_Message::newInstance( ... )``, then call the
  504. ``setBcc()`` method with a complete array of addresses, or use
  505. the ``addBcc()`` method to iteratively add recipients.
  506. The ``setBcc()`` method accepts input in various formats as described earlier in
  507. this chapter. The ``addBcc()`` method takes either one or two parameters. The
  508. first being the email address and the second optional parameter being the name
  509. of the recipient.
  510. Only the individual ``Bcc:`` recipient will see their address in the message
  511. headers. Other recipients (including other ``Bcc:`` recipients) will not see the
  512. address.
  513. .. note::
  514. Multiple calls to ``setBcc()`` will not add new recipients -- each
  515. call overrides the previous calls. If you want to iteratively add Bcc:
  516. recipients, use the ``addBcc()`` method.
  517. .. code-block:: php
  518. // Using setBcc() to set all recipients in one go
  519. $message->setBcc(array(
  520. 'person1@example.org',
  521. 'person2@otherdomain.org' => 'Person 2 Name',
  522. 'person3@example.org',
  523. 'person4@example.org',
  524. 'person5@example.org' => 'Person 5 Name'
  525. ));
  526. // Using addBcc() to add recipients iteratively
  527. $message->addBcc('person1@example.org');
  528. $message->addBcc('person2@example.org', 'Person 2 Name');
  529. Specifying Sender Details
  530. -------------------------
  531. An email must include information about who sent it. Usually this is managed
  532. by the ``From:`` address, however there are other options.
  533. The sender information is contained in three possible places:
  534. * ``From:`` -- the address(es) of who wrote the message (required)
  535. * ``Sender:`` -- the address of the single person who sent the message
  536. (optional)
  537. * ``Return-Path:`` -- the address where bounces should go to (optional)
  538. You must always include a ``From:`` address by using ``setFrom()`` on the
  539. message. Swift Mailer will use this as the default ``Return-Path:`` unless
  540. otherwise specified.
  541. The ``Sender:`` address exists because the person who actually sent the email
  542. may not be the person who wrote the email. It has a higher precedence than the
  543. ``From:`` address and will be used as the ``Return-Path:`` unless otherwise
  544. specified.
  545. Setting the ``From:`` Address
  546. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  547. A ``From:`` address is required and is set with the ``setFrom()`` method of the
  548. message. ``From:`` addresses specify who actually wrote the email, and usually who sent it.
  549. What most people probably don't realise is that you can have more than one
  550. ``From:`` address if more than one person wrote the email -- for example if an
  551. email was put together by a committee.
  552. To set the ``From:`` address(es):
  553. * Call the ``setFrom()`` method on the Message.
  554. The ``From:`` address(es) are visible in the message headers and
  555. will be seen by the recipients.
  556. .. note::
  557. If you set multiple ``From:`` addresses then you absolutely must set a
  558. ``Sender:`` address to indicate who physically sent the message.
  559. .. code-block:: php
  560. // Set a single From: address
  561. $message->setFrom('your@address.tld');
  562. // Set a From: address including a name
  563. $message->setFrom(array('your@address.tld' => 'Your Name'));
  564. // Set multiple From: addresses if multiple people wrote the email
  565. $message->setFrom(array(
  566. 'person1@example.org' => 'Sender One',
  567. 'person2@example.org' => 'Sender Two'
  568. ));
  569. Setting the ``Sender:`` Address
  570. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  571. A ``Sender:`` address specifies who sent the message and is set with the
  572. ``setSender()`` method of the message.
  573. To set the ``Sender:`` address:
  574. * Call the ``setSender()`` method on the Message.
  575. The ``Sender:`` address is visible in the message headers and will be seen by
  576. the recipients.
  577. This address will be used as the ``Return-Path:`` unless otherwise specified.
  578. .. note::
  579. If you set multiple ``From:`` addresses then you absolutely must set a
  580. ``Sender:`` address to indicate who physically sent the message.
  581. You must not set more than one sender address on a message because it's not
  582. possible for more than one person to send a single message.
  583. .. code-block:: php
  584. $message->setSender('your@address.tld');
  585. Setting the ``Return-Path:`` (Bounce) Address
  586. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  587. The ``Return-Path:`` address specifies where bounce notifications should
  588. be sent and is set with the ``setReturnPath()`` method of the message.
  589. You can only have one ``Return-Path:`` and it must not include
  590. a personal name.
  591. To set the ``Return-Path:`` address:
  592. * Call the ``setReturnPath()`` method on the Message.
  593. Bouce notifications will be sent to this address.
  594. .. code-block:: php
  595. $message->setReturnPath('bounces@address.tld');
  596. Requesting a Read Receipt
  597. -------------------------
  598. It is possible to request a read-receipt to be sent to an address when the
  599. email is opened. To request a read receipt set the address with
  600. ``setReadReceiptTo()``.
  601. To request a read receipt:
  602. * Set the address you want the receipt to be sent to with the
  603. ``setReadReceiptTo()`` method on the Message.
  604. When the email is opened, if the mail client supports it a notification will be sent to this address.
  605. .. note::
  606. Read receipts won't work for the majority of recipients since many mail
  607. clients auto-disable them. Those clients that will send a read receipt
  608. will make the user aware that one has been requested.
  609. .. code-block:: php
  610. $message->setReadReceiptTo('your@address.tld');
  611. Setting the Character Set
  612. -------------------------
  613. The character set of the message (and it's MIME parts) is set with the
  614. ``setCharset()`` method. You can also change the global default of UTF-8 by
  615. working with the ``Swift_Preferences`` class.
  616. Swift Mailer will default to the UTF-8 character set unless otherwise
  617. overridden. UTF-8 will work in most instances since it includes all of the
  618. standard US keyboard characters in addition to most international characters.
  619. It is absolutely vital however that you know what character set your message
  620. (or it's MIME parts) are written in otherwise your message may be received
  621. completely garbled.
  622. There are two places in Swift Mailer where you can change the character set:
  623. * In the ``Swift_Preferences`` class
  624. * On each individual message and/or MIME part
  625. To set the character set of your Message:
  626. * Change the global UTF-8 setting by calling
  627. ``Swift_Preferences::setCharset()``; or
  628. * Call the ``setCharset()`` method on the message or the MIME part.
  629. .. code-block:: php
  630. // Approach 1: Change the global setting (suggested)
  631. Swift_Preferences::getInstance()->setCharset('iso-8859-2');
  632. // Approach 2: Call the setCharset() method of the message
  633. $message = Swift_Message::newInstance()
  634. ->setCharset('iso-8859-2');
  635. // Apprach 3: Specify the charset when setting the body
  636. $message->setBody('My body', 'text/html', 'iso-8859-2');
  637. // Approach 4: Specify the charset for each part added
  638. $message->addPart('My part', 'text/plain', 'iso-8859-2');
  639. Setting the Line Length
  640. -----------------------
  641. The length of lines in a message can be changed by using the ``setMaxLineLength()`` method on the message. It should be kept to less than
  642. 1000 characters.
  643. Swift Mailer defaults to using 78 characters per line in a message. This is
  644. done for historical reasons and so that the message can be easily viewed in
  645. plain-text terminals.
  646. To change the maximum length of lines in your Message:
  647. * Call the ``setMaxLineLength()`` method on the Message.
  648. Lines that are longer than the line length specified will be wrapped between
  649. words.
  650. .. note::
  651. You should never set a maximum length longer than 1000 characters
  652. according to RFC 2822. Doing so could have unspecified side-effects such
  653. as truncating parts of your message when it is transported between SMTP
  654. servers.
  655. .. code-block:: php
  656. $message->setMaxLineLength(1000);
  657. Setting the Message Priority
  658. ----------------------------
  659. You can change the priority of the message with ``setPriority()``. Setting the
  660. priority will not change the way your email is sent -- it is purely an
  661. indicative setting for the recipient.
  662. The priority of a message is an indication to the recipient what significance
  663. it has. Swift Mailer allows you to set the priority by calling the ``setPriority`` method. This method takes an integer value between 1 and 5:
  664. * Highest
  665. * High
  666. * Normal
  667. * Low
  668. * Lowest
  669. To set the message priority:
  670. * Set the priority as an integer between 1 and 5 with the ``setPriority()``
  671. method on the Message.
  672. .. code-block:: php
  673. // Indicate "High" priority
  674. $message->setPriority(2);