Jump to content

Embedding GIF's and Images in a email


Go to solution Solved by mac_gyver,

Recommended Posts

Hi all,

 

I'm trying to avoid using external libraries as much as possible, mainly because I'd liek to try and understand things myself instead of relying on their code.

 

I've got an email working fine that displays in HTML courtesy of some online guides / tutorials and a fair bit of frowning.  

I now need to embed some GIF's or images into it but I'm having zero success.

 

The images are stored on the server and have been used in various other places so I'm certain the path is correct as I thought this may be the issue.

 

If I take an example from CSS-Tricks and use a http:..... path image.  It works fine but I don't want to do this, I'd like to tell it that the file is "images/bla.gif" and so on.

 

I've found some help online but after trying examples I'm still in the same position that I was before. 

 

Could someone take a look at the code and help please?

 
 
$subject = "Contact Form";
 
        $headers = "From: " .$email. "\r\n";
        $headers .= "Reply-To: " .$email. "\r\n";
        $headers .= "MIME-Version: 1.0\r\n";
        $headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
 
        //body of message
        $message1 = '<html><body>';
        $message1 .= '<table rules="all" style="border-color: #666;" cellpadding="10">';
        $message1 .= "<tr style='background: #eee;'><td><strong>Name:</strong> </td><td>" .$name. "</td></tr>";
        $message1 .= "<tr><td><strong>Email:</strong> </td><td>" .$email. "</td></tr>";
        $message1 .= "<tr><td><strong>Company Name:</strong> </td><td>" .$companyName. "</td></tr>";
        $message1 .= "<tr><td><strong>Contact Number:</strong> </td><td>" .$contactNumber. "</td></tr>";
        $message1 .= "<tr><td><strong>Message:</strong> </td><td>" .$message. "</td></tr>";
        $message1 .= "</table>";
 
        $message1 .= '<IMG SRC="images/loading.gif" alt="GAAAAAh"/>'; -- THIS DIESN"T WORK - THE ONE BELOW DOES -.-
        $message1 .= '<img src="http://css-tricks.com/examples/WebsiteChangeRequestForm/images/wcrf-header.png" alt="Website Change Request" />';
        $message1 .= "</body></html>";
 
        if (mail($myEmail, $subject, $message1, $headers)) {
 
Link to comment
https://forums.phpfreaks.com/topic/279689-embedding-gifs-and-images-in-a-email/
Share on other sites

  • Solution

the email message isn't a page on a web site (even if you are accessing it via a web interface using a browser.)

 

when you browse to a web page, relative url's on that page are take by the browser and have the current protocol/domain/page (i.e. http://domain.com) pre-pended to them, so an image that's given by SRC="images/loading.gif", results in the browser forming the url http://domain.com/images/loading.gif that it then uses to fetch the image.

 

for displaying an email in a browser, you are not on the web page where the images are stored and the browser does not know what protocol/domain/page to use to fetch the image. you must put fully-qualified-absolute urls in email messages.

Edited by mac_gyver
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.