?
Solved

Multi mail recipient

Posted on 2004-09-20
9
Medium Priority
?
326 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 100 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 100 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 100 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
Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

 
LVL 27

Assisted Solution

by:Diablo84
Diablo84 earned 100 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

765 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