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:
- We define the email settings: the recipient's email address, the subject of the email, and the message body.
- We define the email headers, which include the MIME version and the content type (text/html).
- 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. - 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:
- Make sure to set the
Content-type
header totext/html
to ensure that the email is sent as HTML. - You can use inline styles or external stylesheets (e.g.,
<link rel="stylesheet" type="text/css" href="styles.css">
) to style your email. - You can use PHP variables to dynamically generate the email content.
- You can use a library like PHPMailer to send emails with more advanced features, such as attachments and multiple recipients.