2013-06-12 220 views
6

如何用文本/純文本,文本/ html發送電子郵件並附加在zf2中? 我使用此代碼使用SMTP發送電子郵件:用ZF2發送帶有附件的電子郵件

$files = $this->params()->fromFiles(); 
$smtp = new \Zend\Mail\Transport\Smtp(); 
$smtp->setAutoDisconnect(true); 
$optn = new \Zend\Mail\Transport\SmtpOptions(array(
    'host'    => 'mail.myserver.com', 
    'connection_class' => 'login', 
    'connection_config' => array(
     'username' => '[email protected]', 
     'password' => 'mypassword', 
    ), 
)); 
$smtp->setOptions($optn); 


$htmlPart = new \Zend\Mime\Part('<p>some html</p>'); 
$htmlPart->type = Mime::TYPE_HTML; 

$textPart = new \Zend\Mime\Part('some text'); 
$textPart->type = Mime::TYPE_TEXT; 

$i=0; 
$attaches = array(); 
foreach($files as $file){ 
    if ($file['error']) 
     continue; 
    $attaches[$i] = new \Zend\Mime\Part(file_get_contents($file['tmp_name'])); 
    $attaches[$i]->type = $file['type'].'; name="'.$file['name'].'"'; 
    $attaches[$i]->encoding = 'base64'; 
    $attaches[$i]->disposition = 'attachment'; 
    $attaches[$i]->filename = $file['name']; 
    $i++; 
} 

$parts = array(); 
if (count($attaches)>0) { 
    $parts = array_merge(array($textPart,$htmlPart),$attaches); 
    $type = Mime::MULTIPART_MIXED; 
} 
else{ 
    $parts = array($textPart, $htmlPart); 
    $type = Mime::MULTIPART_ALTERNATIVE ; 
} 
$body = new \Zend\Mime\Message(); 
$body->setParts($parts); 

$message = new \Zend\Mail\Message(); 
$message->setFrom('[email protected]'); 
$message->addTo('[email protected]'); 
$message->setSubject('subject'); 
$message->setEncoding("UTF-8"); 
$message->setBody($body); 
$message->getHeaders()->get('content-type')->setType($type); 

$smtp->send($message); 

如果我附加文件,它發送的文件和內容,但它一起顯示在接收機的收件箱中平原和HTML文本:

<p>some html</p> 
some text 

當我不不附加任何文件,它單獨顯示html文本:

some html 

任何幫助嗎?

$attaches[$i]->type = $file['type'].'; name="'.$file['name'].'"'; 

要:

回答

12

目前存在ZF2沒有簡單的方法(2.2),以一個多/替代體(HTML文本替代結合起來,客戶端不能/ DO-不-W螞蟻使用HTML)與附件。 如果您將「multipart/alternative」內容類型標題添加到整個郵件中,則在某些電子郵件客戶端中不會顯示附件(鏈接)。

的解決方案是一分爲二的消息時,所述主體(文本和HTML)和附件:

http://jw-dev.blogspot.com.es/2013/01/zf2-zend-mail-multipartalternative-and.html

一個例子:

 $content = new MimeMessage(); 
     $htmlPart = new MimePart("<html><body><p>Sorry,</p><p>I'm going to be late today!</p></body></html>"); 
     $htmlPart->type = 'text/html'; 
     $textPart = new MimePart("Sorry, I'm going to be late today!"); 
     $textPart->type = 'text/plain'; 
     $content->setParts(array($textPart, $htmlPart)); 

     $contentPart = new MimePart($content->generateMessage());   
     $contentPart->type = 'multipart/alternative;' . PHP_EOL . ' boundary="' . $content->getMime()->boundary() . '"'; 

     $attachment = new MimePart(fopen('/path/to/test.pdf', 'r')); 
     $attachment->type = 'application/pdf'; 
     $attachment->encoding = Mime::ENCODING_BASE64; 
     $attachment->disposition = Mime::DISPOSITION_ATTACHMENT; 

     $body = new MimeMessage(); 
     $body->setParts(array($contentPart, $attachment)); 

     $message = new Message(); 
     $message->setEncoding('utf-8') 
     ->addTo('[email protected]') 
     ->addFrom('[email protected]') 
     ->setSubject('will be late') 
     ->setBody($body); 

     $transport = new SmtpTransport(); 
     $options = new SmtpOptions($transportConfig), 
     )); 

     $transport->setOptions($options); 
     $transport->send($message); 

對於上面就需要以下使用說明:

use Zend\Mail\Message; 
use Zend\Mail\Transport\Smtp as SmtpTransport; 
use Zend\Mail\Transport\SmtpOptions; 
use Zend\Mime\Mime; 
use Zend\Mime\Part as MimePart; 
use Zend\Mime\Message as MimeMessage; 

ZF1有_buildBody()方法在Zend_Mail_Transport_Abstract這是自動做到這一點。

+0

謝謝你。但是當我用這段代碼發送電子郵件到gmail地址時,gmail在電子郵件頁面顯示文本/純文本類型。 –

+0

是的,我也注意到了。不知何故,首選的替代方案應該在數組中最後。我編輯了答案。 – tihe

+0

@tihe你能否提供一種發送多個附件的方法? – GBRocks

0

從設置類型

$attaches[$i]->type = \Zend\Mime\Mime::TYPE_OCTETSTREAM; 

您還需要確認,如果你使用的是SMTP服務,它們允許通過附着物協議。如果你想在MIME部分的更多控制

-3

E-Mail郵件帶附件

$mail = new Zend\Mail\Message(); 
// build message... 
$mail->createAttachment($someBinaryString); 
$mail->createAttachment($myImage, 
         'image/gif', 
         Zend\Mime\Mime::DISPOSITION_INLINE, 
         Zend\Mime\Mime::ENCODING_BASE64); 

此附件,您可以使用createAttachment()的返回值來修改它的屬性產生。所述createAttachment()方法返回一個Zend \默\部分對象:

$mail = new Zend\Mail\Message(); 

$at = $mail->createAttachment($myImage); 
$at->type  = 'image/gif'; 
$at->disposition = Zend\Mime\Mime::DISPOSITION_INLINE; 
$at->encoding = Zend\Mime\Mime::ENCODING_BASE64; 
$at->filename = 'test.gif'; 

$mail->send(); 

另一種方法是創建的Zend \默\部分的一個實例,並用addAttachment()添加:

$mail = new Zend\Mail\Message(); 

$at = new Zend\Mime\Part($myImage); 
$at->type  = 'image/gif'; 
$at->disposition = Zend\Mime\Mime::DISPOSITION_INLINE; 
$at->encoding = Zend\Mime\Mime::ENCODING_BASE64; 
$at->filename = 'test.gif'; 

$mail->addAttachment($at); 

$mail->send(); 

Reference1 Reference2 Reference3

+1

createAttachment方法不存在。 –

+0

http://framework.zend.com/manual/2.0/en/modules/zend.mail.attachments.html – Developer

+0

@MohamadMehdiHabibi你需要看看上面的鏈接。不要只留下負面的反饋。 – Developer

1

我發現它是一個更好的解決方案,所以我寫它。

Namespace YourNamesapace; 

use Zend\Mail\Message as ZendMessage; 
use Zend\Mime\Part as MimePart; 
use Zend\Mime\Message as MimeMessage; 
use Zend\Mail\Transport\Sendmail; 
class Testmail 
{ 
    public static function sendMailWithAttachment($to, $subject, $htmlMsg, $dir, $fileName) 
    { 
     $fileFullPath = $dir . '/' . $fileName; 
     // Render content from template 
     $htmlContent = $htmlMsg; 
     // Create HTML part 
     $htmlPart = new MimePart($htmlContent); 
     $htmlPart->type = "text/html"; 
     // Create plain text part 
     $stripTagsFilter = new \Zend\Filter\StripTags(); 
     $textContent = str_ireplace(array("<br />", "<br>"), "\r\n", $htmlContent); 
     $textContent = $stripTagsFilter->filter($textContent); 
     $textPart = new MimePart($textContent); 
     $textPart->type = "text/plain"; 

     // Create separate alternative parts object 
     $alternatives = new MimeMessage(); 
     $alternatives->setParts(array($textPart, $htmlPart)); 
     $alternativesPart = new MimePart($alternatives->generateMessage()); 
     $alternativesPart->type = "multipart/alternative;\n boundary=\"".$alternatives->getMime()->boundary()."\""; 

     $body = new MimeMessage(); 
     $body->addPart($alternativesPart); 


     $attachment = new MimePart(file_get_contents($fileFullPath)); 
     $attachment->type = \Zend\Mime\Mime::TYPE_OCTETSTREAM; 
     $attachment->filename = basename($fileName); 
     $attachment->disposition = \Zend\Mime\Mime::DISPOSITION_ATTACHMENT; 
     $attachment->encoding = \Zend\Mime\Mime::ENCODING_BASE64; 
     $body->addPart($attachment); 
     // Create mail message 
     $mailMessage = new ZendMessage(); 
     $mailMessage->setFrom('[email protected]', 'from Name'); 
     $mailMessage->setTo($to); 
     $mailMessage->setSubject($subject); 
     $mailMessage->setBody($body); 
     $mailMessage->setEncoding("UTF-8"); 
     $mailMessage->getHeaders()->get('content-type')->setType('multipart/mixed'); 
     $transport = new Sendmail(); 
     $transport->send($mailMessage); 
    } 
} 

參考:http://resoftsol.com/sending-e-mail-with-alternative-parts-plus-attachments/

相關問題