用zend_mail发送邮件(带附件)

以magento为例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
$to_email = 'to@test.com';
$to_name = 'test';
$subject = 'Packagedshipping rules Repeat';
$Body= "Results:ssdfsf";
$sender_email = 'from@test.com';
$sender_email = 'from';
//$storeId = Mage::app()->getStore()->getStoreId();
//$sender_email = Mage::getStoreConfig('trans_email/ident_general/email', $storeId);

//$sender_name = Mage::getStoreConfig('trans_email/ident_general/name', $storeId);

$mail = new Zend_Mail('UTF-8');
$mail->setBodyHtml($Body); //for sending message containing html code
$mail->setFrom($sender_email, $sender_name);
$mail->addTo($to_email, $to_name);
//$mail->addCc($cc, $ccname); //can set cc
//$mail->addBCc($bcc, $bccname); //can set bcc
$mail->setSubject($subject);
//添加附件
//a,xml
$at = new Zend_Mime_Part($content);
$at->type = 'application/xml';
$at->disposition = Zend_Mime::DISPOSITION_INLINE;
$at->encoding = Zend_Mime::ENCODING_BASE64;
$at->filename = date('Y-m-d').'.xml';
$mail->addAttachment($at);
//b,csv
$at = new Zend_Mime_Part($content);

$at->type = 'application/csv'; // if u have PDF then it would like -> 'application/pdf'
$at->disposition = Zend_Mime::DISPOSITION_INLINE;
$at->encoding = Zend_Mime::ENCODING_8BIT;
$at->filename = $filename;
$mail->addAttachment($at);
//c,pdf
$pdf = $this->createPdf();
// attach PDF and set MIME type, encoding and disposition data to handle PDF files
$attachment = $mail->createAttachment($pdf);
$attachment->type = 'application/pdf';
$attachment->disposition = Zend_Mime::DISPOSITION_ATTACHMENT;
$attachment->encoding = Zend_Mime::ENCODING_BASE64;
$attachment->filename = 'document.pdf';
//d,gif
$at = new Zend_Mime_Part($myImage);
$at->type = 'image/gif';
$at->disposition = Zend_Mime::DISPOSITION_INLINE;
$at->encoding = Zend_Mime::ENCODING_BASE64;
$at->filename = 'test.gif';

$mail->addAttachment($at);

try {
if($mail->send())
{
echo "send ok ! \r\n";
}
}
catch(Exception $ex) {
echo $ex->getMessage();
}

 

坚持原创技术分享,您的支持将鼓励我继续创作!