Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send HTML/CSS emails?

Tags:

css

php

email

xhtml

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.

like image 534
James Skidmore Avatar asked Dec 03 '22 07:12

James Skidmore


1 Answers

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.

like image 115
ajhit406 Avatar answered Dec 18 '22 18:12

ajhit406