Solved

Multi mail recipient

Posted on 2004-09-20
9
316 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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.

738 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