如何用文本/純文本,文本/ 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'].'"';
要:
謝謝你。但是當我用這段代碼發送電子郵件到gmail地址時,gmail在電子郵件頁面顯示文本/純文本類型。 –
是的,我也注意到了。不知何故,首選的替代方案應該在數組中最後。我編輯了答案。 – tihe
@tihe你能否提供一種發送多個附件的方法? – GBRocks