Embedded Image in HTML email

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Da_Elf
Forum Commoner
Posts: 81
Joined: Mon Dec 29, 2008 12:31 pm

Embedded Image in HTML email

Post by Da_Elf »

Im trying to do an emailer which would send a newsletter. I want to be able to replace the URL of the header logo with an embedded attached image so that people wont always have to click the "download from server" option when my emails come in. so far ive got the image into the email as a variable but dont know what to do with it in HTML. here is a simplified version

Code: Select all

<?php
$to = "email@emailaddress.com";
$subject = "a title";
$random_hash = md5(date('r', time()));
$headers = "From: some@addy.com\r\nReply-To: some@addy.com";
$headers .= "\r\nContent-Type: multipart/alternative; boundary=\"PHP-alt-".$random_hash."\""; 
$file =	file_get_contents("http://www.somewhere.com/sml_image.jpg");
ob_start(); 
?>
--PHP-alt-<?php echo $random_hash; ?>  
Content-Type: text/html; charset="iso-8859-1" 
Content-Transfer-Encoding: 7bit

<table border="2" bordercolor="#000066" style="border-collapse:collapse" cellpadding="0" cellspacing="0" width="700" height="auto">
<tr>
 <td>****place the image here****</td>
</tr>
<tr>
  <td>***content***</td>
</tr>
</table>

--PHP-alt-<?php echo $random_hash; ?>--
<?
$message = ob_get_clean();
$mail_sent = @mail( $to, $subject, $message, $headers );
echo $mail_sent ? "Mail sent " : "Mail failed";
?>
the closest ive come to finding anything about converting it back to an image is

Code: Select all

<?php
header ('Content-Type: image/jpg'); 
imagejpeg(imagecreatefromstring($file), null,100);
?>
but i still dont know what to do with it.
Da_Elf
Forum Commoner
Posts: 81
Joined: Mon Dec 29, 2008 12:31 pm

Re: Embedded Image in HTML email

Post by Da_Elf »

never mind. i guess its just best to put it in as a direct link which means the receiver will have to click to download the online image
adampalmer
Forum Newbie
Posts: 2
Joined: Sat Jul 17, 2010 6:12 pm

Re: Embedded Image in HTML email

Post by adampalmer »

You can do it with PHPMailer:

Code: Select all

$mail->AddEmbeddedImage()
Post Reply