最近,我使用 php 脚本从在线联系表单发送电子邮件时发生了一些奇怪的事情,我只是想知道是否有人可以对这个问题有所了解。
我有一个在多个网站上使用的 php 脚本,它一直运行良好,但出于某种奇怪的原因,我尝试在一个网站上使用它,但它无法正常工作。
我试着摆弄它,最终意识到它与以下代码部分有关:
这是通常可以正常工作的原始代码部分,但由于某种原因无法正常工作:
$to = 'My Name <info@mydomain.com>';
然后我删除了名称位,因此代码如下所示:
$to = 'info@mydomain.com';
现在它通过确定发送电子邮件。
正如我所说,顶级代码通常可以正常工作,所以有什么想法为什么这次我必须更改代码才能使其正常工作?
任何可能的解释都会很棒: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;
}
?>