2012-04-17 12 views
6

Qualcuno può fornirmi qualche esempio su come aggiungere allegati al componente ZF2 Mail?Zend Mail 2.0 Allegati

mi è piaciuto:

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

ma sono bloccato quando necessario, per aggiungere un allegato. Grazie.

risposta

9

Per aggiungere un allegato, è sufficiente creare una nuova parte MIME e aggiungerla al messaggio.

Esempio:

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

Ecco alcuni documenti utili sul tema: ZF2 - Zend\Mail

+0

non ho ancora verificato, ma sembra essere una soluzione di lavoro perché ho anche considerato che ci dovrebbe essere qualche affare con Mime. Grazie. –

+6

Non ho idea del motivo per cui è stata presa la decisione di rimuovere il metodo 'addAttachment' dalla classe Mail che in ZF1 creerebbe solo una nuova parte di mime per te e la aggiungerà al messaggio, ora è un po 'più esplicita. – drew010

+2

Penso che valga la pena ricordare qui che i nuovi MimePart e MimeMessage in ZF 2 sono ora in Zend \ Mime \ Part e Zend \ Mime \ Message - quindi dovrai usare i loro spazi dei nomi di conseguenza. –