How to send HTML / CSS emails?

Most email clients have trouble reading CSS in HTML emails (including Gmail and Hotmail). I often use this service to convert my HTML / CSS to the correct email format so that everything looks normal at the end of the user. Basically what he does is convert all CSS to inline styles:

http://premailer.dialect.ca/

Do you have any other methods to send CSS to your HTML emails? I automatically generate emails, and due to some restrictions I cannot change the inline styles.

+3
source share
4 answers

, CSS, SwiftMailer (http://swiftmailer.org/) PHP5 , .

, , HTML, , , - .

"views" ( smarty, , .tpl). SwiftMailer:: sendTemplate() , :

 $email_templates = array('text/html' => 'email/html/' . $template . '.en.html.tpl',
                        'text/plain' => 'email/text/' . $template . '.en.txt.tpl');

foreach ($email_templates as $type => $file) {
  if ($email->template_exists($file)) {
    $message->attach(new Swift_Message_Part($email->fetch($file), $type));
  } elseif ($type == 'text/plain') {
    throw new Exception('Could not send email -- no text version was found');
  }
}

. SwiftMailer , "" , . .

+7

, , HTML. mail(), : : html/text ( "" ).

: ( php.net/mail)

// 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 .= 'To: Mary <mary@example.com>, Kelly <kelly@example.com>' . "\r\n";
$headers .= 'From: Birthday Reminder <birthday@example.com>' . "\r\n";
$headers .= 'Cc: birthdayarchive@example.com' . "\r\n";
$headers .= 'Bcc: birthdaycheck@example.com' . "\r\n";

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

- html- css

+3

( PHP ), " ", : to, subject, message headers

Let me know if you want to create a form to fill out and run this PHP script, otherwise you can simply enter everything in this file manually, save it as a PHP file, upload it to a server that supports PHP, and go to the file in your browser.

Here is the code:

// Setup recipients
$to = 'johndoe@google.com' . ',' // comma separates multiple addresses
$to .= 'janedoe@msn.com';

// subject
$subject = 'PHP Email Script - Test Email';

// message (to use single quotes in the 'message' variable, supercede them with a back slash like this-->&nbsp; \'
$message = '
<html>
<head>
  <title>PHP Email Script</title>
</head>
<body>
  <p style="background: #ccc; width: 100%;">Test Email Script</p>
</body>
</html>
';


// 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 .= 'To: Mary <mary@example.com>, Kelly <kelly@example.com>' . "\r\n";
$headers .= 'From: Birthday Reminder <birthday@example.com>' . "\r\n";
$headers .= 'Cc: birthdayarchive@example.com' . "\r\n";
$headers .= 'Bcc: birthdaycheck@example.com' . "\r\n";

// Send the email
mail($to, $subject, $message, $headers);
+2
source

Source: https://habr.com/ru/post/1713159/


All Articles