Home arrow PHP arrow Page 2 - Handling Attachments and Sending HTML Email Messages with Swift Mailer

Sending copies with the setCc() and setBcc() methods - PHP

In this sixth article of a series I discuss how to handle file attachments and sending HTML email messages with the Swift Mailer library. As you'll soon see, performing these tasks is a very straightforward process.

  1. Handling Attachments and Sending HTML Email Messages with Swift Mailer
  2. Sending copies with the setCc() and setBcc() methods
  3. Handling file attachments with Swift Mailer
  4. Sending email messages in HTML with the addPart() method
By: Alejandro Gervasio
Rating: starstarstarstarstar / 1
February 22, 2010

print this article



Before I explain how to handle attachments and send HTML email messages with Swift Mailer, I'd like to reintroduce the script created in the previous tutorial of this series, which demonstrated how to send a blind carbon copy via the "setBcc()" method offered by the library.

With that point clarified, here's the small program that performs the aforementioned task:

// example on sending a basic email message with Swift Mailer (uses the 'setCc() and 'setBcc() methods')


// include required files

require_once 'lib/swift_required.php';


// create the mail transport using the 'newInstance()' method

$transport = Swift_SmtpTransport::newInstance('mail.domain.com', 25)




// create the mailer using the 'newInstance()' method

$mailer = Swift_Mailer::newInstance($transport);


// create a simple message using the 'newInstance()' method

$message = Swift_Message::newInstance()

// specify the subject of the message

->setSubject('Testing Swift Mailer')

// specify the From argument

->setFrom(array('alejandro@domain.com' => 'Alejandro Gervasio'))

// specify the To argument

->setTo(array('john@domain.com' =>' John Doe'))

->setCc(array('mary@domain.com' => 'Mary Jackson'))

->setBcc(array('susan@domain.com' => 'Susan Norton'))

// build the body part of the message

->setBody('Hey, how are you? I am sending you a message with the cool Swift Mailer library');


// send the email message

if ($mailer->send($message))


echo 'The message was sent successfully!';




echo 'Error sending email message';


As you can see from the above script, both the "setCc()" and "setBcc()" methods make it really easy to send copies of an original message to a number of predefined recipients. Actually, the process is reduced to calling the methods in question with the proper incoming arguments, prior to creating the message itself. It's nothing too complex to grasp, really.

Well, now that you're hopefully familiar with using the methods discussed before, it's time to explore other useful features provided by Swift Mailer. So, in accordance with the concepts that I expressed in the introduction, over the course of the upcoming section I'm going to explain how to handle file attachments in a truly approachable fashion.

As you may guess, this process will be performed with the help of a brand new class included with the library, called "Swift_Attachment." But I'm getting ahead of myself, so if you wish to learn more about this class, click on the link displayed below and keep reading.

>>> More PHP Articles          >>> More By Alejandro Gervasio

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates


Dev Shed Tutorial Topics: