Php html email example

Here is an example of a PHP script that sends an HTML email using the mail() function:

<?php

// Define the email headers
$headers = 'From: Your Name <[email protected]>'. "\r\n";
$headers.= 'Reply-To: Your Name <[email protected]>'. "\r\n";
$headers.= 'MIME-Version: 1.0'. "\r\n";
$headers.= 'Content-Type: text/html; charset=UTF-8'. "\r\n";

// Define the email body
$body = '<html><body>';
$body.= '<h1>Hello World!</h1>';
$body.= '<p>This is an example of an HTML email.</p>';
$body.= '</body></html>';

// Define the email recipient
$to = '[email protected]';

// Send the email
mail($to, 'Hello World!', $body, $headers);

?>

This script defines the email headers, body, and recipient, and then uses the mail() function to send the email. The mail() function takes four arguments: the recipient's email address, the subject of the email, the email body, and the email headers.

In this example, the email body is defined as an HTML string that includes an <h1> heading and a paragraph of text. The Content-Type header is set to text/html to indicate that the email body is HTML.

You can also use PHPMailer library which is more powerful and flexible than the built-in mail() function. Here is an example of how to use PHPMailer:

<?php

require 'PHPMailer/PHPMailer.php';
require 'PHPMailer/SMTP.php';

$mail = new PHPMailer\PHPMailer\PHPMailer();

$mail->isSMTP();
$mail->Host = 'smtp.example.com';
$mail->SMTPAuth = true;
$mail->Username = '[email protected]';
$mail->Password = 'your_password';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;

$mail->setFrom('[email protected]', 'Your Name');
$mail->addAddress('[email protected]', 'Recipient Name');

$mail->isHTML(true);
$mail->Subject = 'Hello World!';
$mail->Body = '<html><body><h1>Hello World!</h1><p>This is an example of an HTML email.</p></body></html>';

if (!$mail->send()) {
    echo 'Error: '. $mail->ErrorInfo;
} else {
    echo 'Email sent!';
}

?>

This script uses the PHPMailer library to send an HTML email. It defines the email headers, body, and recipient, and then uses the send() method to send the email. The isHTML() method is used to specify that the email body is HTML.

You can also use other libraries such as SwiftMailer, Zend_Mail, etc.

Please note that you should replace the placeholders with your actual email address, password, and other settings.