【问题标题】:Php mail Contact Form Errorphp邮件联系表错误
【发布时间】:2013-02-11 20:53:12
【问题描述】:

我已经购买了主机并创建了网络邮件。但是联系表的邮件功能在我的主机中不起作用。它打印“错误”。我不明白我错在哪里。我已经在谷歌搜索,查看了示例,但我没有解决我的问题。请帮助我。谢谢。 编辑信息:当我购买托管时,它是 Windows 托管。 maik 功能正在运行。但是,我通过了 linux 托管,然后在 cpanel 中再次创建了邮件。但是,现在邮件功能不起作用。再次感谢。

Edit2:您好,此代码在 Windows 托管中有效。我认为问题源于linux托管...... 这是我的代码:

contact.html

<html>
<form action="contact_form.php" method="POST" enctype="multipart/form-data" id="contactform">

            <fieldset class="row">

                <legend>Contact me :)</legend>

                <p>
                    <label for="your-name">Your Name</label>
                    <input type="text" name="name" id="your-name" class="input-xlarge">
                </p>

                <p>
                    <label for="your-email">Your Email <span class="required">(required)</span></label>
                    <input type="email" name="email" id="your-email" class="input-xlarge" required>
                </p>

                <p>
                    <label for="your-subject">Subject</label>
                    <input type="text" name="subject" id="your-subject" class="input-xlarge">
                </p>

                <p>
                    <label for="your-message">Your message <span class="required">(required)</span></label>
                    <textarea name="message" cols="50" rows="10" id="your-message" class="input-xxlarge" required placeholder="What do you want to say?"></textarea>
                </p>

                <!-- This is hidden for normal users -->
                <div class="hidden">
                    <label>
                        Do not fill out this field
                        <input name="s_check">
                    </label>
                </div>

                <p>
                    <input type="submit" id="submit" name="submit" class="primary" value="Send Message">
                </p>

                <p hidden id="response"></p>

            </fieldset>

        </form>
</html>

contact_form.php

<?php
if(isset($_POST['submit'])) {
        $to = 'info@xyz.com';

        $name = stripslashes($_POST['name']); //sender's name
        $email = stripslashes($_POST['email']); //sender's email
        $subject = stripslashes($_POST['subject']); // the subject

        echo $name."<br/>";
        echo $email."<br/>";
        echo $subject."<br/>";

        //The message you will receive in your mailbox
        $msg  = "From : $name \r\n";  //add sender's name to the message
        $msg .= "e-Mail : $email \r\n";  //add sender's email to the message
        $msg .= "Subject : $subject \r\n\n"; 
        $msg .= "---Message--- \r\n".stripslashes($_POST['message'])."\r\n\n";                        $msg .= "---User information--- \r\n"; //Title
        $msg .= "User IP : ".$_SERVER["REMOTE_ADDR"]."\r\n"; //Sender's IP
        $msg .= "Browser info : ".$_SERVER["HTTP_USER_AGENT"]."\r\n"; //User agent
        $msg .= "User come from : ".$_SERVER["HTTP_REFERER"]; //Referrer

        if  (mail($to, $subject, $msg, "From: $email\r\nReply-To: $email\r\nReturn-Path: $email\r\n")){

        //Message sent!

        echo nl2br("
        <div class=\"MsgSent\">
            <h1>Congratulations!!</h1>
            <p>Thank you <b><?=$name;?></b>, your message is sent!<br /> I will get back to you as soon as possible.</p>
        </div>
       ");
       exit;
        }
        else{

        // Display error message if the message failed to send
        echo "
        <div class=\"MsgError\">
            <h1>Error!!</h1>
            <p>Sorry <b><?=$name;?></b>, your message failed to send. Try later!</p>
        </div>";
        exit;
        }

}


?>

【问题讨论】:

  • 试试这个简单的版本并检查它是否发送邮件mail('example@example.com', 'My Subject', $message); 如果可行,请添加更多信息另见php.net/manual/en/function.mail.php
  • 我按你说的写了,但是没有发邮件:/

标签: php sendmail contact-form


【解决方案1】:

正如你在其他地方所说,mail() 函数存在问题——它的级别非常低,不容易使用。

建议您完全不要使用它——有几个非常好的 PHP 邮件程序类更容易使用、提供更好的结果并且功能更强大。

我的建议是使用phpMailer;它非常易于使用——正如您从this example page 中看到的那样。但是还有其他几个可用的替代库。

【讨论】:

    【解决方案2】:
    <p>Thank you <b><?=$name;?></b>, your message is sent!<br />
    <p>Sorry <b><?=$name;?></b>, your message failed to send. Try later!</p>
    

    应该是

    <p>Thank you <b>{$name}</b>, your message is sent!<br />
    <p>Sorry <b>{$name}</b>, your message failed to send. Try later!</p>
    

    也不确定你为什么使用 nl2br 一个简单的 echo 就足够了。

    【讨论】:

    • 谢谢,但是它又没用了。邮件功能有问题。是关于 php.ini 的吗?
    • 通常在 cPanel 服务器上 mail() 开箱即用。尝试 Perry 的建议,如果这不起作用,请联系您的房东。您能否也粘贴您收到的完整错误消息。
    • 在contact_form.php中,我写了这段代码: if(isset($_POST['submit'])) { $send = mail("info@XYZ.com","My Subject" "这是标题"); if($send) 回显“已发送”;否则回显“错误”; } else{ echo "提交错误"; } 但它打印错误。
    • 只需在一个空白文件中输入并在浏览器中运行它: 仅此而已。你看到了什么?
    • 是的,我正在将我的托管 linux 更改为 windows。我认为源 og 问题是托管问题。在托管邮件功能的 Windows 中。我不知道原因,但它正在工作。感谢您的关注。
    猜你喜欢
    • 1970-01-01
    • 2012-11-15
    • 2019-01-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-13
    • 1970-01-01
    • 2013-11-15
    相关资源
    最近更新 更多