Jump to content

Problem with PHP email content


AdamLewis

Recommended Posts

My code does everything it is supposed to, but within the body of the email I receive is just "My name is $name.
$from
$email
$message"

Here is the PHP code that I'm working with (obviously I've changed the email address for this purpose):

 

<?php
    $name = $_POST['name'];
    $email = $_POST['email'];
    $message = $_POST['message'];
    $from = 'From: Your Website';
    $to = '[email protected]';
    $subject = 'message.';
    $message = 'My name is $name.
$from
$email
$message';

if ($_POST['submit']) {
    mail($to, $subject, $message);
    $feedback = 'Thank you, your message has been sent.';
}

?>

 

Working with the HTML:

        <p id="feedback"><?php echo $feedback; ?></p>

<form method="post" action="contact.php">
    
    <label for="name">Name</label>
    <input id="name" name="name" placeholder="Type Here">
    
    <label for="email">Email</label>
    <input id="email" name="email" type="email" placeholder="Type Here">

    <label for="message">Message</label>
    <textarea name="message" placeholder="Type Here"></textarea>
    <input id="submit" name="submit" type="submit" value="Submit">
        
</form>

 

Thanks for your help in advance.

Link to comment
https://forums.phpfreaks.com/topic/287734-problem-with-php-email-content/
Share on other sites

When the php parser encountered a string enclosed in single quotes in the php document, it treats variables and any others spacial symbols like \n,&,|| and so on...as a literal text.In your example above not only the value of $name will be escaped but $from,$email,$message as well.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.