Solved

Multi mail recipient

Posted on 2004-09-20
9
303 Views
Last Modified: 2006-11-17
Hi,

I've the following form which content a checkbox field with 4 possible value. What I want is to send the form to a recipient depending on checkbox check : here is my code :

<form method='post' action='contact.php'>
E-mail :<input type='text' name='mail' size='20' value='$mail' maxlength='40' />
Your inquirie :
<input class='input2' type='checkbox' name='demande' value='tech'>Technique
<input class='input2' type='checkbox' name='demande' value='sav'>S.A.V.
<input class='input2' type='checkbox' name='demande' value='doc'>Documentation
<input class='input2' type='checkbox' name='demande' value='autre'>Autre

<input name='submit' type='submit' value='Send' />
</form>

--------------------


$toaddress = "technique@test.fr (if 'tech' checked)", "sav@test.fr (if 'sav' checked)", "documentation@test.fr (if 'doc' checked)", "autre@test.fr (if 'autre' checked)";
$subject = "contact";
$mailcontent =
              ."e-mail: ".$email."\n"
              ."Demnande 1: ".$tech."\n"
              ."Demande 2: ".$sav."\n"
              ."Demande 3: ".$doc."\n"
              ."Demande 4: ".$autre."\n"
              ."Message: ".$message."\n";

mail($toaddress, $subject, $mailcontent);


echo "Votre message has been sent


--------------

Does anyone can help

Thanks in advance
   
0
Comment
Question by:yotsse
9 Comments
 
LVL 49

Accepted Solution

by:
Roonaan earned 25 total points
ID: 12101401
is building an array an option?

example:

<?php
$toaddresser = array();
if(isset($_POST['tech'])) $toaddresses[] = 'technique@test.fr';
if(isset($_POST['sav'])) $toaddresses[] = 'sav@test.fr';
if(isset($_POST['doc'])) $toaddresses[] = 'documentation@test.fr';

$mailcontent =
              ."e-mail: ".$email."\n"
              ."Demnande 1: ".$tech."\n"
              ."Demande 2: ".$sav."\n"
              ."Demande 3: ".$doc."\n"
              ."Demande 4: ".$autre."\n"
              ."Message: ".$message."\n";

$mails_send = 0;
foreach($toaddresses as $toaddress)
  if(mail($toaddress, $subject, $mailcontent)) $mails_send++;

if($mails_send == count($toaddresses))
{
  echo 'All your mails are send succesfully';
}
else
{
  echo $mails_send.' out of '.count($toaddresses).' have been send.';
}
?>
0
 
LVL 7

Assisted Solution

by:jdpipe
jdpipe earned 25 total points
ID: 12101477
Hope this helps...

form.php
-------------------------
<?php

$valid_recipients=array(
      'tech'=>"Technique"
      ,'sav'=>"S.A.V."
      ,'doc'=>"Documentation"
      ,'autre'=>"Autres"
);

?><html><head><title>form test</title></head></body>
<form method="post" action="contact.php">

<table><tr>
      <td>E-mail:</td><td><input type='text' name='mail' size='20' value='test@test.cc' maxlength='40' /></td></tr>
<tr><td>
Your inquiry :
</td><td>
<?php
      foreach($valid_recipients as $k=>$v){
            print("<input class='input2' type='checkbox' name='demande[]' value='".htmlspecialchars($k)."'>".htmlspecialchars($v)."\n");
      }
?>
</td></tr>
<tr><td/><td>
<input name='submit' type='submit' value='Send' />
</td></tr></table>
</form>
</body></html>

--------------------------


contact.php
--------------------------
<?php

$valid_recipients=array(
      'tech'=>"Technique"
      ,'sav'=>"S.A.V."
      ,'doc'=>"Documentation"
      ,'autre'=>"Autres"
);

print("<h3>Request</h3><pre>");
print_r($_POST);
print("</pre>");

$toaddress_array=array();

if(isset($_POST['demande'])){      
      foreach($valid_recipients as $k=>$v){
            if(in_array($k,$_POST['demande'])){
                  $toaddress_array[]=$v." <".$k."@test.fr>";
            }
      }
}else{
      die("No recipient selected");
}

$toaddress=join(", ",$toaddress_array);

print("<h3>Mail</h3>");
print("To: ".htmlspecialchars($toaddress)."<br/>");

$subject="Content";

$content=
      "e-mail: ".$email."\n"
    ."Message: ".$message."\n";

mail($toaddress, $subject, $mailcontent);

?>
0
 
LVL 9

Assisted Solution

by:techtonik
techtonik earned 25 total points
ID: 12101529
First modify checkboxes - add [] to 'demande' names like that
<input class='input2' type='checkbox' name='demande[]' value='sav'>S.A.V.
That way you will submit an array with all checked boxes instead of only one latest.

Then define array:

$addresses['tech'] = "technique@test.fr";
$addresses['sav'] = "sav@test.fr";
...
// and here's mass maling script with some sanity checks =)
$addr = array();
foreach ($_POST['demande'] as $p) {
    if (isset($addresses[$p])) {
        $addr[] = $addresses[$p];
    }
}

if(!empty($addr)) {  //send mail
    $addr = array_unique( $addr );
    $toaddress = implode(",", $addr);
    // your mail strings here
    ....  
} else {
    // output error message to check at least one box
}
0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
LVL 27

Assisted Solution

by:Diablo84
Diablo84 earned 25 total points
ID: 12102656
I wasn't going to post to this thread as a working idea had been submitted but as alternatives have been posted i will add this, a much more simplified yet equally effective approach:

<?php
$toadd = $_POST['demande'];
for($i=0;$i<count($toadd);$i++) $toadd[$i] .= "@test.fr";
$toadd = implode(",",$toadd);

$subject = "contact";
$mailcontent =
              ."e-mail: ".$email."\n"
              ."Demnande 1: ".$tech."\n"
              ."Demande 2: ".$sav."\n"
              ."Demande 3: ".$doc."\n"
              ."Demande 4: ".$autre."\n"
              ."Message: ".$message."\n";

mail($toadd, $subject, $mailcontent);

echo "Votre message has been sent";
?>

It will require a couple of modifications to your form (checkboxes need to be a html array and two fields need renaming):

<form method='post' action='contact.php'>
E-mail :<input type='text' name='mail' size='20' value='$mail' maxlength='40' />
Your inquirie :
<input class='input2' type='checkbox' name='demande[]' value='technique'>Technique
<input class='input2' type='checkbox' name='demande[]' value='sav'>S.A.V.
<input class='input2' type='checkbox' name='demande[]' value='documentation'>Documentation
<input class='input2' type='checkbox' name='demande[]' value='autre'>Autre

<input name='submit' type='submit' value='Send' />
</form>
0
 
LVL 49

Expert Comment

by:Roonaan
ID: 12102891
Re: Diable84. You know that with javascript run from your favourites you could change the values of the checkboxes and change them into 'all' or something, quite easily spamming the whole company?

two cts.

-r-
0
 
LVL 27

Expert Comment

by:Diablo84
ID: 12102954
You could achieve the same thing by creating another form and submitting it to the processing page, just a bare bones example to add to the mix... demonstrating how important validation is ;)
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question