PHP mail()不';不起作用,但不会出错
我正在尝试从PHP页面发送邮件。我没有收到任何错误,但它不会回显代码中包含的验证错误/成功消息。我正在尝试从Cloud9发送,这可能是不可能的,但它不应该至少显示我的成功/错误消息吗 [更新]现在它确实会显示我的错误消息,但每次我提交它时,即使我输入了所需的数据。并且没有关于发送是否失败的信息 PHP:PHP mail()不';不起作用,但不会出错,php,html,email,Php,Html,Email,我正在尝试从PHP页面发送邮件。我没有收到任何错误,但它不会回显代码中包含的验证错误/成功消息。我正在尝试从Cloud9发送,这可能是不可能的,但它不应该至少显示我的成功/错误消息吗 [更新]现在它确实会显示我的错误消息,但每次我提交它时,即使我输入了所需的数据。并且没有关于发送是否失败的信息 PHP: ... 波沙尔吉 ... 以及附带的mail.php: <?php $to = "mymail@gmail.com"; if (isset($_POST['
...
波沙尔吉
...
以及附带的mail.php:
<?php
$to = "mymail@gmail.com";
if (isset($_POST['contact-name'], $_POST['contact-mail'], $_POST['contact-text'])) {
if (empty($_POST['contact-name'])) {
$errors[] = "Molimo unesite Vaše ime";
} else {
$contact_name = htmlentities($_POST['contact-name']);
}
if (empty($_POST['contact-mail'])) {
$errors[] = "Molimo unesite Vašu email adresu.";
} else if (strlen($_POST['contact-mail']) > 60) {
$errors[] = "Vaša email adresa je predugačka.";
} else if (filter_var($_POST['contact-mail'], FILTER_VALIDATE_EMAIL) === false ) {
$errors[] = "Unesite validnu email adresu.";
} else {
$contact_mail = "<" . htmlentities($_POST['contact-mail']) . ">";
}
if (empty($_POST['contact-text'])) {
$errors[] = "Molimo unesite poruku.";
} else {
$contact_text = htmlentities($_POST['contact-text']);
}
}
?>
我没有包括css或javascript,我想它们是不相关的。JQuery只是在提交表单后重置表单。如果需要帮助,我尝试了stackoverflow和google?邮件将通过服务器发送,因此您需要发布表单,服务器将发送邮件并回复结果 相同的php脚本用于生成邮件和发送邮件 因此,首先,当您打开页面时。服务器将生成表单,因为表单未过帐。并且未生成$u POST变量 但是当您尝试提交表单时,它不会被提交,因为您在提交时设置了“return false;” 要在不加载页面的情况下提交表单,需要异步发送数据(使用Ajax,搜索Google以获取更多信息)
更重要的是,即使您删除了“Return false”代码,它也不会工作,因为mail()函数返回布尔值true和false,但从不回显它。因此,您可以将其转储。尝试简化版本吗
<?php
// Check for empty fields
if(empty($_POST['name']) ||
empty($_POST['email']) ||
empty($_POST['phone']) ||
empty($_POST['message']) ||
!filter_var($_POST['email'],FILTER_VALIDATE_EMAIL))
{
echo "No arguments Provided!";
return false;
}
$name = $_POST['name'];
$email_address = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];
// Create the email and send the message
$to = 'example@example.com'; // Add your email address inbetween the '' replacing yourname@yourdomain.com - This is where the form will send a message to.
$email_subject = "New Email: $name";
$email_body = "You have a new message.\n\n"."Details:\n\nName: $name\n\nEmail: $email_address\n\nPhone: $phone\n\nMessage:\n$message";
$headers = "From: noreply@yourwebsite.com\n"; // This is the email address the generated message will be from. We recommend using something like noreply@yourdomain.com.
$headers .= "Reply-To: $email_address";
mail($to,$email_subject,$email_body,$headers);
return true;
?>
您确信您的服务器支持PhP邮件功能,对吗?顺便检查一下您的PhP.ini,请参阅
在这个文件中,您可以看到日志文件的位置,并检查所有配置是否正确。为什么“onsubmit=return false”?您只是假设mail()工作。它在失败时返回布尔值false,您完全忽略了它。检查返回值。如果返回布尔值为true,则这不是php问题,您需要查看邮件服务器日志。@MathewsMathai,因为我不希望它在提交时重新加载页面。您确定
onsubmit=false
按您所说的那样工作吗?它确实会阻止页面重新加载,但也会导致问题。我删除了它,因此我现在将更新问题。我删除了onsubmit=“return false”
并在不久前更新了问题。谢谢你提供的关于ajax的信息,我也这么怀疑,但你已经证实了。邮件功能无法提供已发送邮件或未发送邮件的信息。但是,如果邮件已提交,则返回true;如果未提交,则返回false。
<?php
// Check for empty fields
if(empty($_POST['name']) ||
empty($_POST['email']) ||
empty($_POST['phone']) ||
empty($_POST['message']) ||
!filter_var($_POST['email'],FILTER_VALIDATE_EMAIL))
{
echo "No arguments Provided!";
return false;
}
$name = $_POST['name'];
$email_address = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];
// Create the email and send the message
$to = 'example@example.com'; // Add your email address inbetween the '' replacing yourname@yourdomain.com - This is where the form will send a message to.
$email_subject = "New Email: $name";
$email_body = "You have a new message.\n\n"."Details:\n\nName: $name\n\nEmail: $email_address\n\nPhone: $phone\n\nMessage:\n$message";
$headers = "From: noreply@yourwebsite.com\n"; // This is the email address the generated message will be from. We recommend using something like noreply@yourdomain.com.
$headers .= "Reply-To: $email_address";
mail($to,$email_subject,$email_body,$headers);
return true;
?>