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.
blog comments powered by Disqus |
|
|
|
|
|
|
|