如果您没有使用任何外部邮件库,例如 PEAR::SMTP 或 Swift,那么创建附件并不是一件简单的事情。
您必须检查他们指示的邮件标准,了解如何将标头与邮件内容以及邮件内容与附件分开。
分隔符是\r\n
,并且每个部分之前必须有一个分隔符。
检查此链接:http ://webcheatsheet.com/php/send_email_text_html_attachment.php
我的代码示例:
$uid = md5(uniqid(time()));
$header = implode("\r\n",
array(
'MIME-Version: 1.0',
'From: Address <address@example.com>',
'Reply-To: Address <address@example.com>',
'Cc: Address <address@example.com>',
'Content-Type: multipart/mixed;',
' boundary="'.$uid.'"',
'',
'--'.$uid,
'Content-Type: text/plain;',
' charset="us-ascii"',
'Content-Transfer-Encoding: 7bit',
'',
mb_convert_encoding(
implode("\r\n",
array_map('wordwrap',
array(
'Hello,',
'',
'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla laoreet varius augue non pretium. Nullam dignissim iaculis velit, ut cursus neque mollis et. Fusce faucibus dictum nunc, sit amet cursus tellus aliquam ut. Praesent id libero orci, ut vehicula orci. Proin nulla sem, dapibus in lacinia vel, pharetra eget eros. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Cras rhoncus, enim sed mollis suscipit, sapien mauris suscipit mauris, nec placerat nisi sapien ac lectus. Sed vehicula faucibus iaculis. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Integer placerat commodo nibh. Morbi sit amet augue vitae urna mattis pulvinar ac eget tellus. Donec mattis cursus posuere. Donec dolor velit, aliquam eu pretium vel, accumsan eu est. Proin at ante nisi, ut malesuada diam.',
'',
'Nulla ac urna lacus, in luctus ipsum. Nunc luctus risus non dui tincidunt in ultricies tellus rhoncus. Aliquam pellentesque vehicula adipiscing. Phasellus porttitor sodales quam et cursus. Pellentesque iaculis consectetur lorem, et consectetur sem tristique sed. Duis vehicula eleifend nisl, a semper augue lobortis cursus. Fusce ut ligula diam. Quisque fringilla sodales massa vitae rhoncus. Pellentesque a libero sed enim lacinia commodo quis et dolor. Praesent facilisis, orci nec lacinia ultrices, enim nunc imperdiet augue, at aliquet dolor augue nec nulla.',
'',
'Ut venenatis commodo pulvinar. Vestibulum quam odio, malesuada posuere porttitor facilisis, molestie vel risus. Mauris auctor fringilla ornare. Morbi eu libero orci, nec semper lorem. Quisque blandit, ante nec pharetra bibendum, felis felis tincidunt felis, sed mattis sapien lorem et risus. In sed interdum quam. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Maecenas vitae metus id purus ultrices fermentum. In dui eros, feugiat sit amet feugiat vel, accumsan vitae ligula. Nam at consectetur elit. Maecenas eget metus id nibh luctus mattis. Quisque mollis, sapien semper vulputate venenatis, purus arcu ornare orci, sit amet pellentesque enim ipsum non urna. Quisque at augue nec quam vehicula suscipit eu a dui. Morbi at molestie nunc. Mauris ultricies rutrum euismod. Sed non sem nisi. '
)
)
),
'iso-8859-1'
),
'',
'--'.$uid,
'Content-Type: application/pdf;',
' name="file.pdf"',
'Content-Transfer-Encoding: base64',
'Content-Disposition: attachment;',
' filename="file.pdf"',
'',
chunk_split(base64_encode(file_get_contents('file.pdf'))),
'--'.$uid.'--'
)
);
mail($contacts, 'Subject', '', $header);