Help - Search - Members - Calendar
Full Version: HTML in (php) email
HTMLHelp Forums > Programming > Server-side Scripting
joyful
Hey,
So, I have this php file that takes data form a html form and sends it as a email.
This works great! My form has multiple fields (message, subject, etc.) one of which is "image URL".
In this field ("Image URL") I input the url to a image. Now here is my php email file:
CODE
<?php

require('connect.php');

//change php.ini smtp line
ini_set("SMTP", "smtp.mysite.com");

//standard mail header
$headers = "From: info@mysite.com";

//get message to send

$message = $_GET['message'];
$title = $_GET['subject'];
$sign = $_GET['sign'];
$image = $_GET['image'];

//loop through
for($x=0;$x<count($_GET);$x++)
{
    if ($_GET["mail_$x"])
    {
        
        //mail setup
        $to = $_GET["mail_$x"];
        $subject = $title;
        $body = $_GET["name_$x"]."
        \n\n <img src='http://www.mysite.com/email/uploaded/$image' width='450px'> \n\n
        \n\n $message \n\n
        $sign ";
        
        mail($to, $subject, $body, $headers);
        
    }
}

echo "All mail has been processed. Cilck <a href='http://www.mysite.com/email/index.php/'>here</a> to go back to mailing list.";

?>

Now, This's works great! The only issue is: it just says: "img src='http://www.mysite.com/email/uploaded/$image' width='450px'>" (as is should).
Now, I would like to display html content in my emails (such as this image).
I tried this (same issue):
CODE
$body = $_GET["name_$x"]."
        \n\n <html><body><center><img src='http://www.mysite.com/email/uploaded/$image' width='450px'></center></body></html> \n\n
        \n\n $message \n\n
        $sign ";

I really don't know how to do this.

Also, I would like to style things with CSS (in my emails). I can add the CSS style to the top, but I don't know how to apply (as html in this email).
How can I do those things? I know the way I am doing the whole email thing is not the most standard way, but its the way I am doing it. I simply want to know how I can incorporate some css and html.

Thanks in advance.

--
joyful
Hey, Does anyone know how I could know how I could display HTML in my emails (thru this PHP file)?

Thanks, Tell me if I am not being clear about what I want to do.

--
Frederiek
Try searching for "php html email". See e.g. http://css-tricks.com/sending-nice-html-email-with-php/ .
joyful
Hey, Thanks for the reply Frederiek,

Maybe I just don't understand. The site that you said was helpful.
I tried to implement what it said, yet it still shows up as text in the email. Here is the PHP I am now using:
CODE
<?php

set_time_limit(0);

require('connect.php');

//change php.ini smtp line
ini_set("SMTP", "smtp.mysite.com");

//standard mail header
$headers = "From: info@mysite.com";

//get message to send

$body = $_GET['message'];
$title = $_GET['subject'];
$sign = $_GET['sign'];
$img = $_GET['image'];

$image = '<html><body>';
$image .= '<img src="http://www.mysite.com/email/uploaded/'.$img.'" width="450px" />';
$image .= '</body></html>';

//loop through
for($x=0;$x<count($_GET);$x++)
{
    if ($_GET["mail_$x"])
    {
        
        //mail setup
        $to = $_GET["mail_$x"];
        $subject = $title;
        $message = $_GET["name_$x"]."
        \n\n $image \n\n
        \n\n $body \n\n
        $sign ";
        
        mail($to, $subject, $message, $headers);
        
    }
}

echo "All mail has been processed. Cilck <a href='http://www.mysite.com/email/index.php/'>here</a> to go back to mailing list.";

?>


And I tried like this:

CODE

<?php

set_time_limit(0);

require('connect.php');

//change php.ini smtp line
ini_set("SMTP", "smtp.mysite.com");

//standard mail header
$headers = "From: info@mysite.com";

//get message to send

$body = $_GET['message'];
$title = $_GET['subject'];
$sign = $_GET['sign'];
$img = $_GET['image'];

//loop through
for($x=0;$x<count($_GET);$x++)
{
    if ($_GET["mail_$x"])
    {
        
        //mail setup
        $to = $_GET["mail_$x"];
        $subject = $title;
        $message = '<html><body>';
        $message .= '<img src="http://www.mysite.com/email/uploaded/'.$img.'" width="450px" />';
        $message .= '</body></html>';
        $message .= $_GET["name_$x"]."
        \n\n $body \n\n
        $sign ";
        
        mail($to, $subject, $message, $headers);
        
    }
}

echo "All mail has been processed. Cilck <a href='http://www.mysite.com/email/index.php/'>here</a> to go back to mailing list.";

?>

What is the issue? How do you do this?

Thanks!

--
Darin McGrew
You haven't set the Content-Type header, as shown on the page Frederiek linked to. Without that, the body of the email will be interpreted as plain text.
joyful
Hey,
Thanks so much Darin and Frederiek, this was exactly what I needed!!
This worked great! I only have one issue now... My variable "$main" shows up both above and below my image,. I only want it to show up below, and I can't find the issue. Here is my PHP:
CODE
<?php

set_time_limit(0);

require('connect.php');

//change php.ini smtp line
ini_set("SMTP", "smtp.mysite.com");

//standard mail header
$headers = "From: info@mysite.com";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";

//get message to send

$main = $_GET['message'];
$title = $_GET['subject'];
$sign = $_GET['sign'];
$img = $_GET['image'];

//loop through
for($x=0;$x<count($_GET);$x++)
{
    if ($_GET["mail_$x"])
    {
        
        //mail setup
        $to = $_GET["mail_$x"];
        $subject = $title;
        $message .= '<html><head><style type="text/css">';
        $message .= '#text {';
        $message .= 'font-family:"Gill Sans";';
        $message .= 'font-size:18px;';
        $message .= 'font-weight:normal;';
        $message .= 'text-decoration:none;';
        $message .= 'color:#000;';
        $message .= '}';
        $message .= '#table {';
        $message .= 'padding:6px;';
        $message .= 'max-width:450px;';
        $message .= '}';
        $message .= '</style></head><body><center><table id="table"><tr><td>';
        $message .= '<img src="http://www.mysite.com/email/uploaded/'.$img.'" width="450px" /></td></tr><tr><td><span id="text">';
        $message .= "<br>$main<br><br>
        $sign<br><br>
         mysite 2011";
        $message .= '</span></td></tr></table></center></body></html>';
        
        mail($to, $subject, $message, $headers);
        
    }
}

echo "All mail has been processed. Cilck <a href='http://www.mysite.com/email/index.php/'>here</a> to go back to mailing list.";

?>

Can you see why "$main" (it's text) is show up both above and below?

Thanks so much!

--
joyful
Hey, Does anyone see why $main (text) shows up above and below my image?

Thanks.
Darin McGrew
What is the value of $message each time through the loop?
joyful
Hey Darin,
What do you mean by "value of $message"...
$message just contains the content (image, the "$main" text and the "$sign" signature at the bottom).
Its not displaying the whole message twice, its just displaying "$main" above and below the "<img>".

Sorry, I don't totally understand your question...

Thanks.

--
Darin McGrew
Have you tried just displaying the value of $message at the beginning of the loop, and then again at the end (just before you send the email)?
joyful
Hey thanks a lot Darin,

The issue was that I was missing the concatenation (".") in the first "$message":
CODE
$message = '<html><head><style type="text/css">';

It works great now! Thanks again Darin!

--
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2014 Invision Power Services, Inc.