2012-04-17 14 views
6

क्या कोई मुझे ZF2 मेल घटक में अनुलग्नक जोड़ने के तरीके के बारे में कुछ उदाहरण प्रदान कर सकता है?ज़ेंड मेल 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

अनुलग्नक जोड़ने के लिए, आपको बस एक नया एमआईएमई भाग बनाना होगा और इसे संदेश में जोड़ना होगा।

उदाहरण:

// 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 आपके लिए एक नया माइम हिस्सा बनायेगा और इसे संदेश में जोड़ देगा, अब यह थोड़ा और स्पष्ट है। – drew010

+2

मुझे लगता है कि यह उल्लेख करने लायक है कि जेडएफ 2 में नया माइमपार्ट और माइममेजेज अब ज़ेंड \ माइम \ पार्ट और ज़ेंड \ माइम \ संदेश में हैं - इसलिए आपको तदनुसार उनके नामस्थानों का उपयोग करना होगा। –

संबंधित मुद्दे