使用php直接联系表格发送电子邮件

Ant*_*tic 1 html php forms email

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

语法错误,意外$ 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>
Run Code Online (Sandbox Code Playgroud)

这个文件名为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>
Run Code Online (Sandbox Code Playgroud)

我已经做了这么简单,并确保可以轻松阅读电子邮件的详细信息.你能指出上面的任何错误吗?

干杯

Fun*_*ner 18

不能有之间的任何空间<<<,并EMAIL<<< EMAIL

它必须是<<<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;
}
?>
Run Code Online (Sandbox Code Playgroud)

示例#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;
?>
Run Code Online (Sandbox Code Playgroud)

示例#3参数示例中的Heredoc

<?php
var_dump(array(<<<EOD
foobar!
EOD
));
?>
Run Code Online (Sandbox Code Playgroud)

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

我改变了$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>
Run Code Online (Sandbox Code Playgroud)

脚注:

正如Kostis在评论中所说的那样 - "这是结尾标记,前面不能有任何空白.同样重要的是要知道结束标识符之前的第一个字符必须是本地操作系统定义的换行符."