Php html email cc generator
Here is an example of a PHP script that generates an HTML email with a CC field:
<?php
// Set the email variables
$to = '[email protected]';
$subject = 'Test Email';
$from = '[email protected]';
$cc = '[email protected]'; // CC email address
$message = 'This is a test email';
// Set the email headers
$headers = 'MIME-Version: 1.0'. "\r\n";
$headers.= 'Content-type: text/html; charset=UTF-8'. "\r\n";
$headers.= 'From: '. $from. "\r\n";
$headers.= 'CC: '. $cc. "\r\n";
// Create the email body
$email_body = '<html><body>';
$email_body.= '<h1>'. $subject. '</h1>';
$email_body.= '<p>'. $message. '</p>';
$email_body.= '</body></html>';
// Send the email
mail($to, $subject, $email_body, $headers);
?>
This script uses the mail()
function to send the email. The $headers
variable is used to set the email headers, including the From
and CC
fields.
You can also use PHPMailer library which is more powerful and flexible than the built-in mail()
function.
<?php
require_once 'PHPMailer/PHPMailer.php';
require_once '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->addCC('[email protected]', 'CC Name');
$mail->Subject = 'Test Email';
$mail->Body = '<html><body><h1>'. $subject. '</h1><p>'. $message. '</p></body></html>';
$mail->AltBody = 'This is the plain text version of the email';
if (!$mail->send()) {
echo 'Error: '. $mail->ErrorInfo;
} else {
echo 'Email sent!';
}
?>
You can also use a template engine like Twig or Smarty to generate the email body.
Please note that you should replace the placeholders ($to
, $subject
, $from
, $cc
, $message
) with your actual email variables.
Also, you should make sure that the email address you are using as the CC
field is a valid email address and that the email server is configured to allow CC emails.