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

Dynamically changing the name of an attachment - 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

As I said in the section that you just read, Swift Mailer permits you to dynamically change the name of a file being attached to an email message. But, how can this be done? Well, as you might have guessed, the library has another handy method called “setFileName()” that will perform this process in a simple manner.

Obviously, the use of this method can be better understood by means of an example. So, bearing this idea in mind, below I coded a pretty simple one for you. Check it out:

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

 

// 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('/image1.jpg')->setFileName('otherimage.jpg'));

 

// send the email message

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

{

echo 'The message was sent successfully!';

}

else

{

echo 'Error sending email message';

}

That was quite easy to accomplish, right? As you can see, the aforementioned “setFileName()” method permits you to easily modify the name of a file being attached, before sending an email message. While this process may not be a substantial or critical improvement within the previous script, it does show that something as simple as changing the name of an attachment can be done through an elegant and tight programming interface.

And unfortunately, with this last example I’m coming to the end of this article. As always, I encourage you to edit all of the code samples shown in it, so you can see for yourself how simple it is to handle attachments with Swift Mailer.

Final thoughts

In this eighth chapter of the series, you learned how to attach files that have been dynamically generated to an email message. In addition, you saw how to use the “setFileName()” method included with Swift Mailer to modify the name of an attachment before it is sent, which speaks for itself about the flexibility offered by this library.

In the upcoming tutorial, I’m going to teach you how to embed attached files into messages by manipulating the “Content-disposition” MIME header. Thus, now that you know what to expect from the next part, you simply can't 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: