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:
- Call the newInstance() method of Swift_Message.
- Set your sender address (From:) with setFrom() or setSender().
- Set a subject line with setSubject().
- Set recipients with setTo(), setCc() and/or setBcc().
- Set a body with setBody().
- 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'))
;
Parent topic: Creating Messages
Next topic: Message Basics