stevengreen22 Posted June 29, 2013 Share Posted June 29, 2013 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)) { Quote Link to comment https://forums.phpfreaks.com/topic/279689-embedding-gifs-and-images-in-a-email/ Share on other sites More sharing options...
Solution mac_gyver Posted June 29, 2013 Solution Share Posted June 29, 2013 (edited) 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 June 29, 2013 by mac_gyver Quote Link to comment https://forums.phpfreaks.com/topic/279689-embedding-gifs-and-images-in-a-email/#findComment-1438512 Share on other sites More sharing options...
stevengreen22 Posted June 29, 2013 Author Share Posted June 29, 2013 That makes so much more sense now. I feel ashamed. You are now on my christmas card list Quote Link to comment https://forums.phpfreaks.com/topic/279689-embedding-gifs-and-images-in-a-email/#findComment-1438518 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.