【问题标题】:Trying to send mail from my Xampp installation with php [duplicate]尝试使用 php 从我的 Xampp 安装发送邮件 [重复]
【发布时间】:2015-11-29 14:20:45
【问题描述】:

我无法使用 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);
   }
?>

【问题讨论】:

    标签: php email smtp xampp localhost


    【解决方案1】:

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

    如果您的网站在 BluehostGoDaddy 等主机上运行,​​则应该已经安装了 SMTP 服务器,并且您可以在专用页面上测试您的脚本。

    【讨论】:

      【解决方案2】:

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

      Swiftmailer是我经常使用的一种,设置起来也很简单。

      【讨论】:

        猜你喜欢
        • 2014-12-03
        • 2017-08-07
        • 2015-07-28
        • 2014-07-29
        • 1970-01-01
        • 2014-08-28
        • 2015-12-16
        • 1970-01-01
        • 2012-12-15
        相关资源
        最近更新 更多