mail函数发送附件

Mail函数发送附件代码如下

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
<?php

$from = "[email protected]";
$to = "[email protected]";
$subject = "标题";
$subject = "=?UTF-8?B?".base64_encode($subject)."?=";
$path = "附件路径";
$attach_filename = "附件名";

$emailBody = "请查看附件";

# 然后我们要作为附件的HTML文件
$attachment = file_get_contents($path);

$boundary = uniqid("");

$headers = "From: $from
To: $to
Content-type: multipart/mixed; boundary="$boundary"";

$emailBody = "--$boundary
Content-type: text/plain; charset=utf-8
Content-transfer-encoding: 8bit

$emailBody

--$boundary
Content-type: text/html; name=$attach_filename
Content-disposition: inline; filename=$attach_filename
Content-transfer-encoding: 8bit

$attachment

--$boundary--";

mail("[email protected]", $subject, $emailBody, $headers);