Message Basics

A message is a container for anything you want to send to somebody else. There are several basic aspects of a message that you should know.

An e-mail message is made up of several relatively simple entities that are combined in different ways to achieve different results. All of these entities have the same fundamental outline but serve a different purpose. The Message itself can be defined as a MIME entity, an Attachment is a MIME entity, all MIME parts are MIME entities – and so on…

The basic units of each MIME entity – be it the Message itself, or an Attachment – are its Headers and its body:
Header-Name: A header value
Other-Header: Another value

The body content itself

The Headers of a MIME entity, and its body must conform to some strict standards defined by various RFC documents. Swift Mailer ensures that these specifications are followed by using various types of object, including Encoders and different Header types to generate the entity.

The Structure of a Message

Of all of the MIME entities, a message – Swift_Message is the largest and most complex. It has many properties that can be updated and it can contain other MIME entities – attachments for example – nested inside it.

A Message has a lot of different Headers which are there to present information about the message to the recipients' mail client. Most of these headers will be familiar to the majority of users, but we'll list the basic ones. Although it's possible to work directly with the Headers of a Message (or other MIME entity), the standard Headers have accessor methods provided to abstract away the complex details for you. For example, although the Date on a message is written with a strict format, you only need to pass a UNIX timestamp to setDate().

Header Description Accessors
Message-ID: Identifies this message with a unique ID, usually containining the domain name and time generated getId() / setId()
Return-Path: Specifies where bounces should go (Swift Mailer reads this for other uses) getReturnPath() / setReturnPath()
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()
Sender: Specifies the address of the person who physically sent the message (higher precedence than From:) getSender() / setSender()
To: Specifies the addresses of the intended recipients getTo() / setTo()
Cc: Specifies the addresses of recipients who will be copied in on the message getCc() / setCc()
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()
Reply-To: Specifies the address where replies are sent to getReplyTo() / setReplyTo()
Subject: Specifies the subject line that is displayed in the recipients' mail client getSubject() / setSubject()
Date: Specifies the date at which the message was sent getDate() / setDate()
Content-Type: Specifies the format of the message (usually text/plain or text/html) getContentType() / setContentType()
Conent-Transfer-Encoding: Specifies the encoding scheme in the message getEncoder() / setEncoder()

Working with a Message Object

Although there are a lot of available methods on a message object, you only need to make use of a small subset of them. Usually you'll use setSubject(), setTo() and setFrom() before setting the body of your message with setBody().

Calling methods is simple. You just call them like functions, but using the object operator "->" to do so. If you've created a message object and called it $message then you'd set a subject on it like so:
<?php

require_once 'lib/swift_required.php';  

$message = Swift_Message::newInstance();
$message->setSubject('My subject');

All MIME entities (including a message) have a toString() method that you can call if you want to take a look at what is going to be sent. For example, if you echo $message->toString(); you would see something like this:
Message-ID: <1230173678.4952f5eeb1432@swift.generated>
Date: Thu, 25 Dec 2008 13:54:38 +1100
Subject: Example subject
From: Chris Corbyn <chris@w3style.co.uk>
To: Receiver Name <recipient@example.org>
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: quoted-printable

Here is the message

We'll take a closer look at the methods you use to create your message in the following sections.

NOTE: Documentation for version 3 is in the wiki.