Email form

Hi, I found this on the site and need something along these lines. I do not understand much of how to code this so please bear with me:

<?php

function sendit($email,$subject,$message)
{
  mail($email,$subject,$message);
}

$themessage1 = "Your special message for deptx";
$themessage2 = "Your special message for depty";
$themessage3 = "Your special message for deptz";
$themessage4 = "Your special message for depta";

switch($_POST["departmenttfromform"])
{
  case "deptx":sendit("dave@yourdomain.com","Dept X Email Info Request", $themessage1);
                     break;
  case "depty":sendit("dan@yourdomain.com","Dept Y Email Info Request", $themessage2);
                     break;
  case "deptz":sendit("fred@yourdomain.com","Dept Z Email Info Request", $themessage3);
                     break;
  default:sendit("becky@yourdomain.com","Dept A Email Info Request", $themessage4);
                     break;
}

?>

What I need is this
1) A form will be sent to a group of addresses depending on what department is selected in a drop down. Is it possible to set up where a case could have multiple emails?

I do not know much about code so if a full example could be given I would appreciate that much. Thank you.

Rob
pertrai1Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

hernst42Commented:
You can call the sendit multiple times like:
switch($_POST["departmenttfromform"])
{
  case "deptx":
    sendit("dave@yourdomain.com","Dept X Email Info Request", $themessage1);
    sendit("anotherone@yourdomain.com","Dept X Email Info Request", $themessage1);
    sendit("anothertwo@yourdomain.com","Dept X Email Info Request", $themessage1);
    break;
  case "depty":
    sendit("dan@yourdomain.com","Dept Y Email Info Request", $themessage2);
    sendit("anothertree@yourdomain.com","Dept X Email Info Request", $themessage1);
    break;
  case "deptz":
    sendit("fred@yourdomain.com","Dept Z Email Info Request", $themessage3);
    break;
}

Open in new window

0
Ali KayahanFull Stack DeveloperCommented:
<html>
<?php if(empty($_POST['department'])){ ?>
<form action="send.php" method="post">
Please Select Department :<select name="department">
                                             <option value="account">Account</option>
                                             <option value="sales">Sales</option>
                                             <option value="support">Support</option>
                                            </select>
<input type="submit" value="Go on">
</form>
<?php else {
switch($_POST['department']) {
case("account") :
$message = "Your special message from account";
break;
case("sales") :
$message = "Your special message from sales";
break;
case("support") :
$message = "Your special message from support";
break;
default :
$message = "Your special message from us";
break;
}
$email ="mail1@domain.com,mail2@domain.com,mail3@domain.com,mail4@domain.com" ;
$subject ="mail from us" ;
 if( mail($email,$subject,$message)) {
    echo "Mail sent" ;
}
}
?>
0
Ali KayahanFull Stack DeveloperCommented:
Save it as send.php
###################################
<html>
<?php if(empty($_POST['department'])){ ?>
<form action="send.php" method="post">
Please Select Department :<select name="department">
                                             <option value="account">Account</option>
                                             <option value="sales">Sales</option>
                                             <option value="support">Support</option>
                                            </select>
<input type="submit" value="Go on">
</form>
<?php
}
 else {
switch($_POST['department']) {
case("account") :
$message = "Your special message from account";
break;
case("sales") :
$message = "Your special message from sales";
break;
case("support") :
$message = "Your special message from support";
break;
default :
$message = "Your special message from us";
break;
}
$email ="mail1@domain.com,mail2@domain.com,mail3@domain.com,mail4@domain.com" ;
$subject ="mail from us" ;
 if( mail($email,$subject,$message)) {
    echo "Mail sent" ;
}
}
?>
</html>
#############################################
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

pertrai1Author Commented:
Hi, it seems like both of these have a little of what I need. hernst42 has the idea of have it to where I can send to multiple emails depending on what department is selected. ali kayahan has shown me how to use the code but the email addresses are already set in the $email. If I could combine these so that I would know how to implement a case where multiple emails are selected depending on the category and how to insert them into the email.

Thank you both for responding and in advance for any and all help to get this done.

Rob
0
Ali KayahanFull Stack DeveloperCommented:
Try to rewrite it like..
###################################
<html>
<?php if(empty($_POST['department'])){ ?>
<form action="send.php" method="post">
Please Select Department :<select name="department">
                                             <option value="account">Account</option>
                                             <option value="sales">Sales</option>
                                             <option value="support">Support</option>
                                            </select>
Please Type Emails :<textarea name="email" cols="10" rows="5"></textarea>
<input type="submit" value="Go on">
</form>
<?php
}
 else {
switch($_POST['department']) {
case("account") :
$message = "Your special message from account";
break;
case("sales") :
$message = "Your special message from sales";
break;
case("support") :
$message = "Your special message from support";
break;
default :
$message = "Your special message from us";
break;
}
$email=$_POST['email'] ;
$subject ="mail from us" ;
 if( mail($email,$subject,$message)) {
    echo "Mail sent" ;
}
}
?>
</html>
#############################################
0
pertrai1Author Commented:
Hi ali kayahan, the code is appreciated. One thing though. There will not be an option for the person filling out the form to enter the email addresses. The code needs to know the email addresses for each category.

Rob
0
Ali KayahanFull Stack DeveloperCommented:
<html>
<?php if(empty($_POST['department'])){ ?>
<form action="send.php" method="post">
Please Select Department :<select name="department">
                                             <option value="account">Account</option>
                                             <option value="sales">Sales</option>
                                             <option value="support">Support</option>
                                            </select>
Please Type Emails :<textarea name="email" cols="10" rows="5"></textarea>
<input type="submit" value="Go on">
</form>
<?php
}
 else {
switch($_POST['department']) {
case("account") :
$message = "Your special message from account";
$email ="account1@mydomain.com,account2@mydomain.com,account3@mydomain.com" ;
break;
case("sales") :
$message = "Your special message from sales";
$email ="sales1@mydomain.com,sales2@mydomain.com,sales3@mydomain.com" ;
break;
case("support") :
$message = "Your special message from support";
$email ="support1@mydomain.com,support2@mydomain.com,support3@mydomain.com" ;
break;
default :
$message = "Your special message from us";
break;
}
$subject ="mail from us" ;
 if( mail($email,$subject,$message)) {
    echo "Mail sent" ;
}
}
?>
</html>
0
Ali KayahanFull Stack DeveloperCommented:
sorry for the previous one ,just removed email input field ;

<html>
<?php if(empty($_POST['department'])){ ?>
<form action="send.php" method="post">
Please Select Department :<select name="department">
                                             <option value="account">Account</option>
                                             <option value="sales">Sales</option>
                                             <option value="support">Support</option>
                                            </select>
<input type="submit" value="Go on">
</form>
<?php
}
 else {
switch($_POST['department']) {
case("account") :
$message = "Your special message from account";
$email ="account1@mydomain.com,account2@mydomain.com,account3@mydomain.com" ;
break;
case("sales") :
$message = "Your special message from sales";
$email ="sales1@mydomain.com,sales2@mydomain.com,sales3@mydomain.com" ;
break;
case("support") :
$message = "Your special message from support";
$email ="support1@mydomain.com,support2@mydomain.com,support3@mydomain.com" ;
break;
default :
$message = "Your special message from us";
break;
}
$subject ="mail from us" ;
 if( mail($email,$subject,$message)) {
    echo "Mail sent" ;
}
}
?>
</html>
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.