在ZF2中发送带有附件的电子邮件

时间:2013-06-12 12:55:39

标签: php zend-framework2 email-attachments

如何使用text / plain,text / html和zf2中的附件发送电子邮件? 我使用此代码发送带有smtp的电子邮件:

$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' => 'user@myserver.com',
        '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('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

任何帮助?

4 个答案:

答案 0 :(得分:13)

目前在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 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方法会自动执行此操作。

答案 1 :(得分:1)

我发现它是一个更好的解决方案,所以我正在写它。

    INFO   | jvm 1    | 2016/03/08 13:37:50 | Heap
INFO   | jvm 1    | 2016/03/08 13:37:50 |  def new generation   total 135360K, used 89556K [0x042f0000, 0x0d5d0000, 0x0ed90000)
INFO   | jvm 1    | 2016/03/08 13:37:50 |   eden space 120320K,  74% used [0x042f0000, 0x09a5ad58, 0x0b870000)
INFO   | jvm 1    | 2016/03/08 13:37:50 |   from space 15040K,   0% used [0x0c720000, 0x0c72a3f8, 0x0d5d0000)
INFO   | jvm 1    | 2016/03/08 13:37:50 |   to   space 15040K,   0% used [0x0b870000, 0x0b870000, 0x0c720000)
INFO   | jvm 1    | 2016/03/08 13:37:50 |  tenured generation   total 300224K, used 89569K [0x0ed90000, 0x212c0000, 0x242f0000)
INFO   | jvm 1    | 2016/03/08 13:37:50 |    the space 300224K,  29% used [0x0ed90000, 0x14508780, 0x14508800, 0x212c0000)
INFO   | jvm 1    | 2016/03/08 13:37:50 |  compacting perm gen  total 42496K, used 42446K [0x242f0000, 0x26c70000, 0x282f0000)
INFO   | jvm 1    | 2016/03/08 13:37:50 |    the space 42496K,  99% used [0x242f0000, 0x26c63ad8, 0x26c63c00, 0x26c70000)
INFO   | jvm 1    | 2016/03/08 13:37:50 | No shared spaces configured.
INFO   | jvm 1    | 2016/03/08 13:37:50 | 
INFO   | jvm 1    | 2016/03/08 13:37:52 | 2016-03-08 13:37:52,692 [task-scheduler-3]  INFO BULKPAYMENT_LOGGER:89 - com.bcsis.g3.bulkpayment.stpcount.service.STPCountFileAdapterService:Temp cnt file deleted:true
ERROR  | wrapper  | 2016/03/08 13:37:53 | JVM did not exit on request, terminated
STATUS | wrapper  | 2016/03/08 13:37:57 | Launching a JVM...
INFO   | jvm 2    | 2016/03/08 13:37:58 | Listening for transport dt_socket at address: 8000
INFO   | jvm 2    | 2016/03/08 13:37:58 | Wrapper (Version 3.2.0) http://wrapper.tanukisoftware.org

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

答案 2 :(得分:0)

设置类型:

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

要:

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

您还需要确认,如果您使用的SMTP服务允许通过协议附件。

答案 3 :(得分:-3)

带附件的电子邮件

$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);

如果您想要更多地控制为此附件生成的MIME部分,可以使用createAttachment()的返回值来修改其属性。 createAttachment()方法返回Zend \ Mime \ Part对象:

$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 \ Mime \ Part的实例并使用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