Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send Email With Html content with all styling with gmail php api

Tags:

php

gmail-api

I want to send email with all html using the gmail api. This is the preview of the html : https://docs.google.com/drawings/d/1J0nzeTol6VFr8sB1TlMDbTrSVanDncQ8FOelP_SvmY0/edit

But when i send the email to my test account and then i check the email in inbox all the styling in email is lost. This is the screenshot of the email after sending. https://docs.google.com/drawings/d/1yfzmlqzMhJMWbJth-o6mfgSJcGAtUIKr41hpvzlFQGE/edit?usp=sharing

I want to get the all the styling in emails. I have also tried the charset iso-8859-1 but still the same result.

This is my php code :

        $service = new Google_Service_Gmail($this->client);
        $user = 'me';
        $strSubject = $subject;
        $strRawMessage = "From: <".$from.">\r\n";
        $strRawMessage .= "To:  <".$to.">\r\n";
        $strRawMessage .= 'Subject: =?utf-8?B?' . base64_encode($strSubject) . "?=\r\n";
        $strRawMessage .= "MIME-Version: 1.0\r\n";
        $strRawMessage .= "Content-Type: text/html; charset=utf-8\r\n";
        $strRawMessage .= 'Content-Transfer-Encoding: quoted-printable' . "\r\n\r\n";
        $strRawMessage .= $message."\r\n";
        // The message needs to be encoded in Base64URL
        $mime = rtrim(strtr(base64_encode($strRawMessage), '+/', '-_'), '=');
        $msg = new Google_Service_Gmail_Message();
        $msg->setRaw($mime);
        //The special value **me** can be used to indicate the authenticated user.
        $service->users_messages->send("me", $msg);
like image 367
Aneeb Ryan Avatar asked Nov 22 '17 09:11

Aneeb Ryan


1 Answers

As the OP pointed out by changing Content-Transfer-Encoding: quoted-printable to Content-Transfer-Encoding: base64 solves the problem.

I pretty much had the same problem and was able to solve it the same way

like image 155
Gardezi Avatar answered Oct 20 '22 06:10

Gardezi