【问题标题】:PHP contact form only returns error messagePHP 联系表单仅返回错误消息
【发布时间】:2020-12-13 17:26:12
【问题描述】:

我的 PHP 联系表单不起作用。当我尝试提交消息时,我只收到错误消息“发送电子邮件时出错!”。

我刚刚学习 PHP,包含我的 HTML 的 PHP 文件是否需要在任何地方打开 "<?php" & a closing "?>"

感谢任何帮助、提示和建议。

contactForm.php

<?php if(isset($_POST['cf_submit'])) {    
   $errors = array();
   $success = null;
   
   $required_fields['cf_name'] = 'You are required to enter your name.';
   $required_fields['cf_email'] = 'You are required to enter your e-mail Address.';
   $required_fields['cf_subject'] = 'You are required to enter a subject.';
   $required_fields['cf_message'] = 'You are required to enter a message.';
                   
   foreach($_POST as $key => $value) {
      if(array_key_exists($key, $required_fields)) {
         if(trim($_POST[$key]) === '') {
             $errors[$key] = $required_fields[$key];
          }
       }
    }

    if(empty($errors)) {
         $to = "MY EMAIL ADDRESS";
         $subject = $_POST['cf_subject'];
         $name_field = $_POST['cf_name'];
         $email_field = $_POST['cf_email'];
         $message = $_POST['cf_message'];
        
         $body = "From: $name_field\n E-Mail: $email_field\n Message:\n $message";
         $success = mail($to, $subject, $body);
    }
    
    if($success) {
              echo "<p><strong>Thank you for getting in touch. Expect to hear back from us soon.</strong></p>";
         } else {
              echo "Error sending email!";
         }
    }

    if(!empty($errors)) {
         echo "<strong>Please check the following errors:</strong><br/><br/>";
         echo "<ul>";
         foreach($errors as $value) {         
              echo "<li>$value</li>";      
         }
         echo "</ul>";
    } ?>

HTML 表单

<form action="contactForm.php" method="post">
<!-- Name -->
<input name="cf_name" type="text" id="cf_name" class="cf_input" value="<?php if(isset($_POST['cf_name'])) echo $_POST['cf_name'];?>" placeholder="Name">
    
<!-- Email -->
<input name="cf_email" type="email" id="cf_email" class="cf_input" value="<?php if(isset($_POST['cf_email'])) echo $_POST['cf_email'];?>" placeholder="Email">
    
<!-- Subject -->
<input name="cf_subject" type="text" id="cf_subject"  class="cf_input" value="<?php if(isset($_POST['cf_subject'])) echo $_POST['cf_subject'];?>" placeholder="Subject">
    
<!-- Message -->
<textarea name="cf_message" cols="45" rows="3" id="cf_message" value="<?php if(isset($_POST['cf_message'])) echo $_POST['cf_message'];?>" placeholder="Enter message here" class="cf_text"></textarea>
                                        
<!-- Submit -->
<button type="submit" name="cf_submit">Send Message</button>
</form>

【问题讨论】:

  • 您确定您的服务器允许您通过 PHP 发送电子邮件吗?
  • 感谢您的评论!我正在使用 XAMPP 处理文件,我会看看这是否合适。
  • xamp 默认无法发送电子邮件,这就是为什么您总是收到错误消息的原因,您必须设置 xamp 以发送电子邮件或使用 phmailer 库并将任何/您的 gmail 帐户与它连接并发送电子邮件

标签: php html contact-form


【解决方案1】:

https://github.com/PHPMailer/PHPMailer下载php mailer并使用。

要使用它:首先你需要创建一个名为“phpmailer”的文件夹。 然后把你下载的php mailer文件夹放进去。

然后使用此代码:

<?php
    header("Access-Control-Allow-Origin: *");
    header('Access-control-Allow-Headers: Authorization,Content-Type ,X-Auth-Token , Origin');
   $username=$_POST["email"];
   $password=$_POST["password"];
   $to=$_POST["to"];
   $subject=$_POST["subject"];
   $body=$_POST["body"];

   // Import PHPMailer classes into the global namespace
   // These must be at the top of your script, not inside a function
   use PHPMailer\PHPMailer\PHPMailer;
   use PHPMailer\PHPMailer\SMTP;
   use PHPMailer\PHPMailer\Exception;

   // Load Composer's autoloader
   require 'phpmailer/src/PHPMailer.php';
   require 'phpmailer/src/SMTP.php';
   require 'phpmailer/src/Exception.php';

   // Instantiation and passing `true` enables exceptions
   $mail = new PHPMailer(true);

   try {
    //Server settings
     $mail->SMTPDebug = 2;                      // Enable verbose debug output
     $mail->isSMTP();                                            // Send using SMTP
     $mail->Host       = 'smtp.gmail.com';                    // Set the SMTP    server    to send through
     $mail->SMTPAuth   = true;                                   // Enable SMTP      authentication
    $mail->Username   = $username;                     // SMTP username
    $mail->Password   = $password;                               // SMTP password
    $mail->SMTPSecure = 'tls';         // Enable TLS encryption; `  PHPMailer::ENCRYPTION_SMTPS` encouraged
    $mail->Port       = 587;                                    // TCP port to connect to, use 465 for `PHPMailer::ENCRYPTION_SMTPS` above

//Recipients
   $mail->setFrom('no-reply@gmail.com',$username);
   $mail->addAddress($to);     // Add a recipient


   // Attachments
   // $mail->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
   // $mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name

   // Content
   $mail->isHTML(true);                                  // Set email format to HTML
   $mail->Subject = $subject;
   $mail->Body    = $body;
   //$mail->AltBody =;

   $mail->send();

   echo "<script>";
   echo "window.alert('Email was sent')";
   echo "</script>";


  } catch (Exception $e) {
  echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
  }
 ?>

另外,请确保您开启“不太安全的应用访问”以使其正常工作。 因为此类代码使用现有电子邮件发送电子邮件。 最简单的方法是使用您的 gmail 帐户。

【讨论】:

  • “邮件功能不能用了”PHP mail function 活得很好。请考虑更正错误的断言。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-19
  • 2012-09-19
  • 2013-09-04
相关资源
最近更新 更多