Hi all,

I have a function which sends html emails, and inserts an image inline in the body of the email, whereever I want it to be.

So far so good..

My question is, how can I insert all the images I want in the email, not just one?

The code for inserting the one image is like this:

function mail_img_kvittering($to, $from, $subject, $body)
{
$boundary = md5(rand());

$headers = array(
    "MIME-Version: 1.0",
    "Content-Type: multipart/mixed; boundary=\"{$boundary}\"",
    "From: my@from-email.dk"
);  

$message = array(
    "--{$boundary}",

    "Content-Type: image/gif; name=\"mail-top-image.gif\"",
    "Content-Transfer-Encoding: base64",
    "Content-Disposition: inline; filename=\"mail-top-image.gif\"",
    "",
    chunk_split(base64_encode(file_get_contents("css/mail-top-image.gif"))),        

    "--{$boundary}",

    "Content-Type: text/html; charset=UTF-8",
    "Content-Transfer-Encoding: 7bit",
    "",
    $body,

    "--{$boundary}--"
);

mail($to, $subject, implode("\r\n", $message), implode("\r\n", $headers));
}

In the message array, do I simply add another boundary for each image i want to insert in the $body, which contains the email message, and is where I insert the image?

"--{$boundary}",

    "Content-Type: image/gif; name=\"img1.gif\"",
    "Content-Transfer-Encoding: base64",
    "Content-Disposition: inline; filename=\"img1.gif\"",
    "",
    chunk_split(base64_encode(file_get_contents("css/img1.gif"))),      

"--{$boundary}",

"--{$boundary}",

    "Content-Type: image/gif; name=\"img2.gif\"",
    "Content-Transfer-Encoding: base64",
    "Content-Disposition: inline; filename=\"img2.gif\"",
    "",
    chunk_split(base64_encode(file_get_contents("css/img2.gif"))),      

"--{$boundary}",

"--{$boundary}",

    "Content-Type: image/gif; name=\"img3.gif\"",
    "Content-Transfer-Encoding: base64",
    "Content-Disposition: inline; filename=\"img3.gif\"",
    "",
    chunk_split(base64_encode(file_get_contents("css/img3.gif"))),      

"--{$boundary}",

And so on...And then call them by <img src tag wherever I need it in the email body??

Best, Klemme

Member Avatar for iamthwee

Wouldn't it be better to host the images on your webserver then add a link to them in your email?

Is this not the standard practice.

hai klemme,

i agree with what iamthwee said is right, because while sending the image with mail not required to get the contents of the image file instead of that provide the full path of the image which is resided in your webserver like as follow

<img src="http://www.yourservername.com/dir_ofImages/image_path"/>

so you need to add this in the body of youe mail code.

i am sure this will work because i too faced same situation.in that time i got this idea and works fine now..

this is code what i used in my requirement

 $to = "to_address"; 
 $subject = "sample subject goes here";
 $headers =  'MIME-Version: 1.0' . "\r\n";
 $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
 $headers .= 'From: from_address_here '."\r\n";
 body = "<img src='http://www.yourservername.com/dir_ofImages/image_path'/>";
 mail($to,$subject,$body,$headers);

thats it

let me know if you have any doubts in my clarification

happy coindg

any comments are appreciated......

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.