tags:

views:

1291

answers:

5

Most email clients have problems reading CSS in HTML emails (including Gmail and Hotmail). I often use this service to convert my HTML/CSS to proper email format so that everything looks normal on the user's end. Basically what it does is convert all CSS to inline styles:

http://premailer.dialect.ca/

Do any of you have any other methods for sending CSS in your HTML emails? I automatically generate the emails, and due to some restrictions, I can't modify the the inline styles.

+3  A: 

You need to add a header that says the content is HTML. When you use the mail() function, one of the headers should be: Content Type: html/text (That might not be the 'exact' header).

Let me find you an example: (From the php.net/mail page)

// To send HTML mail, the Content-type header must be set
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

// Additional headers
$headers .= 'To: Mary <[email protected]>, Kelly <[email protected]>' . "\r\n";
$headers .= 'From: Birthday Reminder <[email protected]>' . "\r\n";
$headers .= 'Cc: [email protected]' . "\r\n";
$headers .= 'Bcc: [email protected]' . "\r\n";

// Mail it
mail($to, $subject, $message, $headers);
Chacha102
Unfortunately, sending HTML email with no alternate is perceived by many spam filters as spam. To lower the chance of triggering that, you should send it as multi-part, with one section as text/plain and the other as text/html.
staticsan
+1  A: 

To add to the above example, (in case you don't know PHP very well), you just have to build the "email" using the variables: to, subject, message, and headers

Let me know if you want to know how to create a form to fill and run this PHP script, otherwise, you can simply enter everything in this file manually, save as a PHP file, throw it up on a server that supports PHP, and navigate to the file in your browser.

Here's the code:

// Setup recipients
$to = '[email protected]' . ',' // comma separates multiple addresses
$to .= '[email protected]';

// subject
$subject = 'PHP Email Script - Test Email';

// message (to use single quotes in the 'message' variable, supercede them with a back slash like this-->&nbsp; \'
$message = '
<html>
<head>
  <title>PHP Email Script</title>
</head>
<body>
  <p style="background: #ccc; width: 100%;">Test Email Script</p>
</body>
</html>
';


// To send HTML mail, the Content-type header must be set
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

// Additional headers
$headers .= 'To: Mary <[email protected]>, Kelly <[email protected]>' . "\r\n";
$headers .= 'From: Birthday Reminder <[email protected]>' . "\r\n";
$headers .= 'Cc: [email protected]' . "\r\n";
$headers .= 'Bcc: [email protected]' . "\r\n";

// Send the email
mail($to, $subject, $message, $headers);
Jmb-Elite
A: 

the easiest way is to write a html page with embedded css and push it through automated styling machine

+1  A: 

As for direct format, I've always done inline CSS styling, however I use SwiftMailer (http://swiftmailer.org/) for PHP5 to handle email functionality and it has helped immensely.

You can send multipart messages with different formats so if the email client doesn't like the HTML version, you can always default to the text version so you know at least something is getting through clean.

In your "views" folder, you can set apart routes for different email formats (I use smarty too, hence the .tpl extension). Here's what a typical SwiftMailer::sendTemplate() function would look like when you're setting up the templates:

 $email_templates = array('text/html' => 'email/html/' . $template . '.en.html.tpl',
                        'text/plain' => 'email/text/' . $template . '.en.txt.tpl');

foreach ($email_templates as $type => $file) {
  if ($email->template_exists($file)) {
    $message->attach(new Swift_Message_Part($email->fetch($file), $type));
  } elseif ($type == 'text/plain') {
    throw new Exception('Could not send email -- no text version was found');
  }
}

You get the idea. SwiftMailer has a bunch of other good stuff, including returning "undeliverable" addresses, logging delivery errors, and throttling large email batches. I'd suggest you check it out.

ajhit406
A: 

Hii All,

I am a new for email messege related things, so please help me.My main concern is that i wanted to send a html page with the css in mail whatever CSS we are using in our software / sites, that should be in the same Format(GUI point of View) in the email.

I have done this,but still i am not getting the mail with CSS So please help me.

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

/* additional headers */
$headers .= "To: [email protected]\r\n"; $headers .= "From: ".$my_name."<".$my_email.">\r\n";

i am really thankful to you all.

abhishek
@abhishek, I would suggest creating a new question. You will not get any replies in this thread.
James Skidmore