PHP邮件程序/ Hubspot脚本的标头错误

时间:2017-12-17 05:40:22

标签: php

已经提出了类似的问题,但据我所知,它们与这种情况无关。我有一个与PHP脚本链接的表单(如下所示),它发送电子邮件并将提交的信息写入Hubspot。

当我通过表格提交时。它给了我无法修改标题信息 - 标题已经发送在粗体标记的底部(扫描错误发生在下面)。当我删除标题('位置:index.php?vm =您的消息已提交。'。$ hubspot_message); 行时,错误消失,表单功能正确无需重定向,这显然不太理想。

有谁知道这是什么问题?我被困了。

<table>
<?php


    $email_to = "email@email.com";

    function died($error) {



        echo "We are very sorry, but there were error(s) found with the form you submitted. ";

        echo "These errors appear below.<br /><br />";

        echo $error."<br /><br />";

        echo "Please go back and fix these errors.<br /><br />";

        die();

    }


            function clean_string($string) {

                $bad = array("content-type","bcc:","to:","cc:","href");

                return str_replace($bad,"",$string);

            }

            $first_name = clean_string($_POST['q1']); // required

            $last_name = clean_string($_POST['q2']); // required

            $email_from = clean_string($_POST['q3']); // required

            $telephone = clean_string($_POST['q4']); // required


            $error_message = "";

            $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/'; 


            if(!preg_match($email_exp,$email_from))
            {

                $error_message .= 'The Email Address you entered does not appear to be valid.<br>';

            }

            //}
            $string_exp = "/^[A-Za-z .'-]+$/";
            if(!preg_match($string_exp,$first_name)) {

                $error_message .= 'The First Name you entered does not appear to be valid.<br>';

            }

            if(!preg_match($string_exp,$last_name)) {

                $error_message .= 'The Last Name you entered does not appear to be valid.<br>';

            }


           $string_exp = "/^[0-9-+()\s]+$/";

            if(!preg_match($string_exp,$telephone)){

                $error_message .= 'The phone number ' . $telephone . ' you entered does not appear to be valid.<br>';

            }


            if(strlen($error_message) > 0) {
                 header('Location: index.php?vm='.$error_message.'Please correct and resubmit' . '&first_name='.$first_name .'&last_name='. $last_name . '&email_from=' . $email_from . '&telephone='. $telephone . '&comments=' . $comments . '&subject=' . $subject);
                died($error_message);

            }

$hubspot_message = "";        
//Translate to Hubspot field names
$arr = array(
            'properties' => array(
                array(
                    'property' => 'email',
                    'value' => $email_from
                ),
                array(
                    'property' => 'firstname',
                    'value' => $first_name
                ),
                array(
                    'property' => 'lastname',
                    'value' => $last_name
                ),
                array(
                    'property' => 'phone',
                    'value' => $telephone
                )
            )
        );
        $post_json = json_encode($arr);
        //Hubspot API Key
        $hapikey = "126wf72-c560-4e11-a2dc-5s88s8wwfd8";   //<-- Your Hubspot API key must be here...       

        $endpoint = 'https://api.hubapi.com/contacts/v1/contact?hapikey=' . $hapikey;
        $ch = @curl_init();
        @curl_setopt($ch, CURLOPT_POST, true);
        @curl_setopt($ch, CURLOPT_POSTFIELDS, $post_json);
        @curl_setopt($ch, CURLOPT_URL, $endpoint);
        @curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
        @curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $response = @curl_exec($ch);
        $status_code = @curl_getinfo($ch, CURLINFO_HTTP_CODE);
        $curl_errors = curl_error($ch);
        @curl_close($ch);
        if(strlen($curl_errors)> 0)
        {            
            $hubspot_message .= "</br>curl Errors: " . $curl_errors . "</br>";
        }


        if($status_code == '409')
        {
            $hubspot_message .= " Contact with this email address already exists.";
        }

        if($status_code == '200')//New Contact was accepted by Hubspot
        {            
            $email_message = "Form details below.\n\n";

            // email content
            $email_message .= "First Name: ".$first_name."\n";

            $email_message .= "Last Name: ".$last_name."\n";

            $email_message .= "Email: ".$email_from."\n";

            $email_message .= "Telephone: ".$telephone."\n";

             $email_message .= "Subject: ". "Contact Request Form Submission" ."\n";//<--- Change email subject here

            // create email headers
            $headers = 'From: '.$email_from."\r\n".

                    'Reply-To: '.$email_from."\r\n" .

                    'X-Mailer: PHP/' . phpversion();

            @mail($email_to, $email_subject, $email_message, $headers);

            //ERROR HAPPENS BELOW - It's supposed to redirect back to contact page and display status
            header('Location: index.php?vm=Your Message has been submitted. ' . $hubspot_message);
            //echo $hubspot_message . " succeeded";
        }
        else
        {
            //echo $hubspot_message . " failed";

                if (isset($_SERVER["HTTP_REFERER"])) {
                    header("Location: index.php?vm=" . $hubspot_message . '&first_name='.$first_name.'&last_name=' .$last_name. '&email_from=' .$email_from. '&telephone='.$telephone);
                }
            //Redirect back to contact page and display status
            //header('Location: example.html');
        }

?>
</table>

1 个答案:

答案 0 :(得分:0)

You can turn on output buffering before write or send text to the browser, see Output Control Functions, you need execute ob_start before send any text!

<?php
ob_start(); ////////////////Turn on output before (Capture output)

echo '<table>';

$email_to = "email@email.com";

function died($error)
{
    echo "We are very sorry, but there were error(s) found with the form you submitted. ";
    echo "These errors appear below.<br /><br />";
    echo $error . "<br /><br />";
    echo "Please go back and fix these errors.<br /><br />";
    die();
}


function clean_string($string)
{
    $bad = [
        "content-type",
        "bcc:",
        "to:",
        "cc:",
        "href",
    ];
    return str_replace($bad, "", $string);
}

$first_name = clean_string($_POST['q1']); // required
$last_name = clean_string($_POST['q2']); // required
$email_from = clean_string($_POST['q3']); // required
$telephone = clean_string($_POST['q4']); // required

$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';

if (!preg_match($email_exp, $email_from))
{
    $error_message .= 'The Email Address you entered does not appear to be valid.<br>';
}

$string_exp = "/^[A-Za-z .'-]+$/";
if (!preg_match($string_exp, $first_name))
{
    $error_message .= 'The First Name you entered does not appear to be valid.<br>';
}

if (!preg_match($string_exp, $last_name))
{
    $error_message .= 'The Last Name you entered does not appear to be valid.<br>';
}

$string_exp = "/^[0-9-+()\s]+$/";
if (!preg_match($string_exp, $telephone))
{
    $error_message .= 'The phone number ' . $telephone . ' you entered does not appear to be valid.<br>';
}

if (strlen($error_message) > 0)
{
    header(
        'Location: index.php?vm=' . $error_message . 'Please correct and resubmit' . '&first_name=' . $first_name
        . '&last_name=' . $last_name . '&email_from=' . $email_from . '&telephone=' . $telephone . '&comments='
        . $comments . '&subject=' . $subject
    );
    died($error_message);
}

$hubspot_message = "";
//Translate to Hubspot field names
$arr       = [
    'properties' => [
        [
            'property' => 'email',
            'value'    => $email_from,
        ],
        [
            'property' => 'firstname',
            'value'    => $first_name,
        ],
        [
            'property' => 'lastname',
            'value'    => $last_name,
        ],
        [
            'property' => 'phone',
            'value'    => $telephone,
        ],
    ],
];
$post_json = json_encode($arr);
//Hubspot API Key
$hapikey = "126wf72-c560-4e11-a2dc-5s88s8wwfd8";   //<-- Your Hubspot API key must be here...

$endpoint = 'https://api.hubapi.com/contacts/v1/contact?hapikey=' . $hapikey;
$ch       = @curl_init();
@curl_setopt($ch, CURLOPT_POST, true);
@curl_setopt($ch, CURLOPT_POSTFIELDS, $post_json);
@curl_setopt($ch, CURLOPT_URL, $endpoint);
@curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
@curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response    = @curl_exec($ch);
$status_code = @curl_getinfo($ch, CURLINFO_HTTP_CODE);
$curl_errors = curl_error($ch);
@curl_close($ch);
if (strlen($curl_errors) > 0)
{
    $hubspot_message .= "</br>curl Errors: " . $curl_errors . "</br>";
}


if ($status_code == '409')
{
    $hubspot_message .= " Contact with this email address already exists.";
}

if ($status_code == '200')//New Contact was accepted by Hubspot
{
    $email_message = "Form details below.\n\n";
    // email content
    $email_message .= "First Name: " . $first_name . "\n";
    $email_message .= "Last Name: " . $last_name . "\n";
    $email_message .= "Email: " . $email_from . "\n";
    $email_message .= "Telephone: " . $telephone . "\n";
    $email_message .= "Subject: " . "Contact Request Form Submission" . "\n";//<--- Change email subject here

    // create email headers
    $headers = 'From: ' . $email_from . "\r\n" .
               'Reply-To: ' . $email_from . "\r\n" .
               'X-Mailer: PHP/' . phpversion();
    @mail($email_to, $email_subject, $email_message, $headers);
    //ERROR HAPPENS BELOW - It's supposed to redirect back to contact page and display status
    header('Location: index.php?vm=Your Message has been submitted. ' . $hubspot_message);
    //echo $hubspot_message . " succeeded";
}
else
{
    //echo $hubspot_message . " failed";
    if (isset($_SERVER["HTTP_REFERER"]))
    {
        header(
            "Location: index.php?vm=" . $hubspot_message . '&first_name=' . $first_name . '&last_name=' . $last_name
            . '&email_from=' . $email_from . '&telephone=' . $telephone
        );
    }
    //Redirect back to contact page and display status
    //header('Location: example.html');
}
ob_end_flush(); ////////////////Write buffer (Send output to browser)
?>
</table>