php表单不发送电子邮件或电子邮件特定内容

时间:2018-05-29 17:02:52

标签: php

我有一个相当简单的PHP表单。它不是什么花哨的东西。但是,我发送时无法在最终电子邮件中显示某些项目。具体来说,

组织,网址,电话,预算,我也无法回复电子邮件发件人。我只是(我的电子邮件),主题(静态)和正文消息。我添加了$ header以查看我是否可以获得回复并添加$ org,$ url,$ budget,$ phone - 但现在我根本没有收到电子邮件。

    <?php 

// define variables and set to empty values
$name_error = $email_error = $phone_error = $url_error = "";
$name = $email = $phone = $org = $url = $budget = $message = $success = "";

//form is submitted with POST method
if ($_SERVER["REQUEST_METHOD"] == "POST") {
  if (empty($_POST["name"])) {
    $name_error = "Name is required";
  } else {
    $name = test_input($_POST["name"]);
    // check if name only contains letters and whitespace
    if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
      $name_error = "Only letters and white space allowed"; 
    }
  }

  if (empty($_POST["email"])) {
    $email_error = "Email is required";
  } else {
    $email = test_input($_POST["email"]);
    // check if e-mail address is well-formed
    if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
      $email_error = "Invalid email format"; 
    }
  }

  if (empty($_POST["phone"])) {
    $phone_error = "Phone is required";
  } else {
    $phone = test_input($_POST["phone"]);
    // check if e-mail address is well-formed
    if (!preg_match("/^(\d[\s-]?)?[\(\[\s-]{0,2}?\d{3}[\)\]\s-]{0,2}?\d{3}[\s-]?\d{4}$/i",$phone)) {
      $phone_error = "Invalid phone number"; 
    }
  }

   if (empty($_POST["org"])) {
    $org = "";
  } else {
    $org = test_input($_POST["org"]);
  }

  if (empty($_POST["url"])) {
    $url_error = "";
  } else {
    $url = test_input($_POST["url"]);
    // check if URL address syntax is valid (this regular expression also allows dashes in the URL)
    if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$url)) {
      $url_error = "Invalid URL"; 
    }
  }

  if (empty($_POST["message"])) {
    $message = "";
  } else {
    $message = test_input($_POST["message"]);
  }

  if (empty($_POST["budget"])) {
    $budget = "";
  } else {
    $budget = test_input($_POST["budget"]);
  }


  if ($name_error == '' and $email_error == '' and $phone_error == ''){
      $url = '';
      $org = '';
      $budget = '';
      $message_body = '';
      unset($_POST['submit']);
      foreach ($_POST as $key => $value){
          $message_body .=  "$key: $value\n";
      }

      $name = 'name';
      $email = 'email';
      $phone = 'phone';
      $url = 'url';
      $budget = 'budget';
      $to = 'myemail@somedomain.com';
      $subject = 'Corinth Designs Contact Form';
      $headers = 'From: '.$email."\r\n" .
        'Reply-To: '.$email."\r\n" .
        'X-Mailer: PHP/' . phpversion();
      if (mail($to, $subject, $phone, $org, $url, $budget, $message)){
          $success = "Message sent, thank you for contacting us!";
          $name = $email = $phone = $org = $url = $message = $budget = '';
      }
  }

}

function test_input($data) {
  $data = trim($data);
  $data = stripslashes($data);
  $data = htmlspecialchars($data);
  return $data;
}

1 个答案:

答案 0 :(得分:0)

您正在使用mail功能错误

来自php文档:

bool mail ( string $to , string $subject , string $message [, mixed $additional_headers [, string $additional_parameters ]] )

我试图理解你的代码并重写了一点

// collect and validate all the required POST Fields
// I presume you have then variables filled with data -like $email, $url, $phone, $name, ...

if ($name_error == '' and $email_error == '' and $phone_error == ''){
      $message_body = "Hello ".$name;
      $message_body .= 'Phone Number: '.$phone;
      $message_body .= 'Url: '.$url;
      //... until your email content is filled - just the normal email text you want in your email :)

      // recipients email address
      $to = $email;
      $subject = 'Corinth Designs Contact Form';

      // $email_sender should be a real email address something like admin@yourdomain.com
      $headers = 'From: '.$email_sender."\r\n" .
        'Reply-To: '.$email."\r\n" .
        'X-Mailer: PHP/' . phpversion();

      //sends the email to $to with subject $subject and email-content $message_body
      if (mail($to, $subject, $message_body, $headers)){
          $success = "Message sent, thank you for contacting us!";

      }
  }