php mail()不发送邮件或返回错误消息,在我的系统(localhost)上工作但在ubuntu / apache2上没有

时间:2017-10-16 03:05:23

标签: php apache email ubuntu

所以我有一个简单的php文件,可以从表单中获取数据,并且应该给我发电子邮件。当我在localhost上测试时,它可以在我自己的系统上运行。但是,当我使用apache2在ubuntu上部署它时,它没有用。该文件可能不太漂亮,我第一次尝试使用php发送电子邮件,但我已经在下面包含了php文件。我知道它到达mail()方法并失败,它会激活(!$ mail)条件,但我无法打印$ mail或任何错误,所以我不知道出了什么问题。有任何想法吗?回声簇是我尝试打印某种错误信息而没有运气。另外,我实际上将它发送到我的电子邮件地址,我刚刚为此示例更改了



<?php

if(!isset($_POST['submit'])){
  
  echo "error; you need to submit the form!";
}

$visitor_name = $_POST['name'];
$visitor_message = $_POST['message'];

//incase the email isn't provided
if(empty($_POST['email'])){
  $visitor_email = 'n/a';
} else {
  $visitor_email = $_POST['email'];
}

//incase the phone isn't provided
if(empty($_POST['phone'])){
  $visitor_phone = 'n/a';
  } else {
    $visitor_email = $_POST['email'];
  }

  //incase the phone isn't provided
  if(empty($_POST['phone'])){
    $visitor_phone = 'n/a';
  } else {
    $visitor_phone = $_POST['phone'];
  }


  if(empty($visitor_name) || empty($visitor_message))
  {
    echo "Name and message are mandatory!";
    exit;
  }
  //a function created below for security purposes
if(IsInjected($visitor_email))
{
    echo "Bad email value!";
    exit;
}

// **************************** CODE FOR EMAIL BODY BELOW *****************************************

$email_body = '<html><body>';
$email_body .= "<h2> You've recieved a new message from: $visitor_name, they need a building </h2>";
$email_body .= '<h4> Here is the message: </h4>';
$email_body .= "<p> $visitor_message </p>";
$email_body .= "<h4> Their contact info is below</h4>";
$email_body .= "<ul> <li> email: $visitor_email </li>";
$email_body .= "<li> phone: $visitor_phone </li></ul>";
$email_body .= '</body></html>';


// **************************** END OF CODE FOR EMAIL BODY ****************************************



$to = 'j@example.com';
$subject = "Building Form Submission: $visitor_name Needs a building\r\n";
$headers = "From: building-form@ArchitectureAdvertisingWebsite.com \r\n";
$headers .= 'MIME-Version: 1.0' . "\r\n";

$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";


if($visitor_email != 'n/a'){
  $headers .= "Reply-To: $visitor_email \r\n";
}


$mail = mail($to, $subject, $email_body, $headers);
print_r ($mail);
echo "end test";
if (!$mail){
  echo "Message not sent, there was an error. Please contact Jerrod at .....";
  $errorMessage = error_get_last();
  echo "There was an error: $errorMessage";
  echo "Below the error is printed : ";
  print_r(error_get_last());
} else {
  echo "Message sent";
  header('Location: end.html');
}


// Function to validate against any email injection attempts
function IsInjected($str)
{
  $injections = array('(\n+)',
              '(\r+)',
              '(\t+)',
              '(%0A+)',
              '(%0D+)',
              '(%08+)',
              '(%09+)'
              );
  $inject = join('|', $injections);
  $inject = "/$inject/i";
  if(preg_match($inject,$str))
    {
    return true;
  } 
  else
    {
    return false;
  } 
} 
?>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:1)

以这种方式由PHP脚本直接发送的电子邮件通常会被主要电子邮件提供商标记为垃圾邮件或垃圾邮件。如果您开始以任何数量发送它们,您的电子邮件地址(以及可能的域名)将最终出现在Spamhaus和其他黑名单上。

如果您需要发送上述示例的个性化电子邮件,请考虑使用ios 11 errorSendGrid等服务。