Php html email css example

Here is an example of a PHP script that sends an HTML email with CSS styles:

PHP Script:

<?php
// Define the email settings
$to = '[email protected]';
$subject = 'Test Email with HTML and CSS';
$message = '<html>
  <head>
    <title>Test Email</title>
    <style>
      body {
        font-family: Arial, sans-serif;
        font-size: 16px;
      }
      h1 {
        color: #00698f;
      }
    </style>
  </head>
  <body>
    <h1>Welcome to our test email!</h1>
    <p>This is a test email sent using PHP.</p>
  </body>
</html>';

// Define the email headers
$headers = 'MIME-Version: 1.0'. "\r\n";
$headers.= 'Content-type: text/html; charset=UTF-8'. "\r\n";

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

echo 'Email sent successfully!';
?>

Explanation:

  1. We define the email settings: the recipient's email address, the subject of the email, and the message body.
  2. We define the email headers, which include the MIME version and the content type (text/html).
  3. We use the mail() function to send the email. The first argument is the recipient's email address, the second is the subject, the third is the message body, and the fourth is the headers.
  4. We use HTML and CSS to format the email message. In this example, we define a simple HTML structure with a heading and a paragraph, and we use CSS to style the text.

HTML and CSS:

The HTML structure is simple, with a heading and a paragraph:

<html>
  <head>
    <title>Test Email</title>
    <style>
      body {
        font-family: Arial, sans-serif;
        font-size: 16px;
      }
      h1 {
        color: #00698f;
      }
    </style>
  </head>
  <body>
    <h1>Welcome to our test email!</h1>
    <p>This is a test email sent using PHP.</p>
  </body>
</html>

The CSS styles the text using the body and h1 selectors:

body {
  font-family: Arial, sans-serif;
  font-size: 16px;
}

h1 {
  color: #00698f;
}

Tips and Variations: