Php html email code mailto

Here is an example of PHP code that generates an HTML email with a mailto link:

<?php

// Set the email address and subject
$email_to = '[email protected]';
$email_subject = 'Test Email';

// Set the email body
$email_body = '<html>
  <body>
    <p>Hello!</p>
    <p>This is a test email.</p>
    <p><a href="mailto:'.$email_to.'?subject='.$email_subject.'">Click here to reply</a></p>
  </body>
</html>';

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

// Send the email
mail($email_to, $email_subject, $email_body, $headers);

?>

This code sets the email address, subject, and body, and then uses the mail() function to send the email. The email body is an HTML string that includes a mailto link with the recipient's email address and the subject of the email.

You can also use PHPMailer library which is more powerful and flexible than the built-in mail() function.

<?php

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

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

$mail->isHTML(true);

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

$mail->Subject = $email_subject;
$mail->Body = '<html>
  <body>
    <p>Hello!</p>
    <p>This is a test email.</p>
    <p><a href="mailto:'.$email_to.'?subject='.$email_subject.'">Click here to reply</a></p>
  </body>
</html>';

$mail->send();

?>

You can also use sprintf function to format the email body and subject

<?php

$email_to = '[email protected]';
$email_subject = 'Test Email';

$email_body = sprintf('<html>
  <body>
    <p>Hello!</p>
    <p>This is a test email.</p>
    <p><a href="mailto:%s?subject=%s">Click here to reply</a></p>
  </body>
</html>', $email_to, $email_subject);

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

mail($email_to, $email_subject, $email_body, $headers);

?>

Please note that the mailto link will only work if the email client is configured to handle mailto links.