Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send HTML email including CSS style sheet via PHP

Tags:

html

css

php

email

I want to send a HTML page that contains CSS stylesheet to an email account. The problem is that the CSS style sheet is not visible at all. Do I have to inline every CSS styling? It seems to be highly inefficient method. Any others ideas ?

Below is the PHP code for sending the email :

<?php
$to = "[email protected]";

// subject
$subject = "Test mail";

// message
$message = file_get_contents("index.html");

// from
$from = "[email protected]";

// 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 .= "From:" . $from;

// Mail it
mail($to,$subject,$message,$headers);

echo "Mail Sent.";
?>

I would greatly appreciate your help. Thanks in advance.

like image 336
Wijden Avatar asked Dec 17 '13 15:12

Wijden


1 Answers

Google, Outlook, Yahoo all the major email service providers do not allow CSS external files. It may sound antiquated that they have opted for inline styles and tables to render a proper email message with any styling.

It may be tedious to code but companies like https://litmus.com/ and envato will help with some templates and formatting issues.

my 2¢'s: email providers can't trust all sources of information being sent and so this is a way of keeping the malicious code at bay.

like image 192
JFly28 Avatar answered Oct 17 '22 17:10

JFly28