PHP形成没有错误,但没有消息到达

时间:2015-07-22 08:13:12

标签: javascript php forms

我有以下问题,我有一个PHP脚本发送表单但不幸的是它说邮件已发送但没有任何东西到达。任何想法可能是什么问题?垃圾邮件文件夹中也没有任何内容。

这是表格:

<?php

$to = "name@domain.com"; //This is the email address you want to send the email to
//$subject_prefix = ""; //Use this if you want to have a prefix before the subject

if(!isset($_GET['action'])) {
  die("You must not access this page directly!"); //Just to stop people from visiting contact.php normally
}

$subject = 'Contactform'; //The subject

/* Now lets trim up the input before sending it */
$name = trim($_GET['name']); //The senders name
$email1 = trim($_GET['email1']); //The senders email address
//$subject = trim($_GET['subject']); //The senders subject
$message .= "===================================="."\n";
$message .= "Kontaktanfrage"."\n";
$message .= "===================================="."\n\n";
$message .= "Name: ".$name."\n";
$message .= "email: ".trim($_GET['email1'])."\n";
$message .= "Telefon: ".trim($_GET['phone1'])."\n";
$message .= "Strasse: ".trim($_GET['street'])."\n";
$message .= "Stadt: ".trim($_GET['city'])."\n";
$message .= "------------------------------------"."\n\n";
$message .= "Nachricht: "."\n".trim($_GET['msg'])."\n\n"; //The senders message
$message .= "===================================="."\n";

if ( empty($name) or empty($_GET['phone1']) or empty($_GET['msg']) ) {
  echo 'errorarea|Bitte f&uuml;llen Sie folgende Felder aus: Name, Telefon und Nachricht!';

} else {
mail($to,$subject,$message,"From: ".$email1.""); //a very simple send
  echo 'contactarea|Vielen Dank '.$name.', Ihre eMail wurde verschickt.'; //now lets update the "contactarea" div on the contact.html page. The contactarea| tell's the javascript which div to update.
}
?>

这是脚本:

<script language="javascript" type="text/javascript">

        function createRequestObject() {
            var ro;
            var browser = navigator.appName;
            if(browser == "Microsoft Internet Explorer"){
                ro = new ActiveXObject("Microsoft.XMLHTTP");
            }else{
                ro = new XMLHttpRequest();
            }
            return ro;
        }

        var http = createRequestObject();

        function sendemail() {
            var msg = document.contactform.msg.value;
            var name = document.contactform.name.value;
            var email1 = document.contactform.email1.value;
            var phone1 = document.contactform.phone1.value;
            var street = document.contactform.street.value;
            var city = document.contactform.city.value;
            //var subject = document.contactform.subject.value;
            document.contactform.send.disabled=true; 
            document.contactform.send.value='Absenden';

            http.open('get', '/js/message.php?msg='+msg+'&name='+name+'&email1='+email1+'&phone1='+phone1+'&street='+street+'&city='+city+'&action=send'); //'&subject='+subject+
            http.onreadystatechange = handleResponse;
            http.send(null);
        }

        function handleResponse() {
            if(http.readyState == 4){
                var response = http.responseText;
                var update = new Array();

                if(response.indexOf('|' != -1)) {
                    update = response.split('|');
                    document.getElementById(update[0]).innerHTML = update[1];
            document.contactform.send.disabled=false; 
            document.contactform.send.value='Absenden';
                }
            }
        }
        </script>
<div id="contact">
    <h3>Kontaktformular</h3>
    Bitte f&uuml;llen Sie alle unten aufgef&uuml;hrten Felder aus damit wir Ihre Kontaktanfrage bestm&ouml;glich bearbeiten k&ouml;nnen<br/>
    <br/>
    Vielen Dank.<br/>
    <br/>
    Um Ihre Anfrage richtig bearbeiten zu k&ouml;nnen bitten wir Sie alle Felder richtig auszuf&uuml;llen.<br/>
    <br/>
    <form name="contactform" id="contactform">
        <label for="name">Name *</label>
        <input type="text" name="name" id="name" size="24"><br/>

        <label for="street">Strasse</label>
        <input type="text" name="street" id="street" size="24"><br/>

        <label for="zip">PLZ/Stadt</label>
        <input type="text" name="city" id="city" size="24"><br/>

        <label for="phone1">Telefon *</label>
        <input type="text" name="phone1" id="phone1" size="24"><br/>

        <label for="email1">eMail</label>
        <input type="text" name="email1" id="email1" size="24"><br/>

        <div style="height: 10px;"></div>

        <label for="message">Nachricht *</label>
        <textarea name="msg" rows="5" id="msg" cols="25"></textarea>

        <hr noshade size="1px" color="#E0DFA5">

        <div style="color: #CFCDC3; font-size: 9px; margin-top: 10px; margin-bottom: 5px;">* Diese Felder werden ben&ouml;tigt.</div>
        <div id="contactarea" style="text-align: center;"><div id="errorarea"></div></div>
        <input type="button" class="submit" value="Absenden" name="send" onclick="sendemail();" id="submitbutton">
    </form>
</div>

非常感谢任何帮助。非常感谢。

1 个答案:

答案 0 :(得分:0)

假设现场网站允许发送电子邮件

<?php
    /*
        Why not use POST to send the data via ajax?
        Then you could use 
        if( $_SERVER['REQUEST_METHOD']=='POST' ){

        }
    */
    /* Comment out the lines below if you get a response in the console and receive an email */
    $res=mail('me@example.com','test','this is a test');
    echo $res;
    print_r( $_GET );
    exit();
    /* comment out above lines */

    if( !isset($_GET['action']) ) {
        die("You must not access this page directly!");
    }

    $to = "name@domain.com";
    $subject = 'Contactform';
    /*
        There is no filtering of the request - you could use
        filter_input( INPUT_GET, 'email1', FILTER_SANITIZE_ENCODED, array( 'flags' => FILTER_FLAG_STRIP_HIGH | FILTER_FLAG_ENCODE_HIGH ) );
        to provide some basic filtering of supplied data
    */
    $name = trim($_GET['name']);
    $email1 = trim($_GET['email1']);

    /*
        Initially you had $message.= but there was no message at that stage
    */
    $message = "===================================="."\n";
    $message .= "Kontaktanfrage"."\n";
    $message .= "===================================="."\n\n";
    $message .= "Name: ".$name."\n";
    $message .= "email: ".trim($_GET['email1'])."\n";
    $message .= "Telefon: ".trim($_GET['phone1'])."\n";
    $message .= "Strasse: ".trim($_GET['street'])."\n";
    $message .= "Stadt: ".trim($_GET['city'])."\n";
    $message .= "------------------------------------"."\n\n";
    $message .= "Nachricht: "."\n".trim($_GET['msg'])."\n\n";
    $message .= "===================================="."\n";

    if ( empty($name) or empty($_GET['phone1']) or empty($_GET['msg']) ) {
        echo 'errorarea|Bitte f&uuml;llen Sie folgende Felder aus: Name, Telefon und Nachricht!';
    } else {
        /* Test for return value of mail call and use to decide which message to send */
        $response=mail( $to, $subject, $message, "From: ".$email1 );
        if( $response==true ) echo 'contactarea|Vielen Dank '.$name.', Ihre eMail wurde verschickt.';
        else echo 'contactarea|Failed';
    }

?>


<script type='text/javascript' charset='utf-8'>
    function sendemail() {
      var http = navigator.appName=="Microsoft Internet Explorer" ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();
      var msg = document.contactform.msg.value;
      var name = document.contactform.name.value;
      var email1 = document.contactform.email1.value;
      var phone1 = document.contactform.phone1.value;
      var street = document.contactform.street.value;
      var city = document.contactform.city.value;

      document.contactform.send.disabled=true; 
      document.contactform.send.value='Absenden';

        /* Why use GET as the method here?? */
      http.open('GET', '/js/message.php?msg='+msg+'&name='+name+'&email1='+email1+'&phone1='+phone1+'&street='+street+'&city='+city+'&action=send');
      http.onreadystatechange = function(){
        if( http.status==200 && http.readyState==4 ){

            console.log(http.responseText);

            var response = http.responseText;
            if( response.indexOf('|') != -1 ) {
                update = response.split('|');
                document.getElementById(update[0]).innerHTML = update[1];
                document.contactform.send.disabled=false;
                document.contactform.send.value='Absenden';
            }
        }
      };
      http.send(null);  
    }
</script>