Hi there, I've been trying to find an answer to this for hours and I can only find help on it in ASP.
What I am trying to do is send an email from a website with inline images attached and displayed rather than linking them.
Here is the code:
Now the email gets through OK, however, the selected image is not displayed.
Has anyone ever got some thing like this working before? I can't for the life of me work it out.
Cheers
Aaron
What I am trying to do is send an email from a website with inline images attached and displayed rather than linking them.
Here is the code:
Code:
<?php
$separator = "\r\n";
$from = "test@test.com";
$to = "test@test.biz";
$salutation = "Aaron";
$subject = "Tell a Friend";
$message = "<html><body>The enclosed files are tests.<img src=\"cid:test.gif\" /></body></html>" . $separator;
// Set the required attachments
$attachments[]["file"] = "images/test.gif";
$attachments[]["file"] = "images/footer.gif";
// Loop through the attachements array and get the filename only
reset($attachments);
while(list($k, $v) = each($attachments)) {
$attachments[$k]["name"] = basename($v["file"]);
}
// Loop through the attachments and get the filesize
reset($attachments);
while(list($k, $v) = each($attachments)) {
$attachments[$k]["size"] = filesize($v["file"]);
}
// Loop through the attachments and get the filetypes
reset($attachments);
while(list($k, $v) = each($attachments)) {
switch(substr(strrchr($v["file"], "."), 1)) {
case "gif":
$attachments[$k]["type"] = "image/gif; name=\"" . $attachments[$k]["name"] . "\"";
break;
case "jpg":
$attachments[$k]["type"] = "image/jpg; name=\"" . $attachments[$k]["name"] . "\"";
break;
case "png":
$attachments[$k]["type"] = "image/png; name=\"" . $attachments[$k]["name"] . "\"";
break;
Default:
$attachments[$k]["type"] = "application/octet-stream";
}
$attachments[$k]["encoding"] = "base64";
}
// Initialize the MIME start and ending vars
$message_prefix = "";
$message_suffix = "";
// Only do this lot if the attachment array exists, is an array, and has elements
if (isset($attachments) and is_array($attachments) and count($attachments) > 0) {
// Create the boundary field for the beginning of the attachment
list($x, $y) = explode(" ", microtime());
$boundary = "boundary--" . $y . substr($x, 2);
// Assign the MIME header to tell the email client what the message is
$add[] = "MIME-Type: 1.0";
$add[] = "Content-Type: multipart/mixed; boundary=\"" . $boundary . "\"";
// Add dashes to the start of the boundary var now, as this needs to be done to denote a new MIME header after the above
$boundary = "--" . $boundary;
// Add the MIME header to indicate that the message has text in the body
$message_prefix .= $boundary . $separator
. "Content-Type: text/html; charset=us-ascii" . $separator
. "Content-Transfer-Encodinig: 8bit" . $separator
. $separator;
// Loop through the attachments, and assign the MIME headers for each one
reset($attachments);
while(list($k, $v) = each($attachments)) {
// Open the file and read it in one chunk
$f = fopen($v["file"], "rb");
$x = fread($f, $v["size"]);
fclose($f);
// MIME headers denoting start of an attachment
$message_suffix .= $boundary . $separator
. "Content-Type: " . $v["type"] . "; name: " . $v['name'] . $separator
. "Content-Transfer-Encoding: base64" . $separator
. "Content-Disposition: inline; filename=\"" . $v["name"] . "\"" . $separator
. "Content-ID: <" . $v['name'] . ">" . $separator;
// Base encode the file, and split to managable chunks
$message_suffix .= chunk_split(base64_encode($x));
$message_suffix .= $separator;
}
// The attachments are set, so close off the MIME headers
$message_suffix .= $boundary . "--" . $separator;
}
// Build the non mime headers
if(isset($from) and strlen($from)) {
$add[] = "From: " . $from;
}
$additional = "";
if(isset($add) and is_array($add)) {
$additional = implode($separator, $add) . $separator;
}
// Send the mail!
$full_message = $message_prefix . "Dear " . $salutation . ",\n" . $message . $message_suffix;
if(mail($to, $subject, $full_message, $additional)) {
echo "Thank u for your interst to send mail";
} else {
echo "failed";
}
?>
Now the email gets through OK, however, the selected image is not displayed.
Has anyone ever got some thing like this working before? I can't for the life of me work it out.
Cheers
Aaron