尝试使用php从我的Xampp安装发送邮件

时间:2015-09-03 16:22:39

标签: php email smtp xampp localhost

我无法使用phps mail()函数和xampp发送任何邮件,试图阅读教程但其中大部分都已过时而且对我不起作用。

我正在尝试从我正在开发的网站上构建的表单发送电子邮件,但我想与我的本地主机进行测试。

 $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);
   }
?>

2 个答案:

答案 0 :(得分:0)

PHP的本机mail函数通常需要在php.ini文件中进行一些配置。但是,有很多第三方软件包可以让您使用Gmail SMTP服务器更可靠地发送电子邮件。

Swiftmailer是我经常使用的,它很容易设置。

答案 1 :(得分:0)

您必须安装本地SMTP服务器才能使mail()功能正常工作。希望它不会太旧,这个视频可能会帮助你: https://www.youtube.com/watch?v=TO7MfDcM-Ho

如果您的网站位于BluehostGoDaddy等主机上,则应已安装SMTP服务器,您可以在私有网页上测试脚本。