Zend Mail 2.0 Attachments

Can someone provide me an example of how to add attachments to the ZF2 Mail component?

I like it:

$message = new Message; $message->setEncoding('utf-8'); $message->setTo($email); $message->setReplyTo($replyTo); $message->setFrom($from); $message->setSubject($subject); $message->setBody($body); 

but stuck when needed to add an attachment. Thanks.

+6
source share
1 answer

To add an attachment, you just need to create a new MIME part and add it to the message.

Example:

 // create a new Zend\Mail\Message object $message = new Message; // create a MimeMessage object that will hold the mail body and any attachments $bodyPart = new MimeMessage; // create the attachment $attachment = new MimePart(fopen($pathToAttachment)); // or $attachment = new MimePart($attachmentContent); // set attachment content type $attachment->type = 'image/png'; // create the mime part for the message body // you can add one for text and one for html if needed $bodyMessage = new MimePart($body); $bodyMessage->type = 'text/html'; // add the message body and attachment(s) to the MimeMessage $bodyPart->setParts(array($bodyMessage, $attachment)); $message->setEncoding('utf-8') ->setTo($email) ->setReplyTo($replyTo) ->setFrom($from) ->setSubject($subject) ->setBody($bodyPart); // set the body of the Mail to the MimeMessage with the mail content and attachment 

Here are some useful related documents: ZF2 - Zend \ Mail

+9
source

Source: https://habr.com/ru/post/913424/


All Articles