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
// create the mail transport using the 'newInstance()' method
$transport = Swift_SmtpTransport::newInstance('mail.domain.com', 25)
// 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('firstname.lastname@example.org' => 'Alejandro Gervasio'))
// specify the To argument
->setTo(array('email@example.com' =>' John Doe'))
->setCc(array('firstname.lastname@example.org' => 'Mary Jackson'))
->setBcc(array('email@example.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')
// send the email message
echo 'The message was sent successfully!';
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.
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!
blog comments powered by Disqus