Attaching Files
Attachments are downloadable parts of a message and can be added by calling the attach() method on the message. You can add attachments that exist on disk, or you can create attachments on-the-fly.
Attachments are actually an interesting area of Swift Mailer and something that could put a lot of power at your fingertips if you grasp the concept behind the way a message is held together.
Although we refer to files sent over e-mails as "attachments" – because they're attached to the message – lots of other parts of the message are actually "attached" even if we don't refer to these parts as attachments.
File attachments are created by the Swift_Attachment class and then attached to the message via the attach() method on it. For all of the "every day" MIME types such as all image formats, word documents, PDFs and spreadsheets you don't need to explicitly set the content-type of the attachment, though it would do no harm to do so. For less common formats you should set the content-type – which we'll cover in a moment.
-
Attaching Existing Files
Files that already exist, either on disk or at a URL can be attached to a message with just one line of code, using Swift_Attachment::fromPath(). -
Setting the Filename
Usually you don't need to explicitly set the filename of an attachment because the name of the attached file will be used by default, but if you want to set the filename you use the setFilename() method of the Attachment. -
Attaching Dynamic Content
Files that are generated at runtime, such as PDF documents or images created via GD can be attached directly to a message without writing them out to disk. Use the standard Swift_Attachment::newInstance() method. -
Changing the Disposition
Attachments just appear as files that can be saved to the Desktop if desired. You can make attachment appear inline where possible by using the setDisposition() method of an attachment.