Solved

Multi mail recipient

Posted on 2004-09-20
9
292 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 27

Assisted Solution

by:Diablo84
Diablo84 earned 25 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
The viewer will learn how to dynamically set the form action using jQuery.
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.

763 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now