<br>
如果不含圖檔:<br>
\
$mail
=
new
html_mime_mail();<br>
\
$html
=\"HTML內容\";<br>
\
$mail
->add_html(\
$html
, \
$text
);<br>
\
$mail
->build_message();<br>
\
$mail
->send(收信人,收信Email,發信人,發信Email,標題,額外Header);<br>
<br>
<hr><br>
如果含圖檔:<br>
\
$url
=\
"www.cuti.com.my/AD/\"; //圖檔位?FONT color="
#007700
">} ?FONT color="
#007700">}頭無http:// 結尾有斜線<br>
\
$path
=\"/MAIL/AD/\";
<br>
\
$filename1
=
'a.jpg'
;<br>
\
$backgrnd1
=
fread
(\
$fp
=
fopen
(\
$path
.\
$filename1
,
'r'
),
filesize
(\
$path
.\
$filename1
));<br>
fclose(\
$fp
);<br>
...依此類推...<br>
\
$filename6
=
'f.gif'
;<br>
\
$backgrnd6
=
fread
(\
$fp
=
fopen
(\
$path
.\
$filename6
,
'r'
),
filesize
(\
$path
.\
$filename6
));<br>
fclose(\
$fp
);<br>
<br>
\
$mail
->add_html_image(\
$backgrnd1
,\
$filename1
,
'image/jpeg'
,
'http://'
.\
$url
.\
$filename1
);<br>
...依此類推...<br>
\
$mail
->add_html_image(\
$backgrnd6
,\
$filename6
,
'image/gif'
,
'http://'
.\
$url
.\
$filename6
);<br>
<br>
\
$mail
=
new
html_mime_mail();<br>
\
$html
=\"HTML含圖內容\";<br>
\
$mail
->add_html(\
$html
, \
$text
);<br>
\
$mail
->build_message();<br>
\
$mail
->send(收信人,收信Email,發信人,發信Email,標題,額外Header);<br>
";<br>
<br>
}<br>
<br>
<br>
class
html_mime_mail{<br>
<br>
var
$headers
;<br>
var
$body
;<br>
var
$multipart
;<br>
var
$mime
;<br>
var
$html
;<br>
var
$html_text
;<br>
var
$html_images
=
array
();<br>
var
$cids
=
array
();<br>
var
$do_html
;<br>
var
$parts
=
array
();<br>
<br>
<br>
function
html_mime_mail(
$headers
= '){<br>
$this
->headers =
$headers
;<br>
}<br>
<br>
<br>
function
add_html(
$html
,
$text
){<br>
$this
->do_html = 1;<br>
$this
->html =
$html
;<br>
$this
->html_text =
$text
;<br>
if
(
is_array
(
$this
->html_images) AND
count
(
$this
->html_images) > 0){<br>
<br>
}<br>
}<br>
<br>
<br>
function
build_html(
$orig_boundary
){<br>
$sec_boundary
=
'=_'
.md5(uniqid(time()));<br>
$thr_boundary
=
'=_'
.md5(uniqid(time()));<br>
<br>
if
(
count
(
$this
->html_images) == 0){<br>
$this
->multipart.=
'--'
.
$orig_boundary
.
"\n"
;<br>
$this
->multipart.=
'Content-Type: multipart/alternative;'
.
chr
(10).
chr
(9).
'boundary="'
.
$sec_boundary
. "\
"\n\n\n"
;<br>
<br>
$this
->multipart.=
'--'
.
$thr_boundary
.
"\n"
;<br>
$this
->multipart.=
'Content-Type: text/plain'
.
"\n\n"
;<br>
$this
->multipart.=
$this
->html_text.
"\n\n"
;<br>
$this
->multipart.=
'--'
.
$thr_boundary
.
"--\n\n"
;<br>
$this
->multipart.=
'--'
.
$sec_boundary
.
"\n"
;<br>
$this
->multipart.=
'Content-Type: text/plain'
.
"\n"
;<br>
$this
->multipart.=
'Content-Transfer-Encoding: base64'
.
"\n\n"
;<br>
$this
->multipart.=
chunk_split
(
base64_encode
(
$this
->html_text)).
"\n\n"
;<br>
$this
->multipart.=
'--'
.
$thr_boundary
.
"\n"
;<br>
$this
->multipart.=
'Content-Type: text/html'
.
"\n\n"
;<br>
$this
->multipart.=
$this
->html.
"\n\n"
;<br>
$this
->multipart.=
'--'
.
$thr_boundary
.
"--\n\n"
;<br>
<br>
$this
->multipart.=
'--'
.
$sec_boundary
.
"\n"
;<br>
$this
->multipart.=
'Content-Type: text/html'
.
"\n"
;<br>
$this
->multipart.=
'Content-Transfer-Encoding: base64'
.
"\n\n"
;<br>
$this
->multipart.=
chunk_split
(
base64_encode
(
$this
->html)).
"\n\n"
;<br>
$this
->multipart.=
'--'
.
$sec_boundary
.
"--\n\n"
;<br>
}
else
{<br>
$this
->multipart.=
'--'
.
$orig_boundary
.
"\n"
;<br>
$this
->multipart.=
'Content-Type: multipart/related;'
.
chr
(10).
chr
(9).
'boundary="'
.
$sec_boundary
. "\
"\n\n\n"
;<br>
<br>
$this
->multipart.=
'--'
.
$sec_boundary
.
"\n"
;<br>
$this
->multipart.=
'Content-Type: multipart/alternative;'
.
chr
(10).
chr
(9).
'boundary="'
.
$thr_boundary
. "\
"\n\n\n"
;<br>
<br>
$this
->multipart.=
'--'
.
$thr_boundary
.
"\n"
;<br>
$this
->multipart.=
'Content-Type: text/plain'
.
"\n"
;<br>
$this
->multipart.=
'Content-Transfer-Encoding: base64'
.
"\n\n"
;<br>
$this
->multipart.=
chunk_split
(
base64_encode
(
$this
->html_text)).
"\n\n"
;<br>
<br>
$this
->multipart.=
'--'
.
$thr_boundary
.
"\n"
;<br>
$this
->multipart.=
'Content-Type: text/html'
.
"\n\n"
;<br>
$this
->multipart.=
$this
->html.
"\n\n"
;<br>
$this
->multipart.=
'--'
.
$thr_boundary
.
"--\n\n"
;<br>
<br>
$this
->multipart.=
'--'
.
$thr_boundary
.
"\n"
;<br>
$this
->multipart.=
'Content-Type: text/html'
.
"\n"
;<br>
$this
->multipart.=
'Content-Transfer-Encoding: base64'
.
"\n\n"
;<br>
$this
->multipart.=
chunk_split
(
base64_encode
(
$this
->html)).
"\n\n"
;<br>
$this
->multipart.=
'--'
.
$thr_boundary
.
"--\n\n"
;<br>
<br>
for
(
$i
=0;
$i
<
count
>html_images);
$i
++){<br>
$this
->multipart.=
'--'
.
$sec_boundary
.
"\n"
;<br>
$this
->build_html_image(
$i
);<br>
}<br>
<br>
$this
->multipart.=
"--"
.
$sec_boundary
.
"--\n\n"
;<br>
}<br>
}<br>
<br>
function
add_html_image(
$file
,
$name
=
', $c_type= '
application/octet-stream
', $location= '
){<br>
$this
->html_images[] =
array
(
'body'
=>
$file
,<br>
'name'
=>
$name
,<br>
'c_type'
=>
$c_type
,<br>
'location'
=>
$location
,<br>
'cid'
=> md5(uniqid(time())) );<br>
}<br>
<br>
<br>
<br>
function
add_attachment(
$file
,
$name
=
', $c_type= '
application/octet-stream'){<br>
$this
->parts[] =
array
(
'body'
=>
$file
,<br>
'name'
=>
$name
,<br>
'c_type'
=>
$c_type
);<br>
}<br>
<br>
<br>
function
build_html_image(
$i
){<br>
$this
->multipart.=
'Content-Type: '
.
$this
->html_images[
$i
][
'c_type'
];<br>
<br>
if
(
$this
->html_images[
$i
][
'name'
] !=
') $this->multipart .= '
; name=
"'.$this->html_images[$i][ 'name']. "
\
"\n"
;<br>
else
$this
->multipart .=
"\n"
;<br>
<br>
$this
->multipart.=
'Content-Transfer-Encoding: base64'
.
"\n"
;<br>
$this
->multipart.=
'Content-Location:'
.
$this
->html_images[
$i
][
'location'
].
"\n"
;<br>
$this
->multipart.=
'Content-ID: html_images[$i][ '
cid'].
">\n\n"
;<br>
$this
->multipart.=
chunk_split
(
base64_encode
(
$this
->html_images[
$i
][
'body'
])).
"\n"
;<br>
}<br>
<br>
<br>
function
build_part(
$i
){<br>
$message_part
= ';<br>
$message_part
.=
'Content-Type: '
.
$this
->parts[
$i
][
'c_type'
];<br>
if
(
$this
->parts[
$i
][
'name'
] != ')<br>
$message_part
.=
'; name="'
.
$this
->parts[
$i
][
'name'
]. "\
"\n"
;<br>
else
<br>
$message_part
.=
"\n"
;<br>
<br>
if
(
$this
->parts[
$i
][
'c_type'
] ==
'text/plain'
){<br>
$message_part
.=
'Content-Transfer-Encoding: base64'
.
"\n\n"
;<br>
$message_part
.=
chunk_split
(
base64_encode
(
$this
->parts[
$i
][
'body'
])).
"\n"
;<br>
}
else
{<br>
$message_part
.=
'Content-Transfer-Encoding: base64'
.
"\n"
;<br>
$message_part
.=
'Content-Disposition: attachment; filename="'
.
$this
->parts[
$i
][
'name'
]. "\
"\n\n"
;<br>
$message_part
.=
chunk_split
(
base64_encode
(
$this
->parts[
$i
][
'body'
])).
"\n"
;<br>
}<br>
<br>
return
$message_part
;<br>
}<br>
<br>
<br>
function
build_message(){<br>
$boundary
=
'=_'
.md5(uniqid(time()));<br>
<br>
$this
->headers.=
"MIME-Version: 1.0\n"
;<br>
$this
->headers.=
"Content-Type: multipart/mixed;"
.
chr
(10).
chr
(9).
"boundary=\""
.
$boundary
.
"\"\n"
;<br>
$this
->multipart = ';<br>
<br>
if
(isset(
$this
->do_html) AND
$this
->do_html == 1)
$this
->build_html(
$boundary
);<br>
if
(isset(
$this
->body) AND
$this
->body !=
') $this->parts[] = array( '
body
' => $this->body, '
name
' => '
,
'c_type'
=>
'text/plain'
);<br>
<br>
for
(
$i
=(
count
(
$this
->parts)-1);
$i
>=0;
$i
--){<br>
$this
->multipart.=
'--'
.
$boundary
.
"\n"
.
$this
->build_part(
$i
);<br>
}<br>
<br>
$this
->mime =
$this
->multipart.
"--"
.
$boundary
.
"--\n"
;<br>
}<br>
<br>
<br>
function
send(
$to_name
,
$to_addr
,
$from_name
,
$from_addr
,
$subject
=
', $headers = '
){<br>
<br>
if
(
$to_name
!=
') $to = '
"'.$to_name. '"
';<br>
else
$to
=
$to_addr
;<br>
<br>
if
(
$from_name
!=
') $from = '
"'.$from_name. '"
';<br>
else
$from
=
$from_addr
;<br>
<br>
mail(
$to
,
$subject
,
$this
->mime,
'From: '
.
$from
.
"\n"
.
$headers
.
"\n"
.
$this
->headers);<br>
}<br>
<br>
<br>
function
smtp_send(
$smtp_obj
,
$from_addr
,
$to_addr
,
$subject
){<br>
global
$
$smtp_obj
;<br>
$smtp_obj
= $
$smtp_obj
;<br>
<br>
$this
->headers .=
'From: '
.
$from_addr
.
"\n"
;<br>
$this
->headers .=
'Subject: '
.
$subject
.
"\n"
;<br>
<br>
if
(
substr
(
$this
->headers, -2) ==
"\n"
)
$this
->headers =
substr
(
$this
->headers,0,-2);<br>
$this
->headers =
explode
(
"\n"
,
$this
->headers);<br>
<br>
$smtp_obj
->sendmessage(
$from_addr
,
$to_addr
,
$this
->headers,
$this
->mime);<br>
}<br>
<br>
}
?><br>
</
count
></
count
></sascha></tobias></richard.heyes>