php email:使用复选框的多个沉淀剂

时间:2014-01-25 07:18:00

标签: php forms email

我目前正在尝试创建一个表单,以便人们可以选择多个复选框,每个复选框对应一个人和电子邮件地址。然后,他们可以填写表单的其余部分并将电子邮件发送给他们选择的每个人。这是我到目前为止的代码,它似乎没有工作。任何线索和想法都会很棒

表格

<form action="mail.php" method="post"  name="contact_form">
<input type="checkbox" name="emails" value="name1" id="thing"/><label for="thing" class="name1"></label>
<input type="checkbox" name="emails" value="name2" id="thing2"/><label for="thing2" class="name2"></label>
<input type="checkbox" name="emails" value="name3" id="thing3"/><label for="thing3" class="name3"></label>
<input type="checkbox" name="emails" value="name4" id="thing4"/><label for="thing4" class="name4"></label>
<input type="checkbox" name="emails" value="name5" id="thing5"/><label for="thing5" class="name5"></label>
<input type="checkbox" name="emails" value="name6" id="thing6"/><label for="thing6" class="name6"></label>
<input type="checkbox" name="emails" value="name7" id="thing7"/><label for="thing7" class="name7"></label>
<input type="checkbox" name="emails" value="name8" id="thing8"/><label for="thing8" class="name8"></label>
<input type="checkbox" name="emails" value="name9" id="thing9"/><label for="thing9" class="name9"></label>
<div class="title_bar">Name</div>
<input type="text" name="name" placeholder="John Urbanist" required/>
<div class="title_bar">Email</div>
<input type="email" name="from_email" placeholder="john_urbanist@example.com" required/>
<div class="title_bar" style="height:202px;">Additional Comments</div><textarea name="additional" maxlength="300" required></textarea>
<div style="clear:both;display:block;"></div>
<button class="buttonsend submit" type="submit">Send Letter</button>
</form>

Mail.php

$mails = array(
'email@domain.com' => 'name1',
'email2@domain.com' => 'name2',
'email3@domain.com' => 'name3',
'email4@domain.com' => 'name4',
'email5@domain.com' => 'name5',
'email6@domain.com' => 'name6',
'email7@domain.com' => 'name7',
'email8@domain.com' => 'name8',
'email9@domain.com' => 'name9'
);
$name = $_REQUEST["name"];
$subject = 'Hello';
$additional = $_REQUEST["additional"];
$from_email = $_REQUEST["from_email"];
$headers = "From: ".$from_email."\r\n" .
"X-Mailer: php";

foreach ($_POST['emails'] as $value) {
if (in_array($value, $mails)) {
$addrs = array_keys($mails, $value);

foreach ($addrs as $addr) {
if (mail($addr, $subject, $additional, $headers)) {
echo("Message sent!");
} else {
echo("Message delivery failed...");
}
}
}
}

2 个答案:

答案 0 :(得分:2)

您需要将[]添加到复选框的名称中,以便将其理解为数组。

表格

 <form action="mail.php" method="post"  name="contact_form">
    <input type="checkbox" name="emails[]" value="name1" id="thing"/><label for="thing" class="name1"></label>
    <input type="checkbox" name="emails[]" value="name2" id="thing2"/><label for="thing2" class="name2"></label>
    <input type="checkbox" name="emails[]" value="name3" id="thing3"/><label for="thing3" class="name3"></label>
    <input type="checkbox" name="emails[]" value="name4" id="thing4"/><label for="thing4" class="name4"></label>
    <input type="checkbox" name="emails[]" value="name5" id="thing5"/><label for="thing5" class="name5"></label>
    <input type="checkbox" name="emails[]" value="name6" id="thing6"/><label for="thing6" class="name6"></label>
    <input type="checkbox" name="emails[]" value="name7" id="thing7"/><label for="thing7" class="name7"></label>
    <input type="checkbox" name="emails[]" value="name8" id="thing8"/><label for="thing8" class="name8"></label>
    <input type="checkbox" name="emails[]" value="name9" id="thing9"/><label for="thing9" class="name9"></label>
    <div class="title_bar">Name</div>
    <input type="text" name="name" placeholder="John Urbanist" required/>
    <div class="title_bar">Email</div>
    <input type="email" name="from_email" placeholder="john_urbanist@example.com" required/>
    <div class="title_bar" style="height:202px;">Additional Comments</div><textarea name="additional" maxlength="300" required></textarea>
    <div style="clear:both;display:block;"></div>
    <button class="buttonsend submit" type="submit">Send Letter</button>
    </form>

mail.php

<?php
    //print_r($_POST['emails']);
    $mails = array(
    'email@domain.com' => 'name1',
    'email2@domain.com' => 'name2',
    'email3@domain.com' => 'name3',
    'email4@domain.com' => 'name4',
    'email5@domain.com' => 'name5',
    'email6@domain.com' => 'name6',
    'email7@domain.com' => 'name7',
    'email8@domain.com' => 'name8',
    'email9@domain.com' => 'name9'
    );
    $name = $_REQUEST["name"];
    $subject = 'Hello';
    $additional = $_REQUEST["additional"];
    $from_email = $_REQUEST["from_email"];
    $headers = "From: ".$from_email."\r\n" .
    "X-Mailer: php";

    foreach ($_POST['emails'] as $value) {
    if (in_array($value, $mails)) {
    $addrs = array_keys($mails, $value);

    foreach ($addrs as $addr) {
    if (mail($addr, $subject, $additional, $headers)) {
    echo("Message sent!");
    } else {
    echo("Message delivery failed...");
    }
    }
    }
    } ?>

答案 1 :(得分:1)

使用PHP邮件程序http://phpmailer.worxware.com/,您可以设置很多选项,如

    $userMail = new phpmailer;
    $userMail->IsMail();
    $userMail->IsHTML(true);
    $userMail->Priority = 3;
    $userMail->CharSet = 'utf-8';
    //...
    $userMail->From = 'm2@me.com';
    $userMail->FromName = SITE_OWNERS;

    foreach($whatever as $key => $what) {
        $userMail->AddAddress($key, $what);
    }
    //etc...
    $userMail->WordWrap = 50;
    //etc...

最后

if($userMail->Send()){
    //do yes
} else {
    //do no
}

rossco说的是什么!!