Anthosiastic
Anthosiastic

Reputation: 69

Contact Form directly to email itself using php

I tried many ways to solve this but seems like I hit the wall. It should be working with this way, but I couldnt find any mistake as it's written that there is a

syntax error, unexpected $end

after I click submit button.

First of all, I type the code like this.

<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>

This file named sendContact.php

         <?php
            $to = '[email protected]';
            $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>

I already make as simple as this and also make sure that the details on the email will be read easily. Can you point any mistakes above?

Cheers

Upvotes: 1

Views: 872

Answers (1)

Funk Forty Niner
Funk Forty Niner

Reputation: 74216

There cannot be any spaces between <<< and EMAIL in <<< EMAIL

It must be <<<EMAIL (see footnotes for more information)

Consult:

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

for more information on heredoc

Example #1 Invalid example (from the manual)

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

Example #2 Heredoc string quoting example

<?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
));
?>

and in your case: (which I tested and working)

I changed $topic to $phone otherwise it would have thrown an error.

You will need to further modify your PHP to reflect the appropriate changes.

<?php
$to = '[email protected]';
$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>

Footnotes:

And as Kostis stated in a comment --- "It's the closing tag that must not have any whitespace in front. It's also important to realize that the first character before the closing identifier must be a newline as defined by the local operating system."


Upvotes: 18

Related Questions