Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending html and css mails to gmail with Swift Mailer

I have some basic html and some css but for some reason gmail puts 3D infornt the 'text/css'

Code sample

$message = "
            <style type='text/css'>
              #main {border:1px solid red;}
            </style>

              <div id='main'>some text</div>
";

But when I view the original send to the gmail

<style type=3D'text/css'>

and maybe thats why the mail is not styled. I am using the swift mailer

// also in html

<div id=3D'main'>

// swift mailer

    $type = $message->getHeaders()->get('Content-Type');
    $type->setValue('text/html');
    $type->setParameter('charset', 'utf-8');
like image 508
grape1 Avatar asked May 26 '12 01:05

grape1


3 Answers

From what I know gmail doesnt support style tag in head or in body. I use style attributes for elements when sending email.

like image 120
Josnidhin Avatar answered Nov 02 '22 08:11

Josnidhin


to let swift mailer send message in HTML format, even for gmail , because swift mailer escape html tags to its represented characters , so you need to write these lines this way:

$message = (new Swift_Message('Message Subject HERE '))
        ->setFrom(array('sender email' => 'Sender Name')) // can be $_POST['email'] etc...
        ->setTo(array('reciever email' => 'Receiver Name')) // your email / multiple supported.
        ->setEncoder( new Swift_Mime_ContentEncoder_PlainContentEncoder('8bit'))
        ->setBody($template, "text/html");

after that every thing should work fine the trick here is to till swift mailer to use Swift mailler MimeType class and point to the correct encoding . you can check here for more information link here

like image 2
Mohammed Omer Avatar answered Nov 02 '22 08:11

Mohammed Omer


Swift Mailer is trying to escape " to \". You have to use setEncoder() before setBody():

$message->setEncoder(Swift_Encoding::get8BitEncoding());
$message->setBody($body, "text/html");
like image 1
Demis Palma ツ Avatar answered Nov 02 '22 06:11

Demis Palma ツ