Home arrow PHP arrow Page 3 - Using Different Paths for Attachments with Swift Mailer

Attaching files from a different location with the fromPath() method - PHP

Welcome to the seventh tutorial of a series on Swift Mailer. Comprised of ten parts, this series introduces you gently to using this thorough PHP5-based email package. It complements the corresponding theory with numerous code samples that you can incorporate into your own PHP applications.

TABLE OF CONTENTS:
  1. Using Different Paths for Attachments with Swift Mailer
  2. Review: handling file attachments with Swift Mailer
  3. Attaching files from a different location with the fromPath() method
  4. Attaching files from a remote host
By: Alejandro Gervasio
Rating: starstarstarstarstar / 1
February 23, 2010

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

As I stated in the introduction, Swift Mailer provides developers with the ability to attach files from locations other than where the email script or application is being executed.

To demonstrate this capability, below I rewrote the example shown in the previous section, which uses the “fromPath()” method to attach an image file from a distinct folder on the web server.

Here’s the corresponding code sample; examine it closely, please:

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

}

As depicted above, thanks to the flexibility offered by the “fromPath()” method, it’s perfectly possible to attach a file from a different location on the web server. Naturally, for the sake of clarity, the file path specified in the above example is merely fictional, but when working in a production environment, the path obviously would be a real one.

So far, everything looks good, right? At this point, you've learned how to use Swift Mailer’s abilities for attaching files from a different folder on the local web server. Nevertheless, the library also permits you to perform this same task by using a remote host. In the upcoming segment I’m going to build a final script, which will recreate this particular scenario for you.

To see how this last example will be developed, click on the link below and keep reading.



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