Something is wrong with my contact form. I'm not receive any emails/

http://f13-preview.awardspace.net/groupeezz.dx.am/groupeezz6/index.html

I was following a online course on Udemy, but I can’t seem to figure out what I did wrong… This is the tutorial I followed.

<?php
	define("TITLE", "Contact Us | Franklin's Fine Dining");
	
?>

	<div id="contact">
		<hr>
		
		<h1>Get in touch with us!</h1>
		

		<?php
	
		// Check for Header Injections
		function has_header_injection($str) {
			return preg_match( "/[\r\n]/", $str );
		}
		
		if (isset($_POST['contact_submit'])) {
			
			// Assign trimmed form data to variables
			// Note that the value within the $_POST array is looking for the HTML "name" attribute, i.e. name="email"
			$name	= trim($_POST['name']);
			$email	= trim($_POST['email']);
			$msg	= $_POST['message']; // no need to trim message
		
			// Check to see if $name or $email have header injections
			if (has_header_injection($name) || has_header_injection($email)) {
				
				die(); // If true, kill the script
				
			}
            
            if (!$name || !$email || !$msg) {
                echo '<h4 class="error">All fields are required.</h4><a href="contact.php" class="button block">There was an error. Please try again./a>';
                exit;
            }
        
            
            // Add the recipeitn email to a variable
            $to = "email@example.com";
            
            // Create a subject
            $subject = "$name sent a message via your contact form";
            
            // Conctruct the message 
            $message .= "Name: $name\r\n";
            $message .= "Email: $email\r\n\r\n";
            $messafe .= "Message: \r\n$msg";
            
            // If the subscribe checkbox was checked
            if (isset($_POST['subscribe']) && $_POST['subscribe'] == 'Subscribe') {
                
                // Add a new line to the $message
                $message .= "\r\n\r\nPlease $email to mailing list.\r\n";
            }
            
            $message = wordwrap($message, 72); // Keep the message neat and tidy
            
        
            // Set the mail headers into a variable
			$headers = "MIME-Version: 1.0\r\n";
			$headers .= "Content-type: text/plain; charset=iso-8859-1\r\n";
			$headers .= "From: " . $name . " <" . $email . ">\r\n";
			$headers .= "X-Priority: 1\r\n";
			$headers .= "X-MSMail-Priority: High\r\n\r\n";
            
            // Send the email!
            mail($to, $subject, $message, $headers);
        ?>

        <!-- Show success message after email has sent -->
		<h5>Thanks for contacting Franklin's!</h5>
		<p>Please allow 24 hours for a response.</p>
		<p><a href="/final" class="button block">&laquo; Go to Home Page</a></p>
        <?php 
            } else {
        ?>
        
        <?php 
            }
        ?>
        <hr>
    </div>

That link is no help; it’s just an introduction to the course. Do you have a relevant link for the contact form?

Maybe this is a stupid question, but…
Have you changed this sample e-mail to actual one?

$to = "email@example.com";

Yes. I edited the actual one out of @xaznbabyx’s post, as we advise against posting personal information like that.

What do you enter as the email address in your form, and is your mail host configured to send emails from that address? Is your development or test environment configured to send emails correctly?

Also note that your comment says “show success message after email has sent”, but you don’t check whether the email was sent or not. I am told that the mail() function isn’t that good for feedback and there are others such as phpmail() that might give better information on whether the mail was sent.

I just created an email account and configured it on awardspace.

This was my response from awardspace troubleshoot.
It appears that you have added a “To” address but not specified the “From” address. In order to send/receive emails whenever a visitor uses your form, a valid email needs to be entered into the “From” header of the PHP mail function. In this particular case a “valid email” means an email that is part of your hosting account (e.g. example@groupeezz.dx.am). Such an email can be created using the Email Accounts tool in your Control Panel:

Where do I add the from email address.I’m new to php.

@TechnoBear here is the link to download it. You may need to click the second link so it can be sent to your email.

@droopsnoot So you’re saying I should us phpmail($to, $subject, $message, $headers) for this section? I seen a lot of tutorials that have mail( ). I’m still new to this and learning.

Also I don’t know how to get the success message working. I only see the “Get in Touch with us”.=/

Again, those links are not helpful in the context of your question. The first is to an entire zip file of course code. Many members will be rightly wary of downloading and opening a zip file from an unknown source. The second link appears to be a course sign-up link to receive the same files.

If you are asking for assistance with implementing a specific part of a course, then showing us that part of the code is relevant and useful, as we can easily check for simple typos or other mistakes which might be causing an issue. Linking to the course introduction, or the entire course material, is not really helpful or relevant, and could be misconstrued as advertising.

You specify the from address in the headers.

@xaznbabyx: is this the same issue you reported here?

If so, I’ll merge the two topics. Having two discussions on the same subject is confusing and leads to duplication of effort.

1 Like

I’m not saying that, just reflecting what others have said on the other occasions these mail issues have popped up. There are a lot of tutorials out there that are quite old, possibly written before other methods became available - for example there are lots that will still talk about using the old-style MySQL() calls rather than mysqli or PDO, they just don’t get updated when a newer technique comes out.

I’m learning as well, I’ve never tried sending email from PHP.

The thing is there are a lot of unknowns with the setup of the PHP mail function on your server. If your web host can’t or wont provide you with any information you might - just might - be better off using phpmail or phpmailer or something similar.

I use PHP’s mail().

IMHO it works great for simple text emails.

It starts to get hairy when HTML emails / attachments etc. i.e. anything more than a plain text email are wanted.

Give it time and check the trash folder, but does running this work?
* change the to and from of course

<?php
  $to = "name@domain.com"; // One of your valid email accounts
  $from = "<sendname@sitedomain.com>"; // an account from your site 
  $subject = "Email Test";
  $message = "This is to test the PHP mail function";
  $headers  = "MIME-Version: 1.0 \r\n" ;
  $headers .= "Content-Type: text/plain \r\n";
  $headers .= "From: " . $from . " \r\n\r\n";
  mail($to, $subject, $message, $headers);
?>

Thanks for your help everyone. I actually used a different php code that I used in one of the previous tutorials and it finally works and I recieved an email. Right now I’m trying to work having a success message on the same page rather than a new page. Plus the errormessage is not working yet. Does anyone know why? But at least I;m one step closer now. =)

Thank you for all of your help guys!

@droopsnoot Thanks for telling me about the phpmailer. I did some research on it and will try the phpmailer in the future. I need to get this site up and running asap. Are there tutorials out there that I can follow? I looked at a few, but I don’t know how good they are. Thanks!

I don’t know if this source of code will be good or not, but at least it works. Anyone have any idea how to get the errormsg working?

<?php

$errorMSG = "";

// NAME
if (empty($_POST["name"])) {
    $errorMSG = "Name is required ";
} else {
    $name = $_POST["name"];
}

// EMAIL
if (empty($_POST["email"])) {
    $errorMSG .= "Email is required ";
} else {
    $email = $_POST["email"];
}

// MESSAGE
if (empty($_POST["message"])) {
    $errorMSG .= "Message is required ";
} else {
    $message = $_POST["message"];
}


$EmailTo = "hello@groupeezz.dx.am";
$Subject = "New Message Received";

// prepare email body text
$Body = "";
$Body .= "Name: ";
$Body .= $name;
$Body .= "\n";
$Body .= "Email: ";
$Body .= $email;
$Body .= "\n";
$Body .= "Subject: ";
$Body .= $msg_subject;
$Body .= "\n";
$Body .= "Message: ";
$Body .= $message;
$Body .= "\n";

// send email
$success = mail($EmailTo, $Subject, $Body, "From:".$email);

// redirect to success page
if ($success && $errorMSG == ""){
   echo "Thank you for contacting Groupeezz! We will be with you shortly";
}else{
    if($errorMSG == ""){
        echo "Something went wrong :(";
    } else {
        echo $errorMSG;
    }
}

?>

Edit: I see that you have already got the script working

1 Like

I’m closing this topic, as the issue has apparently been solved, and there is a new topic for the new issue here: