1

Im not that awesome at php to please forgivr me.

So I am sending a html email using PHP.

After some research I have built one using tables and writ a PHP sending script similar to some many of you will of used.

I know inline styling seems to be the most commonly used way to add style.

When I define my $message variable before my $headers, is something LIKE this possible?

$message = " 

  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>BPN UK Contact Request</title>
    <style type="text/css">

     .exampleclass { 
       CSS Rules
     }

    </style>

    REST OF HTML LAYOUT, INCEPTION TABLES ETC ETC

"

Or do I have to go through and inline style it in the tags etc, its just that way seems a lot more work and more restricting.

Sorry if this is a stupid question.

2
  • 1
    In-fact, I do believe that is exactly how you should do it. Commented May 23, 2013 at 20:22
  • 2
    FYI: Google/Gmail and others will strip out all your CSS, unless they're inline coded. Commented May 23, 2013 at 20:22

5 Answers 5

3

That's perfectly acceptable, but realize that many web-based mail clients will replace/mangle inline CCS to prevent that css from affecting the "container" webmail site. Keep the CSS simple and don't try to go for fancy styling.

Sign up to request clarification or add additional context in comments.

3 Comments

I for one, learned that lesson well, some time ago (sharing the wealth) ;-)
Worse, modern versions of Microsoft Outlook will ignore your CSS entirely, along with almost all inline styles. As miserable as it is in every possible way, Outlook is a popular enough client that it's worth seriously considering the value of designing your emails specifically to work with it; since it has the most restricted subset of styling capabilities available in pretty much any client that renders HTML at all, you can be reasonably confident that an email which looks like it's supposed to in Outlook will do so elsewhere as well.
@AaronMiller True. Yet, there is more than 1 way to skin that Waskiwee Wabbit!
2

The way you did it should work just fine, but there are a few things you to keep in mind. The first is that you need to make sure to have a content-type header. This tells the browser to display the message as HTML. If you don't, you will get an email with a whole bunch of html tags as plain text. Something like this would suffice:

$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";

Also, replace double quotes (") in the HTML with single quotes (') as the double quotes dictate the start and end of $message.

Comments

1

You coluld also try:

    <?php
   //include CSS Style Sheet
   echo "<link rel='stylesheet' type='text/css' href='path-to-css-file' />";

   //include a javascript file
   echo "<script type='text/javascript' src='path-to-javascript-file'></script>";
?>

3 Comments

does that actually get noticed?
I was looking for a way to refactor all the CSS in the file, and was able to accomplish this by using the stylesheet method mentioned in here.
Glad it helped @Levi!
0

as some one who has written a few hundred email templates for work i recommend only ever use in-line css for email if you want to maximise compatibility, and send the plain text version to.

Comments

0

Yes everything needs to be inline, but indeed that's very clumsy and not very DRY.

At work we're using the class InlineStyle that allows you to put all the CSS in the <head> of the document like you normally do, and then you can use the class to parse the HTML and CSS and convert all the CSS in the head to inline styles. So you get inline styles, but you write the HTML like you normally would with CSS in the <head>. Works very well.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.