2012-04-17 12 views
6

¿Alguien me puede dar algún ejemplo sobre cómo agregar archivos adjuntos al componente ZF2 Mail?Zend Mail 2.0 Archivos adjuntos

Me gustó:

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

pero quedé atrapado cuando sea necesario añadir un archivo adjunto. Gracias.

Respuesta

9

Para agregar un archivo adjunto, solo tiene que crear una nueva parte MIME y agregarla al mensaje.

Ejemplo:

// 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 

Aquí es algún tipo de documentación útil sobre el tema: ZF2 - Zend\Mail

+0

todavía no he verificado, pero que parece ser una solución de trabajo porque yo también considero que debe haber algún acuerdo con Mime Gracias. –

+6

No tengo idea de por qué se tomó la decisión de eliminar el método 'addAttachment' de la clase Mail, que en ZF1 simplemente crearía una nueva parte mime para usted y la agregaría al mensaje, ahora es un poco más explícita. – drew010

+2

Creo que vale la pena mencionar aquí que el nuevo MimePart y MimeMessage en ZF 2 están ahora en el Zend \ Mime \ Part y Zend \ Mime \ Message, por lo que tendrá que usar sus espacios de nombres en consecuencia. –

Cuestiones relacionadas