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

Sending email messages in HTML with the addPart() method - 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.

TABLE OF CONTENTS:
  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
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

Email messages in HTML format can be sent in several ways with Swift Mailer. However, the simplest way to do this is by means of another method of the "Swift_Message" class, called "addPart()," which permits you to add a new part to a message -- in this case, specifying that the content of that section will be delivered in HTML.

To have a clearer idea of how the "addPart()" method works, I coded another example, which demonstrates its usage. Here it is:

// example on sending a basic email message with Swift Mailer (uses the 'addPart()' method and Swift_Attachment class)

 

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

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

->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. Make sure to check the attached file!')

->addPart('<p>This part of the message has been formatted in HTML to make it look nicer</p>', 'text/html')

->attach(Swift_Attachment::fromPath('image1.jpg'));

 

// send the email message

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

{

echo 'The message was sent successfully!';

}

else

{

echo 'Error sending email message';

}

Do you realize how simple it is to send email messages in HTML? I bet you do! What's more, the previous example not accomplishes this task via the pertinent "addPart()" method, but it also attaches an image file to it. What else can you ask for?

Finally, with this example, I'm concluding this sixth installment of the series on using the Swift Mailer library. As always, feel free to tweak all of the code samples shown in this article, so you can sharpen your skills in working with this powerful email package. 

Final thoughts

That's all for now. In this sixth part of the series I discussed how to handle file attachments and sending HTML email messages with the Swift Mailer library. As you saw a few moments ago, performing these tasks is a very straightforward process that doesn't require any further explanation due to its simplicity.

In the upcoming tutorial, I'm going to extend my discussion of the previous "Swift_Attachment" class. It also allows you to attach files by using different paths. Since this specific feature will be explored in depth in the aforementioned article, you don't have any excuses to miss it!



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