Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error in php while trying to send email

Tags:

html

php

email

I am learning php and have a book which has lots of examples and exercises in. The one I'm doing is about sending email. I copied the code to the letter, but I am getting an error:

Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in C:\website2\www\sendscript.php on line 6 

Why is this happening, any idea?

Here is my PHP script:

<?php
    if(($_POST['sender_name']=="")||($_POST['sender_email']=="")||($_POST['message']=="")){
        header("Location: sendemail.html");
        exit;
    }

    $email = "Sender's name:\t$_POST['sender_name']\n";
    $email .= "Sender's email:\t$_POST['sender_email']\n";
    $email .= "Message:\t$_POST['message']\n";
    $to = "[email protected]";
    $subject = "did script work?";
    $mailheaders = "From: Stafford King";
    $mailheaders .= "Reply to: $_POST['sender_email']\n";
    mail($to, $subject, $email, $mailheaders);
?>
<html>
    <head></head>
    <body>
        <h1>Email sent!</h1>
    </body>
</html>

And here is my email input form:

<html>
  <head></head>
  <body>
    <form method = "post" action = "sendscript.php">
        <p><strong>Your name:</strong><br />
        <input type = "text" name = "sender_name" size = "30"</p>
        <p><strong>Your email address:</strong><br />
        <input type = "text" name = "sender_email" size = "30"</p>
        <p><strong>Message:</strong><br />
    <textarea name = "message" cols = "30" rows = "5" wrap = "virtual"></textarea></p>
        <p><input type = "submit" name = "submit" value = "send email"></p>
    </form>
  </body>
</html>

Thanks guys :)

like image 308
imulsion Avatar asked Jan 23 '26 05:01

imulsion


2 Answers

You need to wrap your array variables in curly brackets {} or concatenate them within the string:

OPTION 1

$email = "Sender's name:\t{$_POST['sender_name']}\n";

OPTION 2

$email = "Sender's name:\t" . $_POST['sender_name'] . "\n";

NOTE:

Use of an IDE such as Dreamweaver (license fee), NetBeans or Eclipse (both open source & free) would have caught this immediately. Try one of these, rather than Notepad or Notepad++.

like image 53
Matt Avatar answered Jan 24 '26 18:01

Matt


$email = "Sender's name:\t$_POST['sender_name']\n";

can't be resolved very well. Use

$email = "Sender's name:\t{$_POST['sender_name']}\n";

instead. Alternatively, you could concatenate or use sprintf:

$email = "Sender's name:\t" . $_POST['sender_name'] . "\n";
$email = sprintf("Sender's name:\t%s\n", $_POST['sender_name']);
like image 24
Waleed Khan Avatar answered Jan 24 '26 19:01

Waleed Khan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!