Quick Reference for Creating a Message

You can think of creating a Message as being similar to the steps you perform when you click the Compose button in your mail client. You give it a subject, specify some recipients, add any attachments and write your message.

To create a Message:

  1. Call the newInstance() method of Swift_Message.
  2. Set your sender address (From:) with setFrom() or setSender().
  3. Set a subject line with setSubject().
  4. Set recipients with setTo(), setCc() and/or setBcc().
  5. Set a body with setBody().
  6. Add attachments with attach().

Creating a Message

<?php

require_once 'lib/swift_required.php';

//Create the message
$message = Swift_Message::newInstance()

  //Give the message a subject
  ->setSubject('Your subject')

  //Set the From address with an associative array
  ->setFrom(array('john@doe.com' => 'John Doe'))

  //Set the To addresses with an associative array
  ->setTo(array('receiver@domain.org', 'other@domain.org' => 'A name'))

  //Give it a body
  ->setBody('Here is the message itself')

  //And optionally an alternative body
  ->addPart('<q>Here is the message itself</q>', 'text/html')

  //Optionally add any attachments
  ->attach(Swift_Attachment::fromPath('my-document.pdf'))
  ;

NOTE: Documentation for version 3 is in the wiki.