我最近发生了一些奇怪的事情,我使用PHP脚本从在线联系表单发送电子邮件,只是想知道是否有人可以对这个问题有所了解。
我有一个PHP脚本,我在多个网站上使用它一直工作正常,但由于一些奇怪的原因,我尝试在一个网站上使用它,它只是没有工作。
我尝试过它并最终意识到这与下面的代码部分有关:
这是代码的原始部分,通常可以正常工作,但由于某种原因无效:
$to = 'My Name <info@mydomain.com>';
然后我删除了名称位,以便代码看起来像这样:
$to = 'info@mydomain.com';
现在它通过ok发送电子邮件。
正如我所说,顶级代码通常工作正常,所以任何想法为什么这次我必须改变代码才能使它工作?
任何可能的解释都很棒:o)
这是完整的代码:
<?php
require("is_email.php"); // email validation function
//Retrieve form data.
//GET - user submitted data using AJAX
//POST - in case user does not support javascript, we'll use POST instead
$name = ($_GET['name']) ?$_GET['name'] : $_POST['name'];
$email = ($_GET['email']) ?$_GET['email'] : $_POST['email'];
$telephone = ($_GET['telephone']) ?$_GET['telephone'] : $_POST['telephone'];
$address = ($_GET['address']) ?$_GET['address'] : $_POST['address'];
$enquiry = ($_GET['enquiry']) ?$_GET['enquiry'] : $_POST['enquiry'];
$calculation = ($_GET['calculation']) ?$_GET['calculation'] : $_POST['calculation'];
//flag to indicate which method it uses. If POST set it to 1
if ($_POST) $post=1;
//Server side validation for POST data
if (!$name) $errors[count($errors)] = 'Please click back and enter your name.';
if (!$email) $errors[count($errors)] = 'Please click back and enter your email.';
else if (!is_email($email)) $errors[count($errors)] = 'Please click back as you may have entered an invalid email address.';
if (!$telephone) $errors[count($errors)] = 'Please click back and enter your telephone number.';
if (!$address) $errors[count($errors)] = 'Please click back and enter your address.';
if (!$enquiry) $errors[count($errors)] = 'Please click back and enter your enquiry.';
if ($calculation != '14') $errors[count($errors)] = 'Please click back and check you have correctly answered the simple calculation (in number format).';
//if the errors array is empty, send the mail
if (!$errors) {
//recipient - change this to your name and email
$to = 'info@mydomain.com';
//sender
$from = $name . ' <' . $email . '>';
//subject and the html message
$subject = 'Website Enquiry: ' . $name;
$email_body = '
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head></head>
<body>
<table cellpadding="5" style="color:#757575;">
<tr><td style="color:#3b5998;">Name: </td><td>' . $name . '</td></tr>
<tr><td style="color:#3b5998;">Email: </td><td>' . $email . '</td></tr>
<tr><td style="color:#3b5998;">Telephone: </td><td>' . $telephone . '</td></tr>
<tr valign="top"><td style="color:#3b5998;">Address: </td><td>' . nl2br($address) . '</td></tr>
<tr valign="top"><td style="color:#3b5998;">Enquiry: </td><td>' . nl2br($enquiry) . '</td></tr>
</table>
</body>
</html>';
//send the mail
$result = sendmail($to, $subject, $email_body, $from);
//if POST was used, display the message straight away
if ($_POST) {
if ($result) echo 'Thank you! We have received your message.<br /><br /><a href="../enquiry_form.html">OK</a>';
else echo 'Sorry, unexpected error. Please try again later';
//else if GET was used, return the boolean value so that
//ajax script can react accordingly
//1 means success, 0 means failed
} else {
echo $result;
}
//if the errors array has values
} else {
//display the errors message
for ($i=0; $i<count($errors); $i++) echo $errors[$i] . '<br />';
exit;
}
//Simple mail function with HTML header
function sendmail($to, $subject, $email_body, $from) {
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
$headers .= 'From: ' . $from . "\r\n";
$result = mail($to,$subject,$email_body,$headers);
if ($result) return 1;
else return 0;
}
?>
答案 0 :(得分:0)
主机可能会因sendmail
的要求而异,具体取决于他们使用的sendmail
软件。您可以与您的托管公司联系,看看是否有任何警告使其工作或他们知道更好的格式。
答案 1 :(得分:0)
使用此:
$mailFrom = '"My Name" <info@mydomain.com>';
FROM标签是双引号,空格是<email>
答案 2 :(得分:0)
我编辑了我最初的问题来解释我是如何克服这个问题的。