gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 19:13:27 24 4
gpt4 key购买 nike

这个问题在这里已经有了答案:





PHP mail function doesn't complete sending of e-mail

(30 个回答)


4年前关闭。




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

<?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;
}
}
?>

最佳答案

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

如果您需要像上面的示例一样发送个性化的电子邮件,请考虑使用像 SendGrid 这样的服务。或 Amazon Simple Email Service .

关于php mail() 不发送邮件或返回错误消息,在我的系统(localhost)上工作,但在 ubuntu/apache2 上没有,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46762359/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com