PHP 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
sleepydad
Forum Commoner
Posts: 75
Joined: Thu Feb 21, 2008 2:16 pm

PHP HTML email

Post by sleepydad »

Sorry if this has been hashed and re-hashed. I looked in previous posts and didn't see the topic covered. I'm new to PHP, and am experimenting with a snippet of code that I googled. I want to send a PHP generated, HTML formatted email. The code that I have is ...

<?php
$to = "xx@ccc.com";
$subject = "My HTML email test";
$headers = "From: xx@ccc.com\r\n";
$headers .= "Reply-To: xx@ccc.com\r\n";
$headers .= "Return-Path: xx@ccc.com\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";

$message = "<html><body>";
$message .= "<table><tr><td>";
$message .= "hello<br>";
$message .= "<img src='image.jpg'>";
$message .= "</td></tr></table>";
$message .= "</body></html>";

if ( mail($to,$subject,$message,$headers) ) {
echo "The email has been sent!";
} else {
echo "The email has failed!";
}
?>

I've filled in the headers with appropriate information and sent the email to myself. The table constructed fine and the word "hello" appeared, but I get a broken link icon where the image should be. I've tripled checked the image file name and compared it to what I have in my code. I've tried putting the image in it's own folder and directing the script there. Nothing's worked.

Suggestions?

Thanks in advance -
sleepydad
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

Re: PHP HTML email

Post by Jade »

With emails you have to provide the whole URL to the image. When someone goes to look at the email and it doesn't have the full URL then it doesn't know where to find it.
konrad
Forum Newbie
Posts: 3
Joined: Tue May 20, 2008 4:10 pm

Re: PHP HTML email

Post by konrad »

If you really want to make a script by yourself read this tutorial http://www.phpeveryday.com/articles/PHP ... -P113.html.
However it would be much easier to use one of freely available scripts:
http://sourceforge.net/projects/phpmailer/, http://pear.php.net/package/Mail,
http://framework.zend.com/manual/en/zend.mail.html etc.
sleepydad
Forum Commoner
Posts: 75
Joined: Thu Feb 21, 2008 2:16 pm

Re: PHP HTML email

Post by sleepydad »

Thanks for the replies! I tried specifying the URL as one of you suggested, but am getting the same results. I'll take a look at the second reply and see if there's anything there that might help me out. Again, thanks to both.

sleepydad
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

Re: PHP HTML email

Post by Jade »

What email client are you using? I know some of them prevent images from showing for safety/virus sake unless you setup the account to allow for downloads or displaying images.
Post Reply