2012-04-17 4 views
6

누군가 ZF2 Mail 구성 요소에 첨부 파일을 추가하는 방법에 대한 몇 가지 예를 제공 할 수 있습니까?Zend Mail 2.0 첨부 파일

나는 좋아했다 :

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

하지만 첨부 파일을 추가 할 필요에 붙어있어. 감사합니다. .

답변

9

첨부 파일을 추가하려면 새 MIME 부분을 만들어 메시지에 추가하기 만하면됩니다.

예 : 여기

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

이 주제에 대한 몇 가지 유용한 문서입니다 : ZF2 - Zend\Mail

+0

난 아직 확인하지 않은 그러나 나는 또한 일부 계약이 있어야한다고 생각하기 때문에 작업 솔루션이 될 것 같다 마임과. 감사. –

+6

ZF1에서 새 mime 파트를 만들고 메시지에 추가하는 메일 클래스에서 addAttachment 메서드를 제거하는 결정이 왜 나왔는지는 알 수 없습니다. 이제는 좀 더 명시 적입니다. – drew010

+2

ZF 2의 새로운 MimePart와 MimeMessage가 이제 Zend \ Mime \ Part와 Zend \ Mime \ Message에 있음을 언급 할 가치가 있다고 생각합니다. 따라서 네임 스페이스를 적절하게 사용해야 할 것입니다. –

관련 문제