【问题标题】:Contact Form directly to email itself using php使用 php 直接联系表格以通过电子邮件发送自己
【发布时间】:2014-01-08 23:08:19
【问题描述】:

我尝试了很多方法来解决这个问题,但似乎我碰壁了。它应该以这种方式工作,但我找不到任何错误,因为它写的是有一个

语法错误,意外的 $end

点击提交按钮后。

首先,我这样输入代码。

<form class="form" method="post" action="sendContact.php">  
                <table>
                    <tr>
                        <td class="contact-firstcol"> <label for="name">Name</label> </td>
                        <td class="contact-secondcol"> : </td>
                        <td class="contact-thirdcol"> <input type="text" name="name" id="name" /> </td>
                    </tr>
                    <tr>
                        <td class="contact-firstcol"> <label for="email">Email</label> </td>
                        <td class="contact-secondcol"> : </td>
                        <td class="contact-thirdcol"> <input type="text" name="email" id="email" /> </td>
                    </tr>
                    <tr>
                        <td class="contact-firstcol"> <label for="phone">Phone</label> </td>
                        <td class="contact-secondcol"> : </td>
                        <td class="contact-thirdcol"> <input type="text" name="phone" id="phone" /> </td>
                    </tr>
                    <tr>
                        <td class="contact-firstcol"> <label for="message">Message</label> </td>
                        <td class="contact-secondcol"> : </td>
                        <td class="contact-thirdcol"> <textarea id="message" name="message"></textarea> </td>
                    </tr>
                    <tr>
                        <td class="contact-firstcol"></td>
                        <td class="contact-secondcol"></td>
                        <td class="contact-thirdcol"> <input type="submit" name="submit" value="SUBMIT" /> </td>
                </table>    
            </form>

这个文件名为 sendContact.php

         <?php
            $to = 'abc@abc.com';
            $subject = 'from email contact';

            $name = $_POST ['name'];
            $email = $_POST ['email'];
            $phone = $_POST ['phone'];
            $message = $_POST ['message'];

            $body = <<< EMAIL
            Hi! My name is $name
            $message.
            From : $name
            Email : $email
            Topic : $topic
            EMAIL;
            $header = "From: $email";

            if (isset($_POST)) {
                if ($name == '' || $email == '' || $topic == '' || $message = '') {
                    $feedback = 'Please fill in any fields.';

                } else {

                mail( $to, $subject, $body, $header);
                $feedback = 'Thanks for the information, we will get back to you in 24 hours.';
                }
            }
?>
<p class="feedback"> <?php echo $feedback ?> </p>

我已经做到了如此简单,并确保电子邮件中的详细信息易于阅读。你能指出上面的任何错误吗?

干杯

【问题讨论】:

    标签: php html forms email


    【解决方案1】:

    &lt;&lt;&lt; EMAIL中的&lt;&lt;&lt;EMAIL之间不能有空格

    必须是&lt;&lt;&lt;EMAIL(更多信息请参见脚注)

    咨询:

    http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc

    有关heredoc的更多信息

    示例 #1 无效示例(来自手册)

    <?php
    class foo {
        public $bar = <<<EOT
    bar
        EOT;
    }
    ?>
    

    Example #2 Heredoc 字符串引用示例

    <?php
    $str = <<<EOD
    Example of string
    spanning multiple lines
    using heredoc syntax.
    EOD;
    
    /* More complex example, with variables. */
    class foo
    {
        var $foo;
        var $bar;
    
        function foo()
        {
            $this->foo = 'Foo';
            $this->bar = array('Bar1', 'Bar2', 'Bar3');
        }
    }
    
    $foo = new foo();
    $name = 'MyName';
    
    echo <<<EOT
    My name is "$name". I am printing some $foo->foo.
    Now, I am printing some {$foo->bar[1]}.
    This should print a capital 'A': \x41
    EOT;
    ?>
    

    Example #3 Heredoc in arguments 示例

    <?php
    var_dump(array(<<<EOD
    foobar!
    EOD
    ));
    ?>
    

    在你的情况下:(我测试并工作)

    我把$topic改成了$phone,否则会报错。

    您需要进一步修改您的 PHP 以反映适当的更改。

    <?php
    $to = 'abc@abc.com';
    $subject = 'from email contact';
    
    $name = $_POST ['name'];
    $email = $_POST ['email'];
    $phone = $_POST ['phone'];
    $message = $_POST ['message'];
    
    $body = <<<EMAIL
    Hi! My name is $name
    $message.
    From : $name
    Email : $email
    Topic : $topic
    EMAIL;
    $header = "From: $email";
    
    if (isset($_POST)) {
        if ($name == '' || $email == '' || $phone == '' || $message = '') {
            $feedback = 'Please fill in any fields.';
    
        } else {
    
        mail( $to, $subject, $body, $header);
        $feedback = 'Thanks for the information, we will get back to you in 24 hours.';
        }
    }
    ?>
    <p class="feedback"> <?php echo $feedback ?> </p>
    

    脚注:

    正如 Kostis 在评论中所说 --- “它是结束标记,前面不能有任何空格。认识到结束标识符之前的第一个字符必须是由定义的换行符也很重要本地操作系统。”


    【讨论】:

    • 它仍然无法正常工作。我很困惑这有什么问题。有时它会发送到垃圾邮件。我不明白为什么。有什么我想念的吗?
    • 其实这不是真的。它是结束标记,前面不能有任何空格“同样重要的是要意识到结束标识符之前的第一个字符必须是本地操作系统定义的换行符”(来自您提供的链接)
    • @Anthosiaastic Kostis 提出了观点。 EMAIL; 之前也不应该有任何空格。查看我的编辑。
    • 注意:还需要将$topic改为$phone,否则会报错。我现在将在我的答案中对其进行编辑。 @Anthosiaastic 我刚刚测试了它,它工作正常。
    • 可能是的。既然你已经测试过了,应该没问题。干杯@Fred-ii-
    猜你喜欢
    • 2014-03-27
    • 2012-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多