2012-04-17 15 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

私はまだそれを確認していませんが、私はまた、いくつかの契約がなければならないと考えているため、作動溶液のようですMimeと一緒に。ありがとう。 –

+6

ZF1で新しいmimeパートを作成してメッセージに追加するMailクラスから 'addAttachment'メソッドを削除した理由はわかりません。 – drew010

+2

ZF 2の新しいMimePartとMimeMessageがZend \ Mime \ PartとZend \ Mime \ Messageにあることをここで言及する価値はあると思います。そのため、名前空間を適切に使用する必要があります。 –

関連する問題