dssk35460 2017-10-16 03:05
浏览 84
已采纳

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

This question already has an answer here:

So I have a simple php file that gets data from a form and is supposed to email me. It works on my own system when I was testing it on localhost. But, when I deployed it on ubuntu using apache2, it didn't work. the file may not be pretty, my first attempt to email with php, but i've included the php file below. I know it gets to the mail() method and fails, it activates the (!$mail) conditional, but I can't ever print $mail or any errors so I have no clue what is wrong. any ideas? The cluster of echoes was my attempt to print some kind of error message with no luck. Also, I actually send it to my email address, I just changed it for this example

<?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
";
$headers = "From: building-form@ArchitectureAdvertisingWebsite.com 
";
$headers .= 'MIME-Version: 1.0' . "
";

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


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


$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('(
+)',
              '(+)',
              '(\t+)',
              '(%0A+)',
              '(%0D+)',
              '(%08+)',
              '(%09+)'
              );
  $inject = join('|', $injections);
  $inject = "/$inject/i";
  if(preg_match($inject,$str))
    {
    return true;
  } 
  else
    {
    return false;
  } 
} 
?>

</div>
  • 写回答

1条回答 默认 最新

  • dousuo2812 2017-10-16 03:27
    关注

    Emails sent directly by a PHP script in this way typically get marked as junk or spam by the major email providers. If you start sending them in any quantity, your email address (and possibly domain) will end up on Spamhaus and other blacklists.

    If you need to send individualized emails like your example above, consider using a service like SendGrid or Amazon Simple Email Service.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了