Help with email processor

I am not certain why the following code is not working. When the submit button is pushed it will clear the form, but not send an email. (Please note I copied the code from my dreamweaver program). Have virtually the same code on another server and it is working, but this one is not working. I am not sure why.

<!-- BEGIN FORM -->
<p class="body-text">Fields marked with an * are required. </p>
<?php 
	$action=$_REQUEST['action']; 
	if ($action=="")    												/* display the contact form */ 
    	{ 
?>  
        <form action="" enctype="multipart/form-data" method="POST" class="body-text">
          Name: <br /><input name="name" type="text" size="30" /> *<br />
          <!--Last Name: <br /><input name="Last-Name" type="text" size="30" required> *<br> -->
          Email: <br /><input name="email" type="text" size="50" /> *<br />
            Event Type: <br />
            <label>
            <select name="eventtype" class="body-text" id="type" width="30">
              <option>Camp event</option>
              <option>Brigade event</option>
              <option>Division event</option>
              <option>National event</option>
              </select>
            </label> *<br /> 
            Message: <br /><textarea name="message" cols="50" rows="10"></textarea> *<br />
          <!--<input type="hidden" name="sendtoemail"><br /> -->
          <input type="submit" value="Send Form" /><br />
        </form>
    <?php 
    }  
else                												/* send the submitted data */ 
    { 
    	$name=$_REQUEST['name']; 
    	$email=$_REQUEST['email']; 
		$event=$_REQUEST['eventtype'];						
   		$message=$_REQUEST['message'];
		$to="[email protected]";
		$link_address = 'www.alscv.org/events.html';
    if (($name=="")||($email=="")||($event=="")||($message=""))		/* required fields */ 
        { 
			echo "All fields are required.  Please fill <a href='$link_address'>Link</a> again";
        	/*echo "All fields are required.  Please fill <a href=\"\">the form</a> again.";*/
        } 
    else
		{         
        	$from="From: $name<$email>\r\nReturn-path: $email"; 					/* who the email is from */
        	$subject="AL Division SCV Event Form Message"; 							/* email subject */
			$emailmessage=$name."\r\n".$email."\r\n".$event."\r\n".$message;		/* builds email message */
        	mail($to, $subject, $from, $emailmessage); 								/* sends mail to email address and what is sent */
			echo "Email sent!"; 
        } 
    }   
	?>

http://alscv.org/test/events-form.php

Take a close look to php’s mail function. You will see that there is no ‘from’ parameter. you should set it in the mail headers (see example 2). Besides that you have to realize that php’s mail function is just a pass through to the underlying system that should handle your mail. If that system is not working then you won’t be able to send emails from php. If you want to have more possibilities or an easier usage you should consider PHPMailer or SwiftMailer

Would this be more accurate?

<!-- BEGIN FORM -->
<p class="body-text">Fields marked with an * are required. </p>
<?php 
	$action=$_REQUEST['action']; 
	if ($action=="")    												/* display the contact form */ 
    	{ 
?>  
        <form action="" enctype="multipart/form-data" method="POST" class="body-text">
          Name: <br /><input name="name" type="text" size="30" /> *<br />
          <!--Last Name: <br /><input name="Last-Name" type="text" size="30" required> *<br> -->
          Email: <br /><input name="email" type="text" size="50" /> *<br />
            Event Type: <br />
            <label>
            <select name="eventtype" class="body-text" id="type" width="30">
              <option>Camp event</option>
              <option>Brigade event</option>
              <option>Division event</option>
              <option>National event</option>
              </select>
            </label> *<br /> 
            Message: <br /><textarea name="message" cols="50" rows="10"></textarea> *<br />
          <!--<input type="hidden" name="sendtoemail"><br /> -->
          <input type="submit" value="Send Form" /><br />
        </form>
    <?php 
    }  
else                												/* send the submitted data */ 
    { 
    	$name=$_REQUEST['name']; 
    	$email=$_REQUEST['email']; 
		$event=$_REQUEST['eventtype'];						
   		$message=$_REQUEST['message'];
		$to="[email protected]";
		$link_address = 'www.alscv.org/events.html';
    if (($name=="")||($email=="")||($event=="")||($message=""))		/* required fields */ 
        { 
			echo "All fields are required.  Please fill <a href='$link_address'>Link</a> again";
        	/*echo "All fields are required.  Please fill <a href=\"\">the form</a> again.";*/
        } 
    else
		{         
        	$headers = 'From: $email' . "\r\n" .'Reply-To: [email protected]' . "\r\n" .'X-Mailer: PHP/' . phpversion();
			/*$headers="From: $name<$email>\r\nReturn-path: $email"; 				/* who the email is from */
        	$subject="AL Division SCV Event Form Message"; 							/* email subject */
			$emailmessage=$name."\r\n".$email."\r\n".$event."\r\n".$message;		/* builds email message */
        	mail($to, $subject, $headers, $emailmessage); 							/* sends mail to email address and what is sent */
			echo "Email sent!"; 
        } 
    }   
	?>

I think you should skip using the mail() function as there’s a high risk of your script ending up as a spambot, because you let people insert any header, what is called “email-injection”. Wrapper classes like the already mentioned PHPMailer and SwiftMailer protect against that.

can you be more specific. maybe provide an example that I can use?

The documentation of each project (PHPMailer, Swiftmailer) have a lot of examples.

Sponsor our Newsletter | Privacy Policy | Terms of Service