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 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来