如何将PHP / HTML表单的副本发送到发件人的电子邮件?

时间:2020-08-09 18:11:42

标签: php html forms email checkbox

我有一个复选框,用于确认发件人是否希望将表格的副本复制到他的电子邮件中。现在我必须使其与PHP一起使用?

我有以下HTML表单代码,“ sendcopy复选框”显示了该代码的底部:

<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;
    }
        ?>

1 个答案:

答案 0 :(得分:0)

首先,检查是否已选中“发送副本”复选框:

$sendCopy = isset($_POST['sendcopy']);

您不必检查其值。如果未选中该复选框,则前端根本不会发送任何内容,因此,如果$ _POST数组中存在该复选框,则表示用户已选中它。

接下来,只需将完全相同的邮件发送到发件人的电子邮件地址:

        if ($sendCopy) {
            $sentToSender = mail($email, "=?$charset?B?" . base64_encode($subject) . "?=", $body, $head);
        }

您可以在将用户重定向到成功页面之前添加此代码。

缺少的括号现在已添加到上面的代码中,因此效果很好。