Jump to content

PHP Mail help


egr103

Recommended Posts

Hello,

 

First time poster!

 

I have successfully set up a send email PHP script that is working well. Is there a way to extend the basic code below so that I can send the messages (submitted by using an online form) to 5 different email addresses based on the selection they have made within a drop down menu on the HTML page?

 

For example: The drop down menu will have 5 options. Each of them will be related to their own individual email addresses. When the user selects option 2 & clicks the submit button, an email will be sent to the email address associated to option 2. When another user selects option 4, an email will just be sent to the address associated with option 4 etc.

 

My basic email code so far is this:

<?php
  $name = $_REQUEST['name'] ;
  $email = $_REQUEST['email'] ;
  $selection = $_REQUEST['selection'] ;
  $message = $_REQUEST['comments'] ;

// Check for spammers
if(isset($_POST['Submit'])) {

  // Check for spammers
  if ( preg_match( "/[\r\n]/", $name ) || preg_match( "/[\r\n]/", $email ) ) {
header( "Location: http://idl.newport.ac.uk/legendofkingarthur/error.htm" );
  }

  // Error detection
  if (!isset($_REQUEST['email'])) {
    header( "Location: http://idl.newport.ac.uk/legendofkingarthur/contact-test.htm" );
  }
  elseif (empty($email) || empty($message)) {
    header( "Location: http://idl.newport.ac.uk/legendofkingarthur/error.htm" );
  }
  else {
    mail( "[email protected]", "Home to the Legend of King Arthur Feedback",
       "
Home to the Legend of King Arthur Feedback\n
Fullname: $name\n
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -\n
Email Address: $email\n
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -\n
Nature of Query: $selection\n
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -\n
Comments:\n 
$message\n",
   "From: $email" );
    header( "Location: http://idl.newport.ac.uk/legendofkingarthur/success.htm" );
  }

} else {

header( "Location: http://idl.newport.ac.uk/legendofkingarthur/error.htm" );

}
?>

 

Could anyone give me any pointers as to how to do this?

Link to comment
https://forums.phpfreaks.com/topic/214181-php-mail-help/
Share on other sites

Yes i bet you can, You could just add them to a Bcc field with a comma seperated.

 

or a bit more complex: You could for instance get e-mail addresses from a database and put them in an array.

Than add the value of each array item in To: in a foreach loop. this will result in a few separate emails

 

So in a nutshell:

1) get data (email addresses) from database and put them in an array.

2) do a foreach loop i think and let it loop through all the selected array items, and send an email.

 

Btw i would recommend not to use $_REQUEST but $_POST instead.

Link to comment
https://forums.phpfreaks.com/topic/214181-php-mail-help/#findComment-1114645
Share on other sites

I'm a complete novice when it comes to PHP so with all due respect, what you just said went straight over my head. Could you perhaps shed a little more detail in to what you mean?

 

Just to confirm as well, the form submissions should just go to one email address not all 5.

 

Just out of curiosity, why should I change to use $_POST rather than $_REQUEST?

 

Thanks for your insight so far.

Link to comment
https://forums.phpfreaks.com/topic/214181-php-mail-help/#findComment-1114929
Share on other sites

Just out of curiosity, why should I change to use $_POST rather than $_REQUEST?

 

Purely for security reasons, google "$_REQUEST security issues" to get a better Idea, while your there, google "$_SERVER['PHP_SELF'] security issues"

 

What exactly is the issue here anyway, you want to have a drop down (<select> tag, with 5 options in it) and dependant on what the selection is, send to the email that's is associated with that?

 

Is that about it? Good news is that's quite a simple thing to do.

 

  • Build the html form, putting values to the options as numbers
  • In the receiver script, use a switch to send the email's depending on the value selected in the select tag
  • Display a nice thank you message, and redirect to the form

 

<?php
//set error reporting
error_reporting(E_ALL);

//catch the request
if(isset($_POST['submit']) && ($_POST['submit'] == "submit")){

//cleanse user input
$_POST = array_map('strip_tags', $_POST);
$_POST = array_map('trim', $_POST);

//construct email message subject & headers here
//the from address can be in the header information
$Subject;
$message;
$headers;

//start the switch
switch($_POST['selectionEmail']){
          case "FirstEmail":
          mail($_POST['selectionEmail'], $subject, $message, $header);
          break;

          case "SecondEmail":
          mail($_POST['selectionEmail'], $subject, $message, $header);
          break;

          case "ThirdEmail":
          mail($_POST['selectionEmail'], $subject, $message, $header);
          break;

}//close switch

}
else{
//error handler redirect back to form
header("location: yourFormFileNameHere");
}
?>

 

Typed OTF, and really is just to give you an idea of how to do this, from this you should be able to extrapolate the logic, and see what's going on.

 

Have fun learning anyway.

 

Rw

Link to comment
https://forums.phpfreaks.com/topic/214181-php-mail-help/#findComment-1114933
Share on other sites

After looking through your code I have tried the following but there still appears to be issues. Also, where do I define the different email addresses? Do I do it within FirstEmail, SecondEmail etc.?

 

<?php
//set error reporting
error_reporting(E_ALL);

//catch the request
if(isset($_POST['submit']) && ($_POST['submit'] == "submit")){

//cleanse user input
$_POST = array_map('strip_tags', $_POST);
$_POST = array_map('trim', $_POST);

//construct email message subject & headers here
//the from address can be in the header information
$subject = "Home to the Legend of King Arthur";
$message = "Home to the Legend of King Arthur Feedback\n
Fullname: $name\n
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -\n
Email Address: $email\n
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -\n
Nature of Query: $selection\n
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -\n
Comments:\n
$message\n";
$headers = 'From: $email';

//start the switch
switch($_POST['selectionEmail']){
          case "FirstEmail":
          mail($_POST['selectionEmail'], $subject, $message, $headers);
          break;

          case "SecondEmail":
          mail($_POST['selectionEmail'], $subject, $message, $headers);
          break;

          case "ThirdEmail":
          mail($_POST['selectionEmail'], $subject, $message, $headers);
          break;

}//close switch

}
else{
//error handler redirect back to form
header("location: http://idl.newport.ac.uk/legendofkingarthur/error.htm");
}
?>

 

 

The HTML for JUST the drop down selection reads:

<select id="queryType" name="selection">
<option value="#">---</option>
<option value="1">General</option>
<option value="2">Problem with web site</option>
<option value="3">Trail related</option>
</select>

Link to comment
https://forums.phpfreaks.com/topic/214181-php-mail-help/#findComment-1114954
Share on other sites

Ah, ok. Right, lets see, where's me pen.

<select id="queryType" name="selection">
<option value="#">---</option>
<option value="FirstEmail">General</option>
<option value="SecondEmail">Problem with web site</option>
<option value="ThirdEmail">Trail related</option>
</select>

 

That's the html selection sorted, just make sure that that code is between a set of form tags (<form action="file_to_use_for_processing_data_here" method="post">) and that the submit button is declared like this:-

<input type="submit" name="submit" value="Submit">

Because the script checks the name & the value of it, and I have done it case sensitive...

 

Now, the script:-

<?php
//set error reporting
error_reporting(E_ALL);

//catch the request
if(isset($_POST['submit']) && ($_POST['submit'] == "Submit")){

//cleanse user input
$_POST = array_map('strip_tags', $_POST);
$_POST = array_map('trim', $_POST);

//construct email message subject & headers here
//the from address can be in the header information
$subject = "Home to the Legend of King Arthur";
$message = "Home to the Legend of King Arthur Feedback\n\r";
$message .= "Fullname: ".$name."\n\r";
$message .= "- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -\n\r\";
$message .= "Email Address: ".$email."\n\r";
$message .= "- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -\n\r";
$message .= "Nature of Query: ".$selection."\n\r";
$message .= "- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -\n\r";
$message .= "Comments: $ComentsFromFormTextArea."\n\r";

$EmailArray = array();

$EmailArray['0'] = "Email address number 1";
$EmailArray['2'] = "Email address number 2";
$EmailArray['2'] = "Email address number 3";

$headers = 'From: $email';

//start the switch
switch($_POST['selectionEmail']){
          case "FirstEmail":
          mail($EmailArray['0'], $subject, $message, $headers);
          break;

          case "SecondEmail":
          mail($EmailArray['1'], $subject, $message, $headers);
          break;

          case "ThirdEmail":
          mail($EmailArray['2'], $subject, $message, $headers);
          break;

}//close switch

}
else{
//error handler redirect back to form
header("location: http://idl.newport.ac.uk/legendofkingarthur/error.htm");
}
?>

 

That's a rough guide anyway, I'm on my break so gotta go now, though hopefully you see the idea there anyway.

 

Rw

Link to comment
https://forums.phpfreaks.com/topic/214181-php-mail-help/#findComment-1115042
Share on other sites

Basiclally having a complete mare with this.

 

Its still not functioning correctly & I just can't see where the problem is. I know I must be trying your patience with this but if anyone can help...please don't hold back.

 

HTML:

<!DOCTYPE html>
<head> 
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/> 
<title>Contact us & send us your feedback » Home to the Legend of King Arthur · South East Wales</title> 
<link rel="shortcut icon" href="favicon.ico" />
<meta name="description" content="Contact us. Embark on a journey through South East Wales that will challenge you to discover where King Arthur the legend was born." />
<link rel="stylesheet" href="screen.css" type="text/css" />
<!--[if IE]>
    <link rel="stylesheet" href="screen-ie.css" type="text/css" />
<![endif]-->
<script type="text/javascript" src="../Scripts/jquery.js"></script>
<!-- Form styles only -->
<script type="text/javascript" src="../Scripts/form-styles.js"></script>
</head> 

<body> 

  <div id="header" class="dropShadow">
    	<span class="logos">
    	<a accesskey="3" tabindex="3" href="http://www.newport.ac.uk" title="University of Wales, Newport" class="uwn">University of Wales, Newport</a>
            <a accesskey="2" tabindex="2" href="../index.htm" title="Institute of Digital Learning" class="idl">Institute of Digital Learning</a>
        </span>
        <h1><a accesskey="1" tabindex="1" title="Home to the Legend of King Arthur" href="index.htm">Home to the Legend of King Arthur</a></h1>
   </div>

   <!-- Begin Wrapper -->
   <div id="wrapper-alt">
   
   		 <span class="sub-nav"><h3 class="contact">Send us your feedback | </h3> <a class="head-back" href="javascript: history.go(-1)" title="Go back to the previous page">← Back</a></span>
         
         
         <!-- Begin Left Column --> 
	 <div id="column-one" class="brown"> 

	        <h2 class="step-one"><span>Fill in your contact details</span></h2>  
                
                <form id="contact" method="POST" action="sendmail-select.php">           
                <label for="name">Your full name <span>(required)</span></label>
                <input id="name" class="text" name="name" type="text" tabindex="12" title="" />
                
                <label for="email">Your email <span>(required)</span></label>
                <input id="email" class="text" name="email" type="text" tabindex="13" title="" />
                
                <label for="selection">Nature of Query <span>(required)</span></label>
                <select id="queryType" name="selection">
                  <option value="#">---</option>
                  <option value="FirstEmail">General</option>
                  <option value="SecondEmail">Problem with web site</option>
                  <option value="ThirdEmail">Trail related</option>
                </select>

	 </div> 
	 <!-- End Left Column --> 

	 <!-- Begin Content Column --> 
	 <div id="column-two" class="brown"> 
	       
          <h2 class="step-two"><span>Write your message</span></h2>		 

               		<label for="message">Your message <span>(required)</span></label>
               		<textarea id="message" rows="0" cols="0" name="comments" tabindex="14" title="" ></textarea>
         
	 </div> 
	 <!-- End Content Column --> 

	 <!-- Begin Right Column --> 
	 <div id="column-three" class="brown"> 

	       <h2 class="step-three"><span>Check everything & send us your feedback</span></h2>  
                    <label for="send">Send your feedback <span>(required)</span></label>
               		<input id="send" class="button" type="submit" name="submit" value="Submit" tabindex="15" />
                      
      		   </form>
         
	 </div> 
	 <!-- End Right Column --> 
	<div class="clear"></div>
   </div>
   <!-- End Wrapper -->


<script type="text/javascript">

  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-4468029-7']);
  _gaq.push(['_trackPageview']);

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();

</script>
</body> 
</html> 

 

 

PHP:



<?php
//set error reporting
error_reporting(E_ALL);

//catch the request
if(isset($_POST['submit']) && ($_POST['submit'] == "Submit")){

//cleanse user input
$_POST = array_map('strip_tags', $_POST);
$_POST = array_map('trim', $_POST);

//construct email message subject & headers here
//the from address can be in the header information
$subject = "Home to the Legend of King Arthur";
$message = "Home to the Legend of King Arthur Feedback\n\r";
$message .= "Fullname: ".$name."\n\r";
$message .= "- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -\n\r";
$message .= "Email Address: ".$email."\n\r";
$message .= "- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -\n\r";
$message .= "Nature of Query: ".$selection."\n\r";
$message .= "- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -\n\r";
$message .= "Comments: ".$comments."\n\r";

$EmailArray = array();

$EmailArray['0'] = "[email protected]";
$EmailArray['1'] = "[email protected]";
$EmailArray['2'] = "[email protected]";

$headers = 'From: $email';

//start the switch
switch($_POST['selectionEmail']){
          case "FirstEmail":
          mail($EmailArray['0'], $subject, $message, $headers);
          break;

          case "SecondEmail":
          mail($EmailArray['1'], $subject, $message, $headers);
          break;

          case "ThirdEmail":
          mail($EmailArray['2'], $subject, $message, $headers);
          break;

}//close switch

}
else{
//error handler redirect back to form
header("location: http://idl.newport.ac.uk/legendofkingarthur/error.htm");
}
?>

Link to comment
https://forums.phpfreaks.com/topic/214181-php-mail-help/#findComment-1115072
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.