We help IT Professionals succeed at work.

Email form

pertrai1
pertrai1 asked
on
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
Comment
Watch Question

Top Expert 2007

Commented:
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

Ali KayahanFull Stack Developer

Commented:
<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" ;
}
}
?>
Ali KayahanFull Stack Developer

Commented:
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>
#############################################

Author

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
Ali KayahanFull Stack Developer

Commented:
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>
#############################################

Author

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
Ali KayahanFull Stack Developer

Commented:
<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>
Full Stack Developer
Commented:
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>

Explore More ContentExplore courses, solutions, and other research materials related to this topic.