使用带有smtp的phpmailer重定向到感谢页面时出现服务器错误



在 Canvas HTML5 模板中工作。 该特定模板在通过页面上的表单发送电子邮件后使用内联成功消息。 我不得不切换设置以使用SMTP并重定向到感谢页面。 电子邮件正在通过,但一旦发送邮件,它就不会重定向我。 相反,我收到内部服务器错误。 我的语法刚刚关闭吗? 除了FTP之外,我无法访问后端。

电子邮件正在通过。 只是重定向似乎已关闭。

<?php
require_once('phpmailer/PHPMailerAutoload.php');
$toemails = array();
$toemails[] = array(
'email' => 'recipient@domain.com', // Your Email Address
'name' => 'Recipient Name' // Your Name
);

$mail = new PHPMailer();
// If you intend you use SMTP, add your SMTP Code after this Line
$mail->IsSMTP();
$mail->Host = "mail.domain.com";
$mail->SMTPDebug = 2;
$mail->SMTPAuth = true;
$mail->Port = 25;
$mail->Username = "forms@domain.com";
$mail->Password = "PaSsWoRd";

if( $_SERVER['REQUEST_METHOD'] == 'POST' ) {
if( $_POST['template-contactform-email'] != '' ) {
$name = isset( $_POST['template-contactform-name'] ) ? $_POST['template-contactform-name'] : '';
$email = isset( $_POST['template-contactform-email'] ) ? $_POST['template-contactform-email'] : '';
$phone = isset( $_POST['template-contactform-phone'] ) ? $_POST['template-contactform-phone'] : '';
$company = isset( $_POST['template-contactform-company'] ) ? $_POST['template-contactform-company'] : '';
$subject = isset( $_POST['template-contactform-subject'] ) ? $_POST['template-contactform-subject'] : '';
$message = isset( $_POST['template-contactform-message'] ) ? $_POST['template-contactform-message'] : '';
$subject = isset($subject) ? $subject : 'New Message From Contact Form';
$botcheck = $_POST['template-contactform-botcheck'];
if( $botcheck == '' ) {
$mail->SetFrom( $email , $name );
$mail->AddReplyTo( $email , $name );
foreach( $toemails as $toemail ) {
$mail->AddAddress( $toemail['email'] , $toemail['name'] );
}
$mail->Subject = $subject;
$name = isset($name) ? "Name: $name<br><br>" : '';
$email = isset($email) ? "Email: $email<br><br>" : '';
$phone = isset($phone) ? "Phone: $phone<br><br>" : '';
$company = isset($company) ? "Firm or Company Name: $company<br><br>" : '';
$message = isset($message) ? "Message: n$message<br><br>" : '';
$referrer = $_SERVER['HTTP_REFERER'] ? '<br><br><br>This Form was submitted from: ' . $_SERVER['HTTP_REFERER'] : '';
$body = "$name $email $phone $company $message $referrer";

$mail->MsgHTML( $body );
$sendEmail = $mail->Send();
if( $sendEmail == true ):
//ORIGINAL ECHO OF SUCCESS MESSAGE              
//echo '{ "alert": "success", "message": "' . $message_success . '" }';
//SWITCH TO REDIRECT
header("Location: ../thank-you.php");
else:
echo '{ "alert": "error", "message": "Email <strong>could not</strong> be sent due to some Unexpected Error. Please Try Again later.<br /><br /><strong>Reason:</strong><br />' . $mail->ErrorInfo . '" }';
endif;
} else {
echo '{ "alert": "error", "message": "Bot <strong>Detected</strong>.! Clean yourself Botster.!" }';
}
} else {
echo '{ "alert": "error", "message": "Please <strong>Fill up</strong> all the Fields and Try Again." }';
}
} else {
echo '{ "alert": "error", "message": "An <strong>unexpected error</strong> occured. Please Try Again later." }';
}
?>

形式:

<form class="nobottommargin" id="template-contactform" name="template-contactform" action="php/sendemail.php" method="post">
<div class="form-process"></div>
<div class="col_half">
<input type="text" id="template-contactform-name" name="template-contactform-name" value="" class="sm-form-control border-form-control required" placeholder="Name" />
</div>
<div class="col_half col_last">
<input type="email" id="template-contactform-email" name="template-contactform-email" value="" class="required email sm-form-control border-form-control" placeholder="Email Address" />
</div>
<div class="col_half">
<input type="text" id="template-contactform-phone" name="template-contactform-phone" value="" class="sm-form-control border-form-control" placeholder="Phone" />
</div>
<div class="col_half col_last">
<input type="text" id="template-contactform-company" name="template-contactform-company" value="" class="sm-form-control border-form-control" placeholder="Company Name" />
</div>
<div class="clear"></div>
<div class="col_full">
<input type="text" id="template-contactform-subject" name="template-contactform-subject" value="" class="sm-form-control border-form-control" placeholder="Subject" />
</div>
<div class="col_full">
<textarea class="sm-form-control border-form-control" id="template-contactform-message" name="template-contactform-message" rows="7" cols="30" placeholder="What do you think we should know?"></textarea>
</div>
<div class="col_full">
<button class="button button-black noleftmargin topmargin-sm" type="submit" id="template-contactform-submit" name="template-contactform-submit" value="submit">Send Message</button>
</div>
<div class="clear"></div>
<div class="col_full hidden">
<input type="text" id="template-contactform-botcheck" name="template-contactform-botcheck" value="" class="sm-form-control" />
</div>
</form>

您的 if 语句语法已关闭。 要么使用大括号,要么使用这种形式 $var==TRUE ?"真" : "假";得到真实

此处的其他帮助:http://php.net/manual/en/control-structures.alternative-syntax.php

在这里: https://www.w3schools.com/php/php_if_else.asp

根据您的请求,首先运行以下命令以确保它是 if 语法。

if( $sendEmail == true )
{
header("Location: ../thank-you.php");
}
else { }

也可能$sendEmail不是真的。 邮件可能会返回错误 https://pear.php.net/manual/en/package.mail.mail.send.php

以防万一它将来对某人有所帮助,在这种特定情况下,我相信 phpMailer 不止一次发送标头,这会导致服务器上发生冲突。

解决方法是缓冲输出,直到整个过程完成,然后一次发送所有内容。 这是在html表单中引用的"action"php文件的开头(紧跟在"<?"之后),然后在该文件的末尾(就在"?>"之前)使用ob_end_flush();ob_start();

来完成的。有人可能会纠正我(我显然不是 PHP 专家),但据我了解,这会将所有标头信息保存在缓冲区中,然后一次发送,而不是尝试多次发送标头信息。

无论这是否是正确的解释方式,解决方案都有效,页面按预期重定向到感谢页面。 如果有人有更好的解释,请随时写出来,我很乐意接受别人的回答。 感谢您的帮助!

相关内容

最新更新