Home arrow PHP arrow Page 3 - Introducing Swift Mailer

Creating mailer and message objects - PHP

Do you want to build a complex email program in PHP that offers more control than the language's native "mail()" function? You may want to consider using the Swift Mailer email library. This multi-part series will walk you through some of its most useful features.

TABLE OF CONTENTS:
  1. Introducing Swift Mailer
  2. Start using Swift Mailer
  3. Creating mailer and message objects
  4. Sending an email message with the send() method
By: Alejandro Gervasio
Rating: starstarstarstarstar / 6
February 10, 2010

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

In the previous segment, I discussed how to create a SMTP transport object from the “Swift_SmtpTransport” class provided by Swift Mailer. Now, I"m going to build a brand new mailer object, which will permit us to send a great variety of email messages very easily. After that, I’m going to create another object, which will be responsible for building the different parts of the email message itself.

Having explained that, here’s the code required to perform these tasks. Take a look at it, please:

// 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'))

// build the body part of the message

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

Definitely, the above script is extremely simple to follow, even if you don’t have a strong background in working with classes and objects. It begins creating a new mailer object by statically calling the factory “newInstance()” method of the “Swift_Mailer” class. Once this process has been completed correctly, a basic email message is built by using a separate class called, not surprisingly, “Swift_Message.”

Again, it’s valid to note here the clever use of the static, chainable factory “newInstance()” method. It returns a message object to client code and allows you to specify certain common parameters of the message, such as the subject, the From and To arguments, and finally the body part.

Well, at this stage I've explained in a step-by-step fashion how to create the different objects required by Swift Mailer to send a simple email message. However, the best way to understand how this process is performed is by putting all of the pieces together in the same script.

With that idea in mind, below I listed the complete script. It first builds the SMTP transport object, then creates the corresponding mailer, and finally composes the message with the proper arguments. Here it is:

// include required file

require_once 'lib/swift_required.php';

 

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

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

->setUsername('alejandro@domain.com')

->setPassword('password');

 

// 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'))

// build the body part of the message

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

I don’t want to sound a bit over-excited here, but things are definitely becoming much more interesting now. Everything has been set up to send a basic email message with Swift Mailer. However, there’s something missing yet; it’s necessary to call a method of the mailer object that actually dispatches the message in question.

Not surprisingly, this method has been called “send(),” and in the last section of this tutorial I’m going to discuss how to use it in conjunction with the script developed before.

Now, to find out how to send email messages with this new method, click on the link below and read the next few lines.



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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- 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: