首页/Home PHP Common Programming PHP发送带图片的邮件 (Send image email)

PHP发送带图片的邮件 (Send image email)

PrintE-mail
Monday, 21 January 2008 17:00  

此类可用于发送嵌入图片的邮件,而不是像常见的那样要请求下载图片。
The following code is used to send inset image email, instead of requiring you choose to download the images.

<?php
/**
* @name multipart mail sending class
* @author Shelley Shyan
* @Desc Simple class using php mail function to construct and
*
send mime multipart emails (i.e. emails with attachments)
*
and support content-id style embedded images in html messages.
* Inspired by code found at www.php.net, thanks to all
* Tested under PHP 5.2
*/

class multipartmail{
var
$header;
var
$parts;
var
$message;
var
$subject;
var
$To_address;
var
$Cc_address;
var
$boundary;

function
multipartmail($to, $from, $mail_subject, $Cc = ""){
$this->To_address = $to;
$this->Cc_address = $Cc;
$this->subject = $mail_subject;
$this->parts = array("");
$this->boundary = "------------" . md5(uniqid(time()));
$this->header = "From: $from\r\n";

if (!empty(
$this->Cc_address))
$this->header .= "Cc: " . $this->Cc_address . "\r\n";

$this->header .= "MIME-Version: 1.0\r\n" .
"Content-Type: multipart/related;\n" .
" boundary=\"" . $this->boundary . "\"\r\n" .
"X-Mailer: PHP/" . phpversion();
}

function
addmessage($msg = "", $ctype = "text/plain"){
$this->parts[0] = "Content-Type: $ctype; charset=utf-8\r\n" .
"Content-Transfer-Encoding: 7bit\r\n" .
"\n" . $msg;
//chunk_split($msg, 68, "\n");
}

function
addattachment($file, $ctype){
$fname = substr(strrchr($file, "/"), 1);
$data = file_get_contents($file);
$i = count($this->parts);
$content_id = "part$i." . sprintf("%09d", crc32($fname))
.
strrchr($this->To_address, "@");
$this->parts[$i] = "Content-Type: $ctype; name=\"$fname\"\r\n" .
"Content-Transfer-Encoding: base64\r\n" .
"Content-ID: <$content_id>\r\n" .
"Content-Disposition: inline;\n" .
" filename=\"$fname\"\r\n" .
"\n" .
chunk_split( base64_encode($data), 68, "\n");
return
$content_id;
}

function
buildmessage(){
$this->message = "This is a multipart message in mime format.\n";
$cnt = count($this->parts);
for(
$i=0; $i<$cnt; $i++){
$this->message .= "--" . $this->boundary . "\n" .
$this->parts[$i];
}
}

/* to get the message body as a string */
function getmessage(){
$this->buildmessage();
return
$this->message;
}

function
sendmail(){
$this->buildmessage();
mail($this->To_address,$this->subject,$this->message,$this->header);
}
}

$mulmail = new multipartmail("tomail@mail.com", "fro@mail.com", "PHP Mail Test");
$cid = $mulmail->addattachment("/images/name.gif", "image/gif");
$mulmail->addmessage(
"<html>\r\n
<head>\r\n
<style type='text/css'>\n
body{ color: #222222;
font-family: Verdana;}\n
td{ color: #0000FF;
font-size: 12px;
font-family: Verdana;
text-align: center;}\n
</style>\n
</head>\n
<body>\n
<p>Welcome <br /><img src=\"cid:$cid\"></p>
<p>This a test with CSS and image using php's mail function.</p>
<p class='end'>Regards, <br />Shelley Shyan</p>\n
</body>\n
</html>\n"
, "text/html");

$mulmail->sendmail();
?>

Hope it make sense.