How to send email templete as mail in php

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

Method 1: Using mail() function

<?php

// Define the email template
$template = file_get_contents('email_template.html');

// Replace placeholders with actual values
$template = str_replace('{name}', 'John Doe', $template);
$template = str_replace('{email}', '[email protected]', $template);

// Set 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";

// Set the email subject and body
$subject = 'Hello from PHP';
$body = $template;

// Send the email
mail('[email protected]', $subject, $body, $headers);

?>

In this example, we first read the email template from a file using file_get_contents(). We then replace placeholders in the template with actual values using str_replace(). We set the email headers using From, Reply-To, MIME-Version, and Content-Type headers. Finally, we set the email subject and body and send the email using the mail() function.

Method 2: Using PHPMailer

<?php

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

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

// Define the email template
$template = file_get_contents('email_template.html');

// Replace placeholders with actual values
$template = str_replace('{name}', 'John Doe', $template);
$template = str_replace('{email}', '[email protected]', $template);

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

// Set the email subject
$mail->Subject = 'Hello from PHP';

// Set the email sender and recipient
$mail->setFrom('[email protected]', 'Your Name');
$mail->addAddress('[email protected]');

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

// Send the email
if (!$mail->send()) {
    echo 'Error sending email: '. $mail->ErrorInfo;
} else {
    echo 'Email sent successfully!';
}

?>

In this example, we use the PHPMailer library to create a new email instance. We define the email template and replace placeholders with actual values. We set the email body, subject, sender, and recipient using the PHPMailer methods. We also set the email headers using the isHTML() method. Finally, we send the email using the send() method and check if the email was sent successfully.

Note that you need to install the PHPMailer library and include it in your PHP script for this method to work.