How to send a mail in php with html

To send an email with HTML content in PHP, you can use the mail() function or a PHP email library such as PHPMailer. Here's an example of how to send an email with HTML content using the mail() function:

<?php
// Define the email headers
$headers = 'MIME-Version: 1.0'. "\r\n";
$headers.= 'Content-type: text/html; charset=iso-8859-1'. "\r\n";

// Define the email body
$email_body = '<html><body><h1>Hello World!</h1></body></html>';

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

// Define the email subject
$subject = 'Hello World!';

// Send the email
mail($recipient, $subject, $email_body, $headers);

?>

In this example, the mail() function is used to send an email with the specified recipient, subject, and body. The body is defined as an HTML string, and the Content-type header is set to text/html to indicate that the email body is HTML.

If you want to send an email with attachments, you can use the mail() function with the attachments parameter. Here's an example:

<?php
// Define the email headers
$headers = 'MIME-Version: 1.0'. "\r\n";
$headers.= 'Content-type: text/html; charset=iso-8859-1'. "\r\n";

// Define the email body
$email_body = '<html><body><h1>Hello World!</h1></body></html>';

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

// Define the email subject
$subject = 'Hello World!';

// Define the attachment
$attachment = 'path/to/attachment.txt';

// Send the email
mail($recipient, $subject, $email_body, $headers, $attachment);

?>

In this example, the mail() function is used to send an email with the specified recipient, subject, and body. The attachments parameter is used to specify the path to the attachment file.

If you want to send an email with HTML content and attachments using PHPMailer, you can use the following code:

<?php
require_once 'PHPMailer/PHPMailer.php';
require_once 'PHPMailer/SMTP.php';

// Create a new PHPMailer object
$mail = new PHPMailer\PHPMailer\PHPMailer();

// Set the email recipient
$mail->setAddress($recipient);

// Set the email subject
$mail->Subject = $subject;

// Set the email body
$mail->Body = $email_body;

// Set the email HTML body
$mail->isHTML(true);

// Add an attachment
$mail->addAttachment($attachment);

// Send the email
$mail->send();

?>

In this example, the PHPMailer library is used to send an email with the specified recipient, subject, and body. The isHTML() method is used to set the email body as HTML, and the addAttachment() method is used to add an attachment to the email.

Note that the mail() function and PHPMailer library have different requirements and limitations for sending emails. The mail() function is a built-in PHP function that is simple to use but has limited features, while PHPMailer is a more powerful and flexible library that provides more features and options for sending emails.