如何将PHP / HTML表单的副本发送到发件人的电子邮件? [英] How to send copy of PHP / HTML form to sender's email?
问题描述
我有一个复选框,用于确认发件人是否希望将表格的副本复制到他的电子邮件中。现在我必须使其与PHP一起使用吗?
I have a checkbox, that confirms if sender want the copy of the form to his email. Now I have to get it working with PHP?
我有以下HTML表单代码,即 sendcopy checkbox。显示此代码的底部:
I have following HTML form code, "sendcopy checkbox" shows bottom of this code:
<form id="contact-form" action="send_form.php" method="post">
<input type="hidden" name="recipient" value="myemail@domain.net">
<input type="hidden" name="inhtml" value="yes">
<div class="contact-row">
<div style="float: left; width: 34%;">
<input class="contact-input" style="width: 300px;" name="name" placeholder="Name*" required><br>
</div>
<div style="float: left; width: 33%;">
<input class="contact-input" style="width: 290px;" name="email" placeholder="Email" type="email"><br>
</div>
<div style="float: right; width: 33%;">
<input class="contact-input" style="width: 305px;" name="subject" placeholder="Subject*" required><br>
</div>
</div>
<br>
<br>
<br>
<div class="contact-row">
<textarea cols="173" rows="10" name="message" placeholder="Message*" required></textarea><br>
</div>
<br>
<label>
<input type="checkbox" name="sendcopy" value="Yes" checked/>Send copy to your mail
</label>
<br>
<div class="confirm">
<label>
<input type="checkbox" name="confirm" required>
Confirm that you are people.
</label>
</div>
<br>
<br>
<div class="contact-row">
<div class="contact-row">
<center>
<input id="contact-submit" type="image" src="submit.png" alt="Send" />
</center>
</div>
</div>
</form>
我的PHP代码:
<?php
$recipient = $_POST["recipient"];
$subject = $_POST["subject"];
$inhtml = $_POST["inhtml"];
$email = $_POST["email"];
$name = $_POST["name"];
$message = $_POST["message"];
$sendCopy = isset($_POST['sendcopy'];
$charset = 'utf-8';
$head =
"From: $email \r\n" .
"MIME-Version: 1.0\r\n" .
"Content-Type: text/html; charset=$charset\r\n" .
"Content-Transfer-Encoding: 8bit";
$body = '';
if ($inhtml == 'yes'){
$body = '<html><body style="font-family: Arial; color: #727272;">';
$body = $body . '<h3 style="font-size: 16px;">Name: ' . $name . '</h3>';
$body = $body . '<h4 style="font-size: 14px;">Subject: ' . $subject . ' (' . $email . ') </h4>';
$body = $body . '<p style="font-size: 13px;">' . $message . '</p>';
$body = $body . '</body></html>';
}
else {
$body = $body . 'Name: ' . $name . '
Subject: ' . $name . ' (' . $email . ')
Message:
' . $message ;
}
if (mail($recipient, "=?$charset?B?" . base64_encode($subject) . "?=", $body, $head)) {
if ($sendCopy) {
$sentToSender = mail($email, "=?$charset?B?" . base64_encode($subject) . "?=", $body, $head);
}
header("Location: ../form-success.htm");
exit;
} else {
header("Location: ../form-error.htm");
exit;
}
?>
推荐答案
首先,检查 sendcopy复选框已选中:
First, check if "sendcopy" checkbox is checked:
$sendCopy = isset($_POST['sendcopy']);
您不必检查其值。如果未选中该复选框,则前端根本不会发送任何内容,因此,如果$ _POST数组中存在该复选框,则表示用户已选中它。
You don't have to check it's value. Frontend simply won't send anything if the checkbox is not checked, so if it exists in the $_POST array, that means the user has checked it.
下一步,只需发送完全相同的内容邮件发送到发件人的电子邮件地址:
Next, just send the exact same mail to the sender's email address:
if ($sendCopy) {
$sentToSender = mail($email, "=?$charset?B?" . base64_encode($subject) . "?=", $body, $head);
}
您可以在将用户重定向到成功页面之前添加此代码。
You can add this code just before redirecting your user to success page.
缺少的括号现在已添加到上面的代码中,因此效果很好。
这篇关于如何将PHP / HTML表单的副本发送到发件人的电子邮件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!