douyao1856 2015-06-16 18:45
浏览 88

为什么我的php没有将我的联系表单输入发送到我的电子邮件?

I am trying to write some PHP which will submit my "contact form" and send the input to a given email.

On the internet, I found the following code that does this:

<?php 
$action=$_REQUEST['action']; 
if ($action=="")    /* display the contact form */ 
    { 
    ?> 
    <form  action="" method="POST" enctype="multipart/form-data"> 
    <input type="hidden" name="action" value="submit"> 
    Your name:<br> 
    <input name="name" type="text" value="" size="30"/><br> 
    Your email:<br> 
    <input name="email" type="text" value="" size="30"/><br> 
    Your message:<br> 
    <textarea name="message" rows="7" cols="30"></textarea><br> 
    <input type="submit" value="Send email"/> 
    </form> 
    <?php 
    }  
else                /* send the submitted data */ 
    { 
    $name=$_REQUEST['name']; 
    $email=$_REQUEST['email']; 
    $message=$_REQUEST['message']; 
    if (($name=="")||($email=="")||($message=="")) 
        { 
        echo "All fields are required, please fill <a href=\"\">the form</a> again."; 
        } 
    else{         
        $from="From: $name<$email>
Return-path: $email"; 
        $subject="Message sent using your contact form"; 
        mail("vik.srk@hotmail.com", $subject, $message, $from); 
        echo "Email sent!"; 
        } 
    }   
?> 

When I click submit, nothing is being sent to my inbox!

  • 写回答

1条回答 默认 最新

  • dpsx99068 2015-06-16 19:13
    关注

    I updated your code to actually test if the mail was sent... I suspect you haven't configured an SMTP Email server, which means PHP has no way to send an email.

    Your code now checks if mail() was successful.

    <?php 
    $action=$_REQUEST['action']; 
    if ($action=="")    /* display the contact form */ 
        { 
        ?> 
        <form  action="" method="POST" enctype="multipart/form-data"> 
        <input type="hidden" name="action" value="submit"> 
        Your name:<br> 
        <input name="name" type="text" value="" size="30"/><br> 
        Your email:<br> 
        <input name="email" type="text" value="" size="30"/><br> 
        Your message:<br> 
        <textarea name="message" rows="7" cols="30"></textarea><br> 
        <input type="submit" value="Send email"/> 
        </form> 
        <?php 
        }  
    else                /* send the submitted data */ 
        { 
        $name=$_REQUEST['name']; 
        $email=$_REQUEST['email']; 
        $message=$_REQUEST['message']; 
        if (($name=="")||($email=="")||($message=="")) 
            { 
            echo "All fields are required, please fill <a href=\"\">the form</a> again."; 
            } 
        else{         
            $from="From: $name<$email>
    Return-path: $email"; 
            $subject="Message sent using your contact form"; 
            $MailSent = mail("vik.srk@hotmail.com", $subject, $message, $from); 
            if($MailSent == true) {
                 echo 'Mail Sent';
            } else {
                 echo 'Mail failed';    
            }
        }   
    ?> 

    If it fails, then you know there's a problem with the mail function. You can also put error_reporting(-1) at the top to turn on errors, in case you have any syntax errors. Like I said before, I'm guessing you haven't configured a mail server which is why you aren't getting any emails.

    </div>
    
    评论

报告相同问题?