0

I have a contact form that allows users to upload attachments. Its odd because when I use the contact form, the success message appears but when I check my email, I receive nothing either in my inbox or my spam folder. However, the files that I attach to the message DO appear in my /upload folder so that part seems to be working correctly.

It's just that I don't receive any message or anything. I'm not sure what's wrong. The code is pasted below.

 <?



 if($_SERVER['REQUEST_METHOD'] == "POST")      {                                                            
$allowedExts = array("gif", "jpeg", "jpg", "png", "doc", "pdf", "docx",  "jpg", "docx", "odt", "txt", "msg", "csv", "pps", "ppt", "pptx", "xml", "tar", "m4a", "mp3", "wav", "wma", "mp4", "mov", "flv", "exe");


 for ($i = 1; $i <= 2; $i++) {
 $temp = explode(".", $_FILES["attach".$i]["name"]);
 $extension = end($temp);
 if (in_array($extension, $allowedExts)) {
  move_uploaded_file($_FILES["attach".$i]["tmp_name"],
  "upload/" .$_POST["firstname"]."-".$_FILES["attach".$i]["name"]);
 }
 }

 $to      = 'myemail@gmail.ca';
 $subject = 'Consultation from '.$_POST["firstname"];
 $message = $_POST["message"]."\n\n\n Attachments: ".$_FILES["attach1"]["firstname"]." ".$_FILES["attach2"]["firstname"]." ".$_FILES["attach3"]["firstname"];
  $firstname=$_REQUEST['firstname'];
  $companyname=$_REQUEST['companyname'];
  $email=$_REQUEST['email'];

  if (($firstname=="")||($email=="")||($message=="")) 
     { 
     echo "<strong><p class =greentip>A first name, message, and email   are required, please fill <a href=/consult.php>the form</a> again.</p></strong><br>"; 
    } 
  else{ 
   mail($to, $subject, $message, $firstname, $email);
 echo "<strong><p class = greentip>Your free consultation request has    been received! Expect a detailed response within the next 3 hours.</p></strong>"; 
    }
  }
  ?>

   <form  action="" method="post" enctype="multipart/form-      data">                       

<strong>First Name *</strong><br> 
<input name="firstname" type="text" value=""><br>

<strong>Company Name </strong><br> 
<input name="companyname" type="text" value=""><br>


<strong>Email *</strong><br> 
<input name="email" type="text" value=""<br> 

<strong>Your message *</strong><br> 
<textarea name="message" rows="7" cols="30" placeholder="In your query, include any and all revelant information pertaining to the nature of your writing request. The more specific you are in your request, the more complete we will be in our response!"></textarea><br> 

<strong>Attachments</strong><br> 

   <input name = "attach1" type="file" class="file" />

  <br> 

     <input name = "attach2" type="file" class="file" />

  <br><br>
   <center><input type="submit" value="submit"></center> <br>
    </form>
  • Are you running it locally or on a server? Do you have a mailserver installed? –  May 30 '15 at 23:43
  • The last (fifth) parameter of the mail function is for additional command line options, http://php.net/manual/en/function.mail.php. Are you trying to send a `From` header there? Try putting the `mail` in a conditional as well so you can confirm it is failing. – chris85 May 30 '15 at 23:43
  • Adndres, I do not have a mailserver installed. However, another one of my contact forms uses the exact same code but without the attachments and it works perfectly. – joshmoe123456 May 31 '15 at 00:05

1 Answers1

1

EDIT: You are using the mail() function incorrectly. The function accepts:

mail($to, $subject, $message, $optional_headers, $optional_additional_params);

The way you are trying to pass it $firstname and $email is wrong. Please see the docs.

====================================================================

The php mail() function is VERY basic. It returns a boolean value indicating success, but even when it returns true there are many things that could have gone wrong preventing the email delivery that mail() won't know about.

In my experience Gmail has become extremely picky about receiving emails. It used to be that if you didn't have the correct headers the email would just land in the spam folder, but nowadays I've seen it change to where Gmail just doesn't accept the email at all.

You can find a lot of information about this by googling something like "php mail going to spam gmail" since it used to be that the emails always at least went to the spam folder. And unfortunately the problem is more complex than just setting headers correctly, your email server config and all the things that go along with it have to be just right also.

https://serverfault.com/questions/449244/php-mail-to-gmail-spam

sending email via php mail function goes to spam

Community
  • 1
  • 1
John McMahon
  • 1,605
  • 1
  • 16
  • 21