0

I am currently trying to create a form so people can select a number of check boxes with each one corresponding to a person and email address. They can then fill out the rest of the form and send the email to everyone they selected. This is the code I have so far and it does not seem to be working. Any clues and ideas would be great

Form

<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="[email protected]" 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 protected]' => 'name1',
'[email protected]' => 'name2',
'[email protected]' => 'name3',
'[email protected]' => 'name4',
'[email protected]' => 'name5',
'[email protected]' => 'name6',
'[email protected]' => 'name7',
'[email protected]' => 'name8',
'[email protected]' => '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...");
}
}
}
}
4
  • What errors are you getting? Commented Jan 25, 2014 at 7:21
  • nothing its just not sending anything Commented Jan 25, 2014 at 7:22
  • Did you try the php mailer class? phpmailer.worxware.com The difficulty are to construct the headers for all clients Commented Jan 25, 2014 at 7:23
  • I have similar forms on my website (without checkboxes) that work fine. I think it is something to do with they checkbox array but i just can't see it Commented Jan 25, 2014 at 7:28

2 Answers 2

2

You need to add [] to the name of the checkboxes for it to be understood as an array.

Form

 <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="[email protected]" 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 protected]' => 'name1',
    '[email protected]' => 'name2',
    '[email protected]' => 'name3',
    '[email protected]' => 'name4',
    '[email protected]' => 'name5',
    '[email protected]' => 'name6',
    '[email protected]' => 'name7',
    '[email protected]' => 'name8',
    '[email protected]' => '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...");
    }
    }
    }
    } ?>
Sign up to request clarification or add additional context in comments.

2 Comments

this is working for some email but not for others? do you know why this would be?
when I run it with all checkboxes selected all of the resulting mail functions run successfully.
1

With PHP-mailer http://phpmailer.worxware.com/ you have a lot of options to set like

    $userMail = new phpmailer;
    $userMail->IsMail();
    $userMail->IsHTML(true);
    $userMail->Priority = 3;
    $userMail->CharSet = 'utf-8';
    //...
    $userMail->From = '[email protected]';
    $userMail->FromName = SITE_OWNERS;

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

And at the end

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

AND what rossco said!!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.