Documentation

Attachments — Zend Framework 2 2.0.7 documentation

Attachments

Files can be attached to an e-mail using the createAttachment() method. The default behavior of Zend\Mail\Message is to assume the attachment is a binary object (application/octet-stream), that it should be transferred with base64 encoding, and that it is handled as an attachment. These assumptions can be overridden by passing more parameters to createAttachment():

E-Mail Messages with Attachments

1
2
3
4
5
6
7
$mail = new Zend\Mail\Message();
// build message...
$mail->createAttachment($someBinaryString);
$mail->createAttachment($myImage,
                        'image/gif',
                        Zend\Mime\Mime::DISPOSITION_INLINE,
                        Zend\Mime\Mime::ENCODING_BASE64);

If you want more control over the MIME part generated for this attachment you can use the return value of createAttachment() to modify its attributes. The createAttachment() method returns a Zend\Mime\Part object:

1
2
3
4
5
6
7
8
9
$mail = new Zend\Mail\Message();

$at = $mail->createAttachment($myImage);
$at->type        = 'image/gif';
$at->disposition = Zend\Mime\Mime::DISPOSITION_INLINE;
$at->encoding    = Zend\Mime\Mime::ENCODING_BASE64;
$at->filename    = 'test.gif';

$mail->send();

An alternative is to create an instance of Zend\Mime\Part and add it with addAttachment():

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
$mail = new Zend\Mail\Message();

$at = new Zend\Mime\Part($myImage);
$at->type        = 'image/gif';
$at->disposition = Zend\Mime\Mime::DISPOSITION_INLINE;
$at->encoding    = Zend\Mime\Mime::ENCODING_BASE64;
$at->filename    = 'test.gif';

$mail->addAttachment($at);

$mail->send();

Copyright

© 2006-2019 by Zend, a Rogue Wave Company. Made with by awesome contributors.

This website is built using zend-expressive and it runs on PHP 7.

Contacts