2017-12-17 1 views
0

Ähnliche Fragen wurden gestellt, aber sie beziehen sich nicht auf dieses Szenario, soweit ich sehen kann. Ich habe ein Formular, das mit einem PHP-Skript (unten) verknüpft ist, das eine E-Mail sendet und die übermittelten Informationen an Hubspot schreibt.Header Fehler mit PHP Mailer/Hubspot Skript

Wenn ich über das Formular absende. es gibt mir, Kann Header-Informationen nicht ändern - Header bereits gesendet auf der fett markierten Zeile nach unten (Suche nach FEHLER PASST UNTER). Wenn ich das entferne header ('Location: index.php? Vm = Ihre Nachricht wurde übermittelt.'. $ Hubspot_message); Zeile, der Fehler verschwindet und das Formular funktioniert ohne Umleitung richtig, die offensichtlich weniger als ideal ist.

Weiß jemand, was das Problem ist? Ich bin so festgefahren.

<table> 
<?php 


    $email_to = "[email protected]"; 

    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._%-][email protected][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

Grundsätzlich können Sie vor einer Weiterleitung kein HTML "rendern" oder ausgeben. Das '

' vor dem PHP-Code-Chunk ist höchstwahrscheinlich das Problem. Sie sollten nach dem 'header()' auch einen 'exit;' haben. Dies verhindert, dass mehr PHP ausgeführt wird (kann zu anderen Problemen führen). – Tigger

Antwort

0

Sie auf Ausgabepufferung vor Schreib drehen können oder Text an den Browser senden, finden Sie Output Control Functions, müssen Sie ausführen ob_start vor einem beliebigen Text senden!

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

echo '<table>'; 

$email_to = "[email protected]"; 

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._%-][email protected][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> 
+0

Das hat leider nicht funktioniert. – cedexious