0

I'm trying to figure out why my php mail form isn't sending an email if the email entered into the input field contains the .edu domain. I can use the form to send to the same email just fine, but if I include the full edu domain address the recipient is to reply to, nothing is sent. The format I'm referring to is "example@mail.universityname.edu", if I drop the edu or even just the "u" in the input field the email sends. I've searched around, but I'm not seeing this same issue from my searches. My php code is below. Thank you

<?php    
if(isset($_POST['email'])) {

    // EDIT THE 2 LINES BELOW AS REQUIRED
    $email_to = "something@gmail.com";
    $email_subject = "Contact Form";


    function died($error) {
        // your error code can go here
        echo "<br /> <br /> We are very sorry, but there were error(s) found with the form you submitted. ";
        echo "These errors appear below.<br /><br />";
        echo $error."<br /><br />";
        echo "Please go back and fix these errors.<br /><br />";
        die();
    }

    // validation expected data exists
    if(!isset($_POST['name']) ||
        !isset($_POST['email']) ||
        !isset($_POST['date1']) ||
        !isset($_POST['date2']) ||
        !isset($_POST['charge']) ||
        !isset($_POST['hours']) ||      
        !isset($_POST['subject'])) {
        died('We are sorry, but there appears to be a problem with the form you submitted.');       
    }

    $name = $_POST['name']; // required
    $email_from = $_POST['email']; // required
    $date1 = $_POST['date1']; // not required
    $date2 = $_POST['date2']; //required
    $charge = $_POST['charge']; //required
    $hours = $_POST['hours']; //required
    $subject = $_POST['subject']; // required

    $error_message = "";

    $string_exp = "/^[A-Za-z .'-]+$/";
  if(!preg_match($string_exp,$name)) {
    $error_message .= 'The First Name you entered does not appear to be valid.<br />';
  }
        $date_exp='/^(19[5-9][0-9]|20[0-4][0-9]|2050)[-](0?[1-9]|1[0-2])[-](0?[1-9]|[12][0-9]|3[01])$/';
  if(!preg_match($date_exp,$date1)) {
    $error_message .= 'The first date you entered does not appear to be valid.<br />';
  }
  if(!preg_match($date_exp,$date2)) {
    $error_message .= 'The second date you entered does not appear to be valid.<br />';
  }
  if(strlen($subject) < 2) {
    $error_message .= 'The reason you entered is not long enough.<br />';
  }
  if(strlen($error_message) > 0) {
    died($error_message);
  }
    $email_message = "Form details below.\n\n";

    function clean_string($string) {
      $bad = array("content-type","bcc:","to:","cc:","href");
      return str_replace($bad,"",$string);
    }

    $email_message .= "Employee Name: ".clean_string($name)."\n";
    $email_message .= "Email: ".clean_string($email_from)."\n";
    $email_message .= "From Date: ".clean_string($date1)."\n";
        $email_message .= "To Date: ".clean_string($date2)."\n";
    $email_message .= "Charge To: ".clean_string($charge)."\n";
    $email_message .= "Hours: ".clean_string($hours)."\n";
    $email_message .= "Reason: ".clean_string($subject)."\n";


// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);  
?>
echo '<script type="text/javascript"> document.location = "http://xx/thanks.html";</script>'

<?php
}
?>
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
namesjj
  • 61
  • 2
  • 8
  • 1
    Start by not silencing the `mail()` function and check what it returns, or if there is any error. – Blackhole Jul 02 '18 at 23:07
  • 1
    Who told you to put an `@` in-front of the mail function? I see this often, and it's wrong. This suppresses all error messages for the call. Remove the `@` to see why the command is failing. – Matt Clark Jul 02 '18 at 23:08
  • 1
    Do you mean "Is not sending" or do you mean "Is not being received"?? There is a not so subtile difference – RiggsFolly Jul 02 '18 at 23:10
  • edu domains tend to have good spam filters. –  Jul 02 '18 at 23:14
  • I saw the @ being used in a few different locations so I threw it in there. Thank you. And it's not being received. This server is hosted elsewhere and I don't have access to see if there are any errors in the logs. Is there a way I can redirect the error to the screen? – namesjj Jul 02 '18 at 23:15
  • The confusing part to me is that I can send to this edu domain perfectly if I use any other domain and even a broken edu mailing address in my input field. – namesjj Jul 02 '18 at 23:17
  • As you are using `@mail()` how can you tell anything for sure. You cannot even tell if the mail is being sent from PHP to its configured mail server. You do realise that `mail()` does not actually send mail it just passes it to a real mail server? – RiggsFolly Jul 02 '18 at 23:17
  • Lots of hosting companies insist that mail is SENT FROM an mail address that actually exists on the hosted email account. As you are sending from some randon email address and therefore potentially a random domain name (not even the hosting domain) Could that be your issue – RiggsFolly Jul 02 '18 at 23:19
  • Use a library like [PHPMailer](https://github.com/PHPMailer/PHPMailer), which acts as its own MTA, and can thus be debugged. `mail()` can only tell you if if the local MTA accepted the email, not why it failed to send – Machavity Jul 02 '18 at 23:25
  • Thanks for the comments, I removed the suppression. I can see some of my attempts in my spam folder, they're not all going through, but it does give a warning in the email saying the address failed to meet required tests for authentication. Maybe that's why it sends sporadically? – namesjj Jul 02 '18 at 23:32

0 Answers0