简体   繁体   English

PHP电子邮件表单未从网络电子邮件表单发送电子邮件

[英]Php email form not sending email from web email form

I am trying to troubleshoot this form. 我正在尝试解决此表单。 It is not sending reservation requests from the form on the website. 它不是从网站上的表单发送预订请求。 Despite showing a message that the form was sent. 尽管显示了已发送表格的消息。

I tried editing email and the headers. 我尝试编辑电子邮件和标题。

<?
//print_r($_POST);

$to = “email@emaildomain.com, {$posting['email']}";
function msg($text){
echo "
    <script type='text/javascript'>
    alert('".$text."');
    top.location.href = 'http://www.aribbq.com';
    </script>
";
exit;
}

function error($text){
echo "
    <script type='text/javascript'>
    alert('".$text."');
    history.go(-1);
    </script>
";
exit;
}

if (!$_POST[date]) {error('Please, insert Date.');}
if (!$_POST[time]) {error('Please, insert Time.');}
if (!$_POST[party]) {error('Please, insert Party.');}
if (!$_POST[reservation_name]) {error('Please, insert Name.');}
if (!$_POST[reservation_email]) {error('Please, insert Email.');}
if (!$_POST[reservation_phone]) {error('Please, insert Phone.');}  
if(isset($_POST['submit'])){
 // then send the form to your email

//$from = ('Reservation from AriBBQ.com'); // sender
$mailheaders = "From: contact@aribbq.com" . "\r\n"; // . "CC: 
design@youremail.com"
$mailheaders .= 'Reply-To: ' . $posting['Email'] . "\r\n";
$subject = "AriBBQ.com Online Reservation";
$body = "\n Contact Name: ".$_POST[reservation_name]." \r\n\n"; 
//
$body .= " Email: ".$_POST[reservation_email]." \r\n\n"; //
$body .= " =================================================== \r\n\n"; //
$body .= "  Book a table \r\n\n
            Date: ".$_POST[date]." \r\n\n
            Time: ".$_POST[time]." \r\n\n
            Party: ".$_POST[party]." \r\n\n
            Contact Details \r\n\n
            Name: ".$_POST[reservation_name]." \r\n\n
            Email: ".$_POST[reservation_email]." \r\n\n
            Phone: ".$_POST[reservation_phone]." \r\n\n
            Message: ".$_POST[reservation_message]." \r\n\n"; //
$body .= " =================================================== \r\n\n"; //


$result = mail($to , $from , $subject , $body , $mailheaders); 
if($result) {msg('Thank you, your reservation has been sent. We 
will send you a confirmation text or call in person.');} // 
else{error('Sending mail is failed. Please try again');} //

} else {
error('No submitted. Please try again');
}

?>

You see the form online at http://aribbq.com/ . 您可以在http://aribbq.com/在线看到该表格。 Click on reservations. 点击预订。 Once the email is received, we want to be able to reply to the sender's email address. 收到电子邮件后,我们希望能够回复发件人的电子邮件地址。

Alright, essentially, you need to turn on error reporting because your script threw about 20 errors at me which you would see with error reporting on. 好的,从本质上讲,您需要打开错误报告,因为您的脚本向我抛出了大约20个错误,打开错误报告会看到这些错误。 As my comment above said, add error_reporting(E_ALL); 如我上面的评论所述,添加error_reporting(E_ALL); to the top of your script while you debug. 调试时将其移到脚本顶部。

The issues I came across are as follows: 我遇到的问题如下:

Parse error: syntax error, unexpected '@' in /mail.php on line 4 caused by an incorrect double quote character, not " but . Subtle, but problematic. Parse error: syntax error, unexpected '@' in /mail.php on line 4是由不正确的双引号引起的,不是"而是

Next up, Multiple or malformed newlines found in additional_header in /mail.php because as of PHP 5.5.2, a bug was fixed to prevent mail header injection, so all of your \\n\\n within the $mailheaders should be removed, I recommend appending PHP_EOL to the end of each line instead. 接下来, Multiple or malformed newlines found in additional_header in /mail.php因为自PHP 5.5.2起, 已修复了一个错误 ,阻止了邮件头的注入,因此应删除$mailheaders所有\\n\\n ,我建议改为在每行末尾附加PHP_EOL

You have your $from variable included in the mail() call, this presents 2 issues. 您将$from变量包含在mail()调用中,这将带来2个问题。 One, the mail() function does not have a from parameter, you include it within the headers. 第一, mail()函数没有from参数,您将其包含在标题中。 Two - your variable is actually commented out. 二-您的变量实际上已被注释掉。

As I mentioned in the comment above, again, your email address variable to send to is typed as $posting['email']' , and $posting['Email'] within $mailheaders . 就像我在上面的评论中提到的,同样,要发送到的电子邮件地址变量在$mailheaders键入为$posting['email']'$posting['Email'] The problem here is $posting doesn't exist. 这里的问题是$posting不存在。 Secondly, your form, which you should include the HTML for in future questions for self-contained examples for people to more easily help you (see https://stackoverflow.com/help/how-to-ask ) , doesn't post email at all, it posts reservation_email . 其次,您的表单( 不会在以后的问题中包括HTML)作为独立示例供人们更轻松地帮助您(请参阅https://stackoverflow.com/help/how-to-ask该表单不会发布email ,它发布了reservation_email

Finally, the majority of your $_POST references do not include quotes so PHP doesn't know what to do with the words in between the square brackets. 最后,大多数$_POST引用都不包含引号,因此PHP不知道如何处理方括号之间的单词。 $_POST[date] should be $_POST['date'] , for example. $_POST[date]应该是$_POST['date']

I've made all the above changes and managed to successfully email myself with the script and email form provided, the only thing that I didn't look at was your msg() which didn't show me a success message. 我进行了上述所有更改,并设法通过提供的脚本和电子邮件表单成功地通过电子邮件发送给自己,我唯一没有看到的是您的msg() ,它没有向我显示成功消息。 I did, however, put an echo statement before this function call which printed out fine. 但是,我确实在此函数调用之前放置了echo语句,该语句打印得很好。

I hope this helps you get your script up and running, good luck and remember, error_reporting(); 我希望这可以帮助您启动并运行脚本,祝您好运,并记住error_reporting(); is your friend! 是你的朋友!

mail()函数在大多数服务器上不可用,尝试从smtp发送邮件PhpMailer是一个很好的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM