Php html email
A classic combination!
To send an email using PHP and HTML, you'll need to use a combination of PHP's built-in mail()
function and some HTML code to format the email content. Here's a basic example to get you started:
PHP Code:
<?php
// Define the email recipient and subject
$to = '[email protected]';
$subject = 'Test Email';
// Define the email body using HTML
$htmlBody = '<html>
<body>
<h1>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 using the mail() function
mail($to, $subject, $htmlBody, $headers);
?>
What's happening:
- We define the email recipient (
$to
) and subject ($subject
). - We define the email body using HTML (
$htmlBody
). In this example, we're using a simple HTML structure with a heading and paragraph. - We define the email headers (
$headers
) using theMIME-Version
andContent-type
headers. We set theContent-type
header totext/html
to indicate that the email body contains HTML content. - We use the
mail()
function to send the email. Themail()
function takes four arguments: the recipient's email address, the subject, the email body, and the headers.
Tips and Variations:
- Make sure to set the
Content-type
header totext/html
to ensure that the email body is rendered as HTML. - You can use PHP's
strip_tags()
function to remove any HTML tags from the email body if you prefer a plain text email. - You can use a PHP email library like PHPMailer or SwiftMailer to send emails with more advanced features, such as attachments and multiple recipients.
- Be aware of email spam filters and ensure that your email content is not flagged as spam.