A
A
astrotrain2017-08-15 14:46:15
PHP
astrotrain, 2017-08-15 14:46:15

How to send valid html email via php to gmail?

There is a simple php code that sends an html page to all recipients in a loop

$code = '';
      $message = file_get_contents($this->message_file);
      $this->message = base64_encode($message);
      foreach($this->to as $to){
         $code .= '
$to = "'.$to.'";

$headers  = "MIME-Version: 1.0" . "\n";
$headers .= "Content-type: text/html; charset=UTF-8" . "\n";
$headers .= "From: '. $this->from .' " . "\n";

$subject = "Message sent from  ".$_SERVER[\'SERVER_NAME\'];


mail($to, $subject, base64_decode("'.$this->message.'"), $headers);
      ';

But the problem is that an unformatted message arrives in the box in the form of text without styles and other things (although in the body of the message, if you look, everything is in place). How to make google recognize the message as html?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
artem78, 2017-08-15
@artem78

Don't reinvent the wheel. Use ready-made solutions, for example - phpmailer . Then the sending procedure will look like this:

<?php

$mail = new PHPMailer;
$mail->setFrom('[email protected]'); // Email отправителя

// Можно отправить письмо нескольким адресатам за один раз
$recipients = ['[email protected]', '[email protected]', '[email protected]'];
foreach ($recipients as $recipient) {
  $mail->addAddress($recipient); 
  //$mail->AddBCC($recipient); // Или можно отправить скрытую копию, чтобы получатели не видели друг друга
}              

$mail->Subject = 'Заголовок письма';
$mail->Body = '<b>Здесь текст письма в формате html.<b>';
$mail->isHTML(true);  // Формат HTML

$mail->send();

K
Kirill Levunin, 2017-08-15
@klevunin

Styles in the letter are made like this?
https://www.codecademy.com/articles/html-inline-styles
Try like this
and header

Content-Transfer-Encoding: quoted-printable
Content-type: text/html; charset=utf-8
MIME-Version: 1.0

DKIM signature - must be done if not done. Otherwise, 100% in spam will always fall.
mail($email, "=?utf-8?b?" . base64_encode("Тема письма") . "?=", quoted_printable_encode("тело письма с html"), $header);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question