Including images in mail()<

If I send an automated email with mail(), how do I include images in the email itself?I looked around and found this code on php.net


<?php
/* recipients */
$to = "[email protected]" . ", " ; // note the comma
$to .= "[email protected]";

/* subject */
$subject = "Birthday Reminders for August";

/* message */
$message = '
<html>
<head>
<title>Birthday Reminders for August</title>
</head>
<body>
<p>Here are the birthdays upcoming in August!</p>
<table>
<tr>
<th>Person</th><th>Day</th><th>Month</th><th>Year</th>
</tr>
<tr>
<td>Joe</td><td>3rd</td><td>August</td><td>1970</td>
</tr>
<tr>
<td>Sally</td><td>17th</td><td>August</td><td>1973</td>
</tr>
</table>
</body>
</html>
';

/* To send HTML mail, you can set the Content-type header. */
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";

/* additional headers */
$headers .= "To: Mary <[email protected]>, Kelly <[email protected]>\r\n";
$headers .= "From: Birthday Reminder <[email protected]>\r\n";
$headers .= "Cc: <!-- e --><a href="mailto:[email protected]">[email protected]</a><!-- e -->\r\n";
$headers .= "Bcc: <!-- e --><a href="mailto:[email protected]">[email protected]</a><!-- e -->\r\n";

/* and now mail it */
mail($to, $subject, $message, $headers);
?>


Why not do it kinda like that, using html in the $message and then using the image tag in there? Would that work? I'm sure you wouldn't have to do quite that much, but it's an idea.you could use the image tag to display an image hosted elsewhere...


or are you after simply attaching a file to the email?

you need to set the mime type of the email to accomodate the file being attached:


<?php
$content.= "Content-Type: " . mime_content_type ( $filelocation ) . ";";
?>


also read the user notes at <!-- w --><a class="postlink" href="http://www.php.net">www.php.net</a><!-- w --> for the mail (<!-- m --><a class="postlink" href="http://uk.php.net/function.mail">http://uk.php.net/function.mail</a><!-- m -->) function - in particular the user note from j [at] jgdataworks.comI tried sending an email with an image in between two of my email accounts - this is part of the source:


</body>
</html>

--------------030103010201050908000802
Content-Type: image/gif;
name="pizzarola.gif"
Content-Transfer-Encoding: base64
Content-ID: <[email protected]>
Content-Disposition: inline;
filename="pizzarola.gif"

R0lGODlhJgJfAdUAAKGVZ+7v+5xjTd7PqKqqmc21j3hwScqZb+fo73RwNiAkK2cvKdrb3N3d
6XRvanyPke/3/HROMUxRT/jzxHlNRJiJP1RMLrnIzN3o7ISCWcrLx9rMdldmaTNJUba4vWN6
lPTeUFqCZt...

...lots and lots of ASCII...

...EtqABTTtcOqZ2fAUt/hgYfTr1d6hBujd+miIYKqlUlhrIaEm2Y6soEnlZKklyw7MEsEE3N5o
AhTSkFGZpNZrG45ThkFYrd5aIQp4SqAiQgWoHs8UIqC0gOHSImX+rBCY6NnwpC+UoeR6In9G
wDkRAYEmBeZOGudOJgEw7HAqkVZBzOaabnVKnVd0yBs27su+bl96qfMIz3JkZuHerh2iJ5CQ
SQ3AQ0L+bkgSQFuEWiuoQ40erzfmx7wsr/E+bz2igKWorhEqQxAAADs=
--------------030103010201050908000802--


So do I just add the image file's binary into the headers (in base64 format)?dunno - did you read those notes at php.net?Yeah, but the script scares me. :)

Although there's this in the notes for base64_encode:

<?
// ...
echo '<img src=http://www.htmlforums.com/archive/index.php/"data:image/png;base64,'.$encoded.' ">';
?>


Which is close to what was in the test email I sent:

<img alt="" src=http://www.htmlforums.com/archive/index.php/"cid:p[email protected]" align="left"
border="0" height="351" width="550">

---

Content-Type: image/gif;
name="pizzarola.gif"
Content-Transfer-Encoding: base64
Content-ID: <[email protected]>
Content-Disposition: inline;
filename="pizzarola.gif"why does that script scare you?He's posted two comments, I followed the link to the 13k one (<!-- m --><a class="postlink" href="http://www.jgdataworks.com/OSS/MIMEMail.class.phps">http://www.jgdataworks.com/OSS/MIMEMail.class.phps</a><!-- m -->). :o
That one looks like just what I want, though. I'll try it out.
 
Back
Top