-1

I have a block of code that takes a form, posts it to the same page with a thank you message and then proceeds to email it to my account.

As of right now, it displays like this in the email end:

hello world., Name: Hello World, , Email: HelloWorld@fake.com

Which means it goes message, name, then email.

How can I make it so it displays similar to something like this in the email..

Name: Hello World
Email: HelloWorld@fake.com

Message: hello world...

Here is the code I am using:

<?php
    if (isset($_REQUEST['email']))
    //if "email" is filled out, send email
       {
            //send email
            $name = $_REQUEST['name'] ;
            $email = $_REQUEST['email'] ;
            $subject = $_REQUEST["My Portfolio Website"] ;
            $message = $_REQUEST['message'].", Name: ".$name.", Email: ".$email;
            mail("MyEmail@gmail.com", $subject, $message, "From:" . $email);
            echo "<h1>Thank you for contacting me. I will get back to you ASAP!</h1>";
        }else{
        //if "email" is not filled out, display the form
            echo "<form method='post' action='index.php'>

                <input type='text' input name='name' id='name' class='contacttext' placeholder='Your Name' required>
                <input type='text' input name='email' id='email' class='contacttext' placeholder='Your Email Address' required>
                <textarea name='message' id='message' class='contacttext' placeholder='Your Message' cols='55' rows='5' required></textarea>

                <input type='submit' id='submit' class='submitcontacttext' value='Send'>
                </form>";
        }
?>   

Also, currently the subject is not setting itself, what would be the cause of this?

knocked loose
  • 3,142
  • 2
  • 25
  • 46

1 Answers1

0

Try the following:

// I don't see 'My Portfolio Website' this being passed into your form
// did you mean to do? $subject = 'My Portfolio Website';
$subject = $_REQUEST["My Portfolio Website"];
// add \n to add line breaks.
// $message = $_REQUEST['message'] . "\nName: " . $name . "\nEmail: ".$email;

/**
 * this will make it easier to re-order the elements or add new ones.
 */
$message = implode("\n", [
    'Name: ' . $name,
    'Email: ' . $email,
    'Message ' . $_REQUEST['message'],
]);
Jonathan
  • 2,778
  • 13
  • 23
  • Would I just add `"\nMessage: " .$message` at the end of this to get it to format so it sends name/email, then messages, like in my example? As of right now, calling the `$message = $_REQUEST['message']` is making it so the message is the first thing displayed in the email. – knocked loose May 04 '15 at 13:56
  • yes you can do that. – Jonathan May 04 '15 at 14:01
  • Will that duplicate the message function? `$message = $_REQUEST['message'] . "\nName: " . $name . "\nEmail: ".$email; "\n \nMessage: ".$message;` – knocked loose May 04 '15 at 14:04
  • I added a new example which will make it easier to work with. – Jonathan May 04 '15 at 14:09