php – 在ZF2中发送附带文件的电子邮件
发布时间:2020-12-13 18:01:58 所属栏目:PHP教程 来源:网络整理
导读:如何在zf2中发送带有text / plain,text / html和附件的电子邮件? 我使用此代码发送带有smtp的电子邮件: $files = $this-params()-fromFiles();$smtp = new ZendMailTransportSmtp();$smtp-setAutoDisconnect(true);$optn = new ZendMailTransportS
如何在zf2中发送带有text / plain,text / html和附件的电子邮件?
我使用此代码发送带有smtp的电子邮件: $files = $this->params()->fromFiles(); $smtp = new ZendMailTransportSmtp(); $smtp->setAutoDisconnect(true); $optn = new ZendMailTransportSmtpOptions(array( 'host' => 'mail.myserver.com','connection_class' => 'login','connection_config' => array( 'username' => 'user@myserver.com','password' => 'mypassword',),)); $smtp->setOptions($optn); $htmlPart = new ZendMimePart('<p>some html</p>'); $htmlPart->type = Mime::TYPE_HTML; $textPart = new ZendMimePart('some text'); $textPart->type = Mime::TYPE_TEXT; $i=0; $attaches = array(); foreach($files as $file){ if ($file['error']) continue; $attaches[$i] = new ZendMimePart(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 ZendMimeMessage(); $body->setParts($parts); $message = new ZendMailMessage(); $message->setFrom('user@myserver.com'); $message->addTo('receiver@myserver.com'); $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 有帮助吗?
目前在ZF2(2.2)中没有简单的方法将多部分/替代主体(html与不能/不想使用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('mywife@home.com') ->addFrom('myself@office.com') ->setSubject('will be late') ->setBody($body); $transport = new SmtpTransport(); $options = new SmtpOptions($transportConfig),)); $transport->setOptions($options); $transport->send($message); 对于上述内容,您需要以下用语: use ZendMailMessage; use ZendMailTransportSmtp as SmtpTransport; use ZendMailTransportSmtpOptions; use ZendMimeMime; use ZendMimePart as MimePart; use ZendMimeMessage as MimeMessage; ZF1在Zend_Mail_Transport_Abstract中有一个_buildBody()方法,它自动执行此操作. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |