WordPress.org

Support

Support » How-To and Troubleshooting » Simple PHP email script not working

Simple PHP email script not working

  • I’m using this very simple PHP email script with my contact form:

    <?php
    
    	if (isset($_POST['form_submit']) && $_POST['love']=='love') {
    
    		if (trim($_POST['form_name']) == '') {
    			$hasError = true;
    		} else {
    			$form_name = trim($_POST['form_name']);
    		}
    
    		if (trim($_POST['form_email']) == '')  {
    			$hasError = true;
    		} else
    
    		if (!eregi("^[A-Z0-9._%-]+@[A-Z0-9._%-]+\.[A-Z]{2,4}$", trim($_POST['form_email']))) {
    			$hasError = true;
    		} else {
    			$form_email = trim($_POST['form_email']);
    		}
    
    		if (trim($_POST['form_message']) == '') {
    			$hasError = true;
    		} else {
    
    			if(function_exists('stripslashes')) {
    				$form_comments = stripslashes(trim($_POST['form_message']));
    			} else {
    				$form_comments = trim($_POST['form_message']);
    			}
    		}
    
    		if (!isset($hasError)) {
    			$emailTo = 'info@joaoramos.org';
    			$subject = 'Website feedback';
    			$body = "$form_name disse:\n\n $form_comments";
    			$headers = 'From: joaoramos.org <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $form_email;
    			wp_mail($emailTo, $subject, $body, $headers);
    			$emailSent = true;
    		}
    	}
    
    ?>
    <form action="#contact" method="post" autocomplete="off">
    	<label for="form_name">Name</label>
    	<input type="text" name="form_name" accesskey="n" placeholder="Let's start with the obvious" required />
    
    	<label for="form_email">Email</label>
    	<input type="email" name="form_email" accesskey="e" placeholder="So that I can reach you back" required />
    
    	<label for="form_message">Message</label>
    	<textarea name="form_message" accesskey="m" placeholder="Please be clear" required></textarea>
    
    	<label for="love" class="love">Love?</label>
    	<input type="text" name="love" class="love" placeholder="If you see this, please leave this blank." />
    
    	<input type="submit" name="form_submit" accesskey="s" value="Send away" /> <span class="emailaddress"></em>
    
    	<?php if (isset($hasError)) { ?>
    		<span class="error">Something's wrong.</span>
    	<?php } ?>
    
    	<?php if (isset($emailSent)) { ?>
    		<span class="success">Message sent.</span>
    	<?php } ?>
    </form>

    It doesn’t seem to be working: emails are not sent, I don’t get any errors and my server over at Media Temple seems to be just fine (both PHP and mail server). What exactly am I doing wrong? My guess is that the form submission redirects to the same page without even processing the form. Any suggestions?

  • The topic ‘Simple PHP email script not working’ is closed to new replies.