2016-10-28 3 views
0

Ich habe etwas Code geschrieben, um E-Mails von einer Website zu senden. Der Basiscode funktionierte perfekt, ich fügte dann ein paar kleine Bits hinzu, um zu überprüfen, ob die eingegebenen Daten richtig waren, und jetzt wird nur angezeigt, dass die Seite nicht funktioniert. Dies ist das erste Mal, dass ich PHP benutze, also bin ich ziemlich neu, obwohl ich keine Fehler mit dem Code sehen kann?Debug: Senden von E-Mails mit PHP

enter image description here

<?php 
if(isset($_POST['submitButton'])){ 

    function errorMessage($error) { 
     echo 'Apologies but the request has not been successful<br/>'; 
     echo 'Please see below, amend and then resubmit<br/><ul>'; 
     echo $error . '</ul>'; 
     die(); 
    } 
    $error_message = ""; // set the error message as empty 

    $email_exp = '/^[A-Za-z0-9._%-][email protected][A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/'; // string to look for 
    $string_exp = "/^[A-Za-z .'-]+$/"; // string to look for 
    $contact_exp = '/^[0-9]/'; 


    $subject = " Request"; // subject of their email to me 
    $subject2 = "Copy of your Request"; // subject of the email back to them 
    $to = "myemail"; // this is my Email address 
    $from = $_POST['emailAddressField']; // this is the requesters Email address 

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

    $first_name = $_POST['firstNameField']; // this is their first name 

    if (!preg_match($string_exp,$first_name)) { 
     $error_message . = '<li>The first name you entered does not appear to be valid.</li>'; 
    } 

    $last_name = $_POST['surnameField']; // this is their surname 

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

    $contact_number = $_POST['contactNumberField']; // this is their contact number 

    if (!preg_match($contact_exp,$contact_number)){ 
     $error_message . = '<li>The contact number you entered does not appear to be valid.</li>'; 
    } 

    $details_field = $_POST['detailsField']; // this is the details 

    if (strlen($details_field) < 10) { 
     $error_message . = '<li>Please give more details, at least 10 characters.</li>'; 
    } 

    $message = $first_name . " " . $last_name . " (" . $contact_number . ") wrote the following:" . "\n\n" . $details_field; // message of email to me 
    $message2 = "Here is a copy of your request " . $first_name . " (" . $contact_number . ").\n\n" . $details_field . "\n\nMany thanks"; // message of email to requester 

    $headers = "From:" . $from; // message of email header to me 
    $headers2 = "From:" . $to; // message of email header to requester 

    // if there have been errors, then display error message and end 
    if(strlen($error_message) > 0) { 
     errorMessage($error_message); 
    } 

    // send the emails 
    mail($to,$subject,$message,$headers); 
    mail($from,$subject2,$message2,$headers2); // sends a copy of the message to the sender 
    echo "Request sent. Thank you " . $first_name . ", I will contact you shortly."; 

} 
?> 

funktioniert perfekt:

<?php 
if(isset($_POST['submitButton'])){ 

    $subject = "Request"; // subject of their email to me 
    $subject2 = "Copy of your Request"; // subject of the email back to them 
    $to = "myemail"; // this is my Email address 
    $from = $_POST['emailAddressField']; // this is the requesters Email address 
    $first_name = $_POST['firstNameField']; // this is their first name 
    $last_name = $_POST['surnameField']; // this is their surname 
    $contact_number = $_POST['contactNumberField']; // this is their contact number 
    $details_field = $_POST['detailsField']; // this is the details 
    $message = $first_name . " " . $last_name . " (" . $contact_number . ") wrote the following:" . "\n\n" . $details_field; // message of email to me 
    $message2 = "Here is a copy of your request " . $first_name . " (" . $contact_number . ").\n\n" . $details_field . "\n\nMany thanks"; // message of email to requester 
    $headers = "From:" . $from; // message of email header to me 
    $headers2 = "From:" . $to; // message of email header to requester 

    // send the emails 
    mail($to,$subject,$message,$headers); 
    mail($from,$subject2,$message2,$headers2); // sends a copy of the message to the sender 
    echo "Request sent. Thank you " . $first_name . ", I will contact you shortly."; 

} 
?> 

mir jemand bei der Suche nach diesen ärgerlichen Fehler helfen?

+1

Funktioniert nicht ** WIE **? –

+0

@MarcB aktualisiert mit Fehler, den ich bekomme. Ich bin zu erfahren mit PHP, um das richtig zu debuggen! – Geoff

+0

@ Fred-ii-das Beispiel am Ende der Frage funktioniert perfekt, und sendet die E-Mails wie erwartet, aufgrund dessen, ist das Formular nicht, was ist falsch? – Geoff

Antwort

3

Überall . = haben, muss .= geändert werden

dh

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

sollte

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

Um sein zu debuggen PHP zu können, müssen Sie irgendwo Sie es lokal und zeigen Sie die Fehlermeldungen ausgeführt werden können. Wenn ich die Datei ausführe, bekomme ich: Parse error: syntax error, unexpected '=' in temp.php on line 23, die Ihnen genau sagt, wo das Syntaxproblem zu finden ist.

0

Ihr Problem ist, mit, wie Sie Ihre $ error_message Variable gesetzt, Ihr Code:

$error_message . = '<your error message here>'; 

aber man kann nicht einen Raum zwischen dem haben "" und die "="

ändern diese Zeilen an:

$error_message .= '<your error message here>'; 

zu diesem Link finden Sie ermöglichen in PHP-Skript error_reporting: PHP Error Reporting