如何将PHP/HTML表单的副本发送到发件人的电子邮件地址? [英] How can I send a copy of a PHP / HTML form to the sender's email address?

查看:53
本文介绍了如何将PHP/HTML表单的副本发送到发件人的电子邮件地址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个复选框,用于确认发件人是否希望将表格的副本发送到他/她的电子邮件中.现在,我怎样才能使其与PHP一起使用?

I have a checkbox that confirms if the sender wants the copy of the form to his/her email. Now, how can I have get it to work with PHP?

我有以下HTML表单代码.发送副本复选框"显示此代码的底部:

I have the following HTML form code. "sendcopy checkbox" shows the 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 the "sendcopy" checkbox is checked:

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

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

You don't have to check its value. The 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 the success page.

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

这篇关于如何将PHP/HTML表单的副本发送到发件人的电子邮件地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆