How to add attachment with the Zend_Mail?

Files can be attached to an email using Zend_Mail->createAttachment() method. The createAttachment() method requires the content of the attachment and 3 optional arguments. Please note that it takes file content as an argument, not the name of the file.

    $mail = new Zend_Mail();
    $content = file_get_contents("attachment.docx")
    $mail->createAttachment($content,
	"application/actet-stream", // Default
	Zend_Mime::DISPOSITION_ATTACHMENT,  // Default
	Zend_Mime::ENCODING_BASE64, // Default
	"attachment.docx");

You may also instantiate a Zend_Mime_Part object to further customize the attachment parameters.

    $content = file_get_contents("flower.gif")
    $attachment = new Zend_Mime_Part($content);
    $attachment->type        = 'image/gif';
    $attachment->disposition = Zend_Mime::DISPOSITION_INLINE;
    $attachment->encoding    = Zend_Mime::ENCODING_BASE64;
    $attachment->filename    = 'flower.gif';
    $mail->addAttachment($attachment);

MIME stands for Multipurpose Internet Mail Extensions, which is used to extend the header, body or attachment of email format other than ASCII. With proper MIME type assigned, email clients would be able to display attachment file(s) INLINE with message body if MIME disposition is set to INLINE.

The Zend_Mime_Part and Zend_Mail->createAttachment() method takes optional MIME Headers as input parameter(s). The three significant MIME headers include content-type, content-disposition and content-encoding:

  • Content-Type: The content type consists of type and subtype separated by a forward slash (i.e. "Content-Type: text/plain"). Zend_Mail uses "application/octet-stream" as the default content-type, which signifies the content of the attachment is a "binary" file without further specification. This content-type can be used for any binary file attachment such as word processor, spreadsheet, and image files. Using this content-type will not allow email clients to display the contents INLINE as the client will not know type of file it's dealing with. To specify the exact content-type, you may wish to read Internet Media Type Wikipedia article.

  • Content-Disposition: The content-disposition specifies the presentation of style, and it can have INLINE or ATTACHMENT values. The INLINE allows a client to automatically display the content with the body of the text, and ATTACHMENT content-disposition requires some form of user action to open the attachment. To use INLINE disposition, the content-type must be very specific so that the email client knows which application plug-in to use to display the INLINE content. Valid values are Zend_Mime::DISPOSITION_INLINE and DISPOSITION_ATTACHMENT.

  • Content-Transfer-Encoding: MIME defined a set of methods for representing binary data in ASCII text format when transmitting data over the Internet. The valid values include Zend_Mime::ENCODING_7BIT, ENCODING_8BIT, ENCODING_QUOTEDPRINTABLE and ENCODING_BASE64. 7BIT encoding is strictly for ASCII values ranging from 1..127, and Quoted-Printerable encoding is for ASCII (1..127) with a small subset of printable characters in the ASCII (128..255) range. Base64 and 8Bit are used for binary encoding.

Conclusion

This guide explains how to attach files to an email using Zend Framework's Zend_Mail class and its createAttachment() method. It also covers the use of Zend_Mime_Part and various aspects of MIME types for more customization.

Share this post

Comments (0)

    No comment

Leave a comment

All comments are moderated. Spammy and bot submitted comments are deleted. Please submit the comments that are helpful to others, and we'll approve your comments. A comment that includes outbound link will only be approved if the content is relevant to the topic, and has some value to our readers.


Login To Post Comment