1

I'm trying to send emails with PHP. I would like to add some style with CSS (no matter if inline, internal or with external file).

I've tried PHPmailer, but it fails to recognize some elements (such as body), media queries (@media) and declarations (max-width, inline-block, etc...). I'm now trying to use SwiftMailer, but online documentation doesn't mention anything about styling.

Just for sake of clarity, here's the snippet I would like to use: JsFiddle

Any ideas to send PHP emails with working HTML/CSS?

4
  • 2
    you have to add inline CSS to customize html elements in mail Commented Jul 31, 2014 at 9:01
  • 1
    you have to use inline css for your design and use mail() function. Commented Jul 31, 2014 at 9:01
  • You can look here for allowed CSS within email clients: campaignmonitor.com/css There is a reason why there is a whole business model around email campaigns and there is a lot of knowledge to obtain if you want to do it 100% correct. Commented Jul 31, 2014 at 9:08
  • I think this question is too broad. But since html mails can be a real pain, it's worth having a look here: zurb.com/ink Commented Jul 31, 2014 at 9:10

3 Answers 3

1

You could use a CSS inliner tool like http://templates.mailchimp.com/resources/inline-css/ to convert your 'normal' template (made like a normal HTML page) to an email-friendly template. This is needed because mail clients doesn't recognise separate elements. I currently use a template based on the ones made available by Zurb, http://zurb.com/ink/

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

Comments

0

It also depends on the receiver's email client. Not all clients accept all css. Use a litmus test of some sort to check with different e-mail clients which css you can use. Tables often work with email clients, a lot of them don't accept divs I believe.

Comments

0

I use PHPMailer to send emails with PHP, and it never fails to recognize these elements (for me anyway).

To use it, i create a fonction :

function sendMail($name, $from, $message, $to, $subject)
{
   $mail = new PHPMailer();
   $body = "<html><head></head><body style=\"background-color: #F2F1F0;\"><p>".$message."</p></body></html>";
   $mail->SetFrom($from, $name);    
   $mail->AddReplyTo($from, $name);

   // Only if you want to send an attachment, send a variable $object to the function
   //$mail->AddAttachment($object);

   $mail->Subject    = $subject;    
   $mail->AltBody    = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test    
   $mail->MsgHTML($body);      
   $mail->AddAddress($to, $name);

   if(!$mail->Send()) {
       $result = $mail->ErrorInfo;
   }
   else
   {
       $result = "Mail sent successfully";
   }
   return $result;
}

And an exemple to use it :

// Preparation of the message
$message = '<p class="img"><img src="http://mywebsite.com/img/myImage.png" width="220px"></p>';
$message .= '<p class="txt">Hello Mr Dupond !</p>';
$message .= '<p class="txt">We are glad to have you among us</p>';
$message .= '<p class="txt">Se you soon on our Website !</p>';
$message = utf8_decode($message);

// variables needed
$name = "Mr Oktopuss";
$from = "[email protected]";
$to = "[email protected]";
$subject = "The subject of this mail !";

// Send the mail and receive the result
$result = sendMail($name, $from, $message, $to, $subject);
echo $result;

Maybe that could be help you

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.