PHP邮件脚本,无法发送电子邮件

4

我正在尝试通过一个简单的PHP邮件脚本发送邮件。我已经检查了代码很多次,页面跳转到“感谢页”,好像邮件应该已经发送了,但是我还没有收到电子邮件。有任何想法为什么这不起作用吗?

<?php
/*
This first bit sets the email address that you want the form to be submitted to.
You will need to change this value to a valid email address that you can access.
*/
$webmaster_email = "email@mail.com";
$subject = "Contact Us";

/*
This bit sets the URLs of the supporting pages.
If you change the names of any of the pages, you will need to change the values here.
*/
$feedback_page = "feedback_form.html";
$error_page = "error_message.html";
$thankyou_page = "thank_you.html";

/*
This next bit loads the form field data into variables.
If you add a form field, you will need to add it here.
*/
$email_address = $_REQUEST['email_address'] ;
$comments = $_REQUEST['comments'] ;

/*
The following function checks for email injection.
Specifically, it checks for carriage returns - typically used by spammers to inject a CC list.
*/
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;
    }
}

// If the user tries to access this script directly, redirect them to the feedback form,
if (!isset($_REQUEST['email_address'])) {
header( "Location: $feedback_page" );
}

// If the form fields are empty, redirect to the error page.
elseif (empty($email_address) || empty($comments)){
header( "Location: $error_page" );
}

// If email injection is detected, redirect to the error page.
elseif ( isInjected($email_address) ) {
header( "Location: $error_page" );
}

// If we passed all previous tests, send the email then redirect to the thank you page.
else {
mail( $webmaster_email, $subject,
  $comments, "From: " . $email_address);
header( "Location: $thankyou_page" );
}
?>

你能打印出电子邮件地址以确保地址正确吗?如果成功,邮件函数返回true,否则返回false。你应该打印它或使用if(mail(...)) header... else header(...error_page)。 - ajon
1
你有检查过服务器日志吗?这封邮件可能被标记为垃圾邮件了吗? - j08691
您的电子邮件头可能存在错误或格式不正确。在这种情况下,收件人的邮件服务器会拒绝您的邮件,即使邮件功能成功发送邮件,您也无法收到它。 - Anirudh Ramanathan
3个回答

3

0
你在php.ini文件中指定了smtp主机吗?如果是,它是否正确?
此外,禁用邮件函数的头部并打印输出,以查看是否成功。还建议在php.ini文件中启用错误报告。

0

email@mail.com 是一个有效的电子邮件地址吗?


这并没有提供问题的答案。如果您想对作者进行批评或请求澄清,请在他们的帖子下留言。 - David B
如果作者觉得这个检查有用的话,这可能就是答案。 - Teena Thomas
然而,这并不是回答问题的方法。回答应该格式化为事实 - 如果你想清除一些初始条件,可以使用注释。一个有用的提示最好留在注释中(或对另一个答案进行编辑)。 - David B

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接