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

时间:2017-04-20 17:51:04

标签: php forms 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>

2 个答案:

答案 0 :(得分:2)

您的if语句语法已关闭。要么使用大括号,要么使用此形式$ var == TRUE? '真假'; //得到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

答案 1 :(得分:0)

以防万一将来帮助某人,在这个特定的例子中,我相信phpMailer不止一次发送标题,这导致了服务器上的冲突。

修复是缓冲输出,直到整个过程完成,然后立即发送所有内容。这是在html表单中引用的“action”php文件的开头使用ob_start();完成的(在“<?”之后),然后是ob_end_flush();在该文件的末尾(只是在“?>”之前。

有人可能会纠正我(我显然不是PHP专家),但据我所知,这会将所有标题信息保存在缓冲区中,然后立即发送,而不是尝试多次发送标题信息。

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