php - 联系表格直接使用 php 给自己发电子邮件

标签 php html forms email

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

syntax error, unexpected $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>

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

干杯

最佳答案

<<< 之间不能有任何空格和 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;
}
?>

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 example

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

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

我改变了$topic$phone否则会引发错误。

您将需要进一步修改您的 PHP 以反射(reflect)适当的更改。

<?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 在评论中所说 --- “它是结束标记,前面不能有任何空格。同样重要的是要意识到结束标识符之前的第一个字符必须是换行符,如定义的那样本地操作系统。”


关于php - 联系表格直接使用 php 给自己发电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21008512/

相关文章:

php - 用于 postgreSQL 的 ssh 隧道

php - Paypal 10486 no “Buyer'的体验”在重定向上

javascript - 使用 Closest 属性 Jquery 获取 Div 元素内输入标签的计数

javascript - 动态元素在调用时不响应

php - 如何将 mysql 记录传递给 php 并确保它是 UTF-8 编码

php - 迁移后无法执行批量操作

javascript - 使用 javascript 获取 nextElementSibling 的属性值

php - 无法以表单形式预先填充数据库中的数据

php - Cakephp 表单必须提交两次才能工作

html - 将 HTML 表单分为两部分