根据选中的复选框发送电子邮件

时间:2012-08-30 00:52:21

标签: php forms

我正在学习PHP并且有一个问题我希望有一个简单的答案。我在过去创建了基本表单,并在提交时向用户发送了相应的电子邮件。

这一次,我有3个复选框。根据选中的复选框,我需要发送一封特定的电子邮件。

例如,如果他们希望收到文档1和3,然后提交,则电子邮件将向他们发送下载这两个文档的链接,依此类推。

我不介意它是否发送两封电子邮件,每个复选框选择一封。如果他们选择了两个他们希望收到的文件,他们会收到一封电子邮件,其中包含文件1的链接和另一份文件3的链接。

我不确定我是否能用PHP做到这一点。

4 个答案:

答案 0 :(得分:0)

// create an array with the checkboxes so you can later easily expand it
// the indexes of the array are the fieldnames of the checkboxes
$checkboxes = array(
    'checkbox1' => 'http://example.com/somelink',
    'checkbox2' => 'http://example.com/somelink2',
    'checkbox3' => 'http://example.com/somelink3',
);

// generate mail body based on checked checkboxes
$mailContents = 'Your links:';
foreach($checkboxes as $name => $link) {
    if (isset($_POST[$name])) {
        $mailContents.= $link . "\n";
    }
}   

现在您可以将$mailContents中包含的链接作为字符串邮寄。

答案 1 :(得分:0)

为每个复选框设置一个值,然后使用POST或GET方法发送三个复选框的值,然后执行:

if(check1 == "value"){
   //send email 1
}
if(check2 == "value"){
   //send email 2
}
if(check3 == "value"){
   //send email 3
}

答案 2 :(得分:0)

您需要做的是以下内容: 创建如下所示的复选框:

<input type="checkbox" name="sendDoc1" value="Selected" /> Send me Document 1

然后,当php检查表单时,您可以使用isset并检查输入的内容

if(isset($_POST['sendDoc1']) &&
$_POST['sendDoc1'] == 'Selected')
{
  //Code to send email
}

你可以为每个文件重复一遍

答案 3 :(得分:0)

使用您的文档复选框创建表单

<form method="post" action="test.php">
    Document 1: <input type="checkbox" name="document[]" value="document1" /><br />
    Document 2: <input type="checkbox" name="document[]" value="document2" /><br />
    Document 3: <input type="checkbox" name="document[]" value="document3" /><br />
    <br />
    <input type="hidden" name="send" value="1" />
    <input type="submit" value="Send" />
</form>

和一个处理它的PHP脚本

<?php
    if(isset($_POST) && ($_POST['send'] == 1)){

        $documents = array(
                        'document1' => 'http://www.example.com/document1.doc',
                        'document2' => 'http://www.example.com/document2.doc',
                        'document3' => 'http://www.example.com/document3.doc'
                    );

        $to      = 'nobody@example.com';
        $subject = 'the subject';
        $message = "hello\n\n";

        if(isset($_POST['document']) && count($_POST['document']) > 0){
            foreach($_POST['document'] as $doc){
                if(isset($documents[$doc])){
                    $message .= "Here is ".$documents[$doc]."\n";
                }
            }
        }


        $headers = 'From: webmaster@example.com' . "\r\n" .
            'Reply-To: webmaster@example.com' . "\r\n" .
            'X-Mailer: PHP/' . phpversion();

        mail($to, $subject, $message, $headers);
    }
?>

我认为你有了一般的想法。

相关问题