联系表格将副本发送给用户/发件人

时间:2015-07-31 04:46:15

标签: php html email

我有一个带有联系表单和sendmail.php文件的网站模板。我想要做的是让发件人选择将表格的副本发送给他们自己。

我尝试了不同的建议,我在网上找到但没有成功。 我已经设法很容易地获得html文件中的复选框选项,但即使选中它,发件人也不会获得电子邮件的副本。所以我的想法是PHP端有些东西我丢失或者错误。

我试过了:

//cc option
$headers["From"] = "$clientEmail";
$headers["To"] = "$emailTo";
$headers["Subject"] = $subject;
$headers['Cc'] = '$clientEmail';

但这没效果。

这是HTML和PHP代码:

HTML:

        <div class="contact-us container">
            <div class="row">
                <div class="contact-form span7">
                    <p>Want to get in touch? Use the form below to send an email.</p>
                    <form method="post" action="assets/sendmail.php">
                        <label for="name" class="nameLabel">Name</label>
                          <input id="name" type="text" name="name" placeholder="Enter your name...">
                        <label for="email" class="emailLabel">Email</label>
                          <input id="email" type="text" name="email" placeholder="Enter your email...">
                        <label for="subject">Subject</label>
                          <input id="subject" type="text" name="subject" placeholder="Your subject...">
                        <label for="message" class="messageLabel">Message</label>
                          <textarea id="message" name="message" placeholder="Your message..."></textarea>
                        <button id="button">Send</button>
                    </form>
                    <input type="checkbox" value="1" id="sendCopy" name="sendCopy" class="rsp-cB"/>
                    <label for="sendCopy">Send me a copy.</label>
                </div>
            </div>
        </div>

PHP:

// Email address verification
if($_POST) {

    // Enter the email where you want to receive the message
    $emailTo = 'email@gmail.com'; 

    $clientName = trim($_POST['name']);
    $clientEmail = trim($_POST['email']);
    $subject = trim($_POST['subject']);
    $message = trim($_POST['message']);

    $array = array();
    $array['nameMessage'] = '';
    $array['emailMessage'] = '';
    $array['messageMessage'] = '';

    if($clientName == '') {
        $array['nameMessage'] = 'Please enter your name.';
    }
    if(!isEmail($clientEmail)) {
        $array['emailMessage'] = 'Please insert a valid email address.';
    }
    if($message == '') {
        $array['messageMessage'] = 'Please enter your message.';
    }
    if($clientName != '' && isEmail($clientEmail) && $message != '') {
        // Send email
    $headers = "From: " . $clientName . " <" . $clientEmail . ">" . "\r\n" . "Reply-To: " . $clientEmail;
    mail($emailTo, $subject, $message, $headers);
    }

    header('Location: thanks.php');
}?>

我是一个完整的新手所以请用外行语言建议需要什么,在哪些文件和哪里。

P.S。我知道这里还有其他类似的问题,但我已经尝试过这些问题以及谷歌搜索其他建议而且我无法让它发挥作用。

5 个答案:

答案 0 :(得分:1)

你不能像这样使用标题数组

grep

正确的方法是。

// Cc option
$headers["From"]    = "$clientEmail";
$headers["To"]      = "$emailTo";
$headers["Subject"] = $subject;
$headers['Cc']      = "$clientEmail";

然后在mail()

$to      = 'reciver_address@gmail.com';   // Recipient of email
$subject = 'I\'m sending a mail';         // Subject of email
$message = 'This is message';             // Body of email
$header  = 'From: address@gmail.com';     // Sender email address
$header .= "\r\n" . 'Cc: copy@gmail.com'; // Copy of email

答案 1 :(得分:0)

第一个变量是它将被发送到的电子邮件地址。

只需在底部添加第二封邮件(),如下所示:

mail($emailTo, $subject, $message, $headers);
if ($_POST["sendCopy"] == '1')
     mail($clientEmail, $subject, $message, $headers);

此外,您应该将复选框放在</form>之前。否则,复选框值不能传递,因为它不是表单的一部分。

答案 2 :(得分:0)

试试此标题。

$headers = "From: " . $clientName . " <" . $clientEmail . ">" . "\r\n";
$headers .= "Reply-To: " . $clientEmail. "\r\n"; 
$headers .= "Cc: " . $clientEmail. "\r\n";

答案 3 :(得分:0)

你是对的,缺少PHP部分。如果使用BuildIN PHP Mail函数,则必须将CC添加到标题中。

未经测试,但将其添加到第一个“$ header”下面的代码中,并且应该将副本发送给客户端:

 // add CC when checked in Form and attach to headers
 if ($_POST['sendCopy'] == '1') {
     $headers .= "Cc: $clientEmail" . "\r\n";
 }

我建议您查看一些更好的邮件解决方案,例如:PHPMailer

答案 4 :(得分:0)

请尝试此操作(之前忽略代码)

<?php 
if($_SERVER['REQUEST_METHOD']=="POST") {         

    if($_POST['sendCopy']=="1") {    

        // Enter the email where you want to receive the message
        $emailTo = 'email@gmail.com'; 

        $clientName = trim($_POST['name']);
        $clientEmail = trim($_POST['email']);
        $subject = trim($_POST['subject']);
        $message = trim($_POST['message']);

        $array = array();
        $array['nameMessage'] = '';
        $array['emailMessage'] = '';
        $array['messageMessage'] = '';

        if($clientName == '') {
            $array['nameMessage'] = 'Please enter your name.';
        }
        if(!isEmail($clientEmail)) {
            $array['emailMessage'] = 'Please insert a valid email address.';
        }
        if($message == '') {
            $array['messageMessage'] = 'Please enter your message.';
        }
        if($clientName != '' && isEmail($clientEmail) && $message != '') {
            // Send email
        $headers = "From: " . $clientName . " <" . $clientEmail . ">" . "\r\n" . "Reply-To: " . $clientEmail;
        mail($emailTo, $subject, $message, $headers);    
        }

    header('Location: thanks.php');
    }        

}

?>