Home arrow PHP arrow Page 2 - Dynamically Attaching Files with Swift Mailer

Review: handling attachments with Swift Mailer - PHP

In this eighth part of a ten-part series on the Swift Mailer email library, you'll learn how to attach files that have been dynamically generated to an email message. In addition, you'll see how to use Swift Mailer's "setFileName()" method to modify the name of an attachment before it's sent, which speaks for itself about this library's flexibility.

TABLE OF CONTENTS:
  1. Dynamically Attaching Files with Swift Mailer
  2. Review: handling attachments with Swift Mailer
  3. Attaching dynamically-generated files
  4. Dynamically changing the name of an attachment
By: Alejandro Gervasio
Rating: starstarstarstarstar / 1
February 24, 2010

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

Before I proceed to explain how to use Swift Mailer to attach files that have been generated dynamically, I’m going to reintroduce the example developed in the previous tutorial of this series. It demonstrated how to attach an image file to an email message from a local server, and a remote host as well.

Having stated that, here’s the first example. It uses a local host to attach the image.

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

 

// 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 as HTML to make it look nicer</p>', 'text/html')

->attach(Swift_Attachment::fromPath('path/to/file/image1.jpg'));

 

// send the email message

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

{

echo 'The message was sent successfully!';

}

else

{

echo 'Error sending email message';

}

The above script is very easy to follow. All it does is attach an image file to an email message from a specified location on the local server. Once this operation has been performed successfully, the message is sent out via the already familiar “send()” method. It's simple to code and read, indeed.

Now, moving forward, take a look at the second example, which does nearly the same thing as the previous one. In this case, however, the image file is attached from a remote URL. Here it is:

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

 

// 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 as HTML to make it look nicer</p>', 'text/html')

->attach(Swift_Attachment::fromPath('http://www.domain.com/images/image1.jpg'));

 

// send the email message

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

{

echo 'The message was sent successfully!';

}

else

{

echo 'Error sending email message';

}

As I said before, in this case the file being attached to the message is retrieved from a fictional remote host, thus demonstrating the remarkable flexibility offered by the “fromPath()” method of the “Swift_Attachment” class, especially when attaching files from different locations.

So far, so good. At this stage, I’m sure that you quickly grasped how the two previous scripts work, so it’s time to explore other handy capabilities of the Swift Mailer library. Therefore, in the following section I’m going to discuss how to use the aforementioned “fromPath()” method for attaching a file that has been created dynamically.

To learn how this will be done, please click on the link below and read the next segment.



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