Just in case you haven't read the prior tutorial of this series, where I discussed how to attach a dynamically generated file to an email message, below I included the two sample scripts developed in that article. They show how to perform this task in a simple manner, in addition to illustrating how to change on the fly the name of the file. So, first, here's the example that attaches a dynamic image to a trivial message: // 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 // $image has been generated previously using for instance the GD library or a similar package $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::newInstance($image, 'sample_image.png'));
// send the email message if ($mailer->send($message)) { echo 'The message was sent successfully!'; } else { echo 'Error sending email message'; } From the above code sample, it's clear to see how easy it is to attach an image that has been created with a server-side library to an email message, thanks to the flexibility provided by the "newInstance()" method that belongs to the "Swift_Attachment" class. In this particular case the $image variable would hold the image in question, which has been generated as a PNG graphic. Now, moving forward, it's time to look at the second example that shows how to modify the name of an attachment. This time we use another handy method of Swift Mailer, not surprisingly called "setFileName()." Here's how this example looks: // 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'; } Nothing really spectacular is happening here, right? As shown before, the "setFileName()" method allows you to change the default name of an attached file dynamically, before sending the email message via the corresponding "send()" method. It's that simple, really. Well, at this point you hopefully understand how these two scripts do their business, so it's time to dig deeper into the capabilities offered by Swift Mailer to manipulate attachments in different ways. In the following section I'm going to demonstrate how to directly embed those attachments into an email message, first by using one new method called "setDisposition()" and finally by means of another one, named simply "embed()." To learn how the first of these methods can be used, jump ahead and read the next segment. It's only one click away.
blog comments powered by Disqus |
|
|
|
|
|
|
|