How to add attachment with the Zend_Mail?

Files can be attached to an email using Zend_Mail->createAttachment() method. The createAttachment() method requires a 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")
	"application/actet-stream", // Default
	Zend_Mime::ENCODING_BASE64, // Default

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';

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 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 "binary" file without further specification. This content-type can be used for any binary file attachment such as word processor, spread sheet, 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 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 body of the text, and ATTACHMENT content-disposition require 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 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 small subset of printerable characters in the ASCII (128..255) range. Base64 and 8Bit are used for binary encoding.



Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.