Solved

Multi mail recipient

Posted on 2004-09-20
9
320 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

 
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

Are You Using the Best Web Development Editor?

The worlds of web hosting and web development are constantly evolving. Every year we see design trends change, coding standards adapt and new frameworks/CMS created. With such a quick pace of change it’s easy to get lost trying to keep up.

See if your editor made the list.

Question has a verified solution.

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

These days socially coordinated efforts have turned into a critical requirement for enterprises.
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

717 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