0

I can 't send any mail using phps mail() function and xampp, trying to read tutorials but most of them are outdated and do not work for me .

i'm trying to send an e-mail from a form i build on a website i'm developing, but i want to test it with my local host.

 $name = $comment = $number = $email = "";

 if (isset($_POST['name']))
   $name = fix_string($_POST['name']);
 if (isset($_POST['comment']))
   $comment  = fix_string($_POST['comment']);
 if (isset($_POST['number']))
   $age      = fix_string($_POST['number']);
 if (isset($_POST['email']))
   $email    = fix_string($_POST['email']);

if (isset($_POST['submit'])){
    $to = "efeimoloame1@gmail.com"; // this is your Email address

    $subject = "Form submission";
    $subject2 = "Copy of your form submission";
    $message = $name . " "  . " wrote the following:" . "\n\n" . $comment;
    $message2 = "Here is a copy of your message " . $name . "\n\n" . $comment;

    $headers = "From:" . $email;
    $headers2 = "From:" . $to;
    mail($to,$subject,$message,$headers);
    $maile = mail($email,$subject2,$message2,$headers2);
    // sends a copy of the message to the sender
  if($maile) { echo "Mail Sent. Thank you " . $name . ", we will contact you shortly.";}
  else{
    echo "j";
  }
    // You can also use header('Location: thank_you.php'); to redirect to another
}

 $fail  = validate_name($name);
 $fail .= validate_comment($comment);
 $fail .= validate_number($number);
 $fail .= validate_email($email);



 if ($fail == "")
 {
   echo "</head><body>Form data successfully validated:
     $name, $comment,  $number, $email.</body></html>";

   // This is where you would enter the posted fields into a database,
   // preferably using hash encryption for the password.

   exit;
 }


 function validate_name($field)
 {
     return ($field == "") ? "No Name was entered<br>": "";
 }

 function validate_comment($field)
 {
     return($field == "") ? "No comment was entered<br>" : "";
 }

   function validate_number($field)
   {
     if ($field == "") return "No phone number  was entered<br>";
     else if ($field.strlen()<11 || $field.strlen()>11)
       return "Phone number should be 11 digits <br>";
     return "";
   }

   function validate_email($field)
   {
     if ($field == "") return "No Email was entered<br>";
       else if (!((strpos($field, ".") > 0) &&
                  (strpos($field, "@") > 0)) ||
                   preg_match("/[^a-zA-Z0-9.@_-]/", $field))
         return "The Email address is invalid<br>";
     return "";
   }

   function fix_string($string)
   {
     if (get_magic_quotes_gpc()) $string = stripslashes($string);
     return htmlentities ($string);
   }
?>
Efe Imoloame
  • 99
  • 11

2 Answers2

0

PHP's native mail function often requires some configuration in your php.ini file. However, there are a lot of third party packages which will allow you to send e-mails much more reliably using the Gmail SMTP server.

Swiftmailer is one that I often use, and it is very easy to set up.

Amo
  • 2,884
  • 5
  • 24
  • 46
0

You have to install a local SMTP server for the mail() function to work. Hoping it's not too old, this video may help you with that: https://www.youtube.com/watch?v=TO7MfDcM-Ho

If your website is live on a host such as Bluehost or GoDaddy, then an SMTP server should be already installed, and you can test your script on a private page.

Jon Kantner
  • 593
  • 2
  • 10
  • 29