1

Forgive me, I am new at this and the different php I have tried hasn't really worked at all. I've found examples for Natural Language forms, but none with working PHP and I'm familiar with PHP when used with traditional forms, but I'm having problems putting the two together.

The general form layout goes like this:
My name is [your name].
Today, I am [whatchya gonna do?].
I'm doing this because [c'mon,why?].
It's important that I [what you said you'd do] because [the big picture].
Shoot me an email with my awesome new declaration at [your email]
Button 1 (sends email)
Button 2 (copies all text and input fields to clipboard -- not as important right now)

Button 1, I want to send a copy to myself with a hard coded email address and also send a copy to the user with the email address they've entered.

This is a little messy right now, as I am simply trying to get it to work... again, I have no included PHP because at this point -- I've confused myself so much that I don't know what to include.

<form method="post" action="todayiam.php">
  <div class="naturallanguageform">
    <div class="nlfinner">
      <p class="line">
<span class="copy">My name is </span>
        <span class="inputcontainer"><input class="textinput" name="name" value="" placeholder="[your name]">.</span>
        <span class="copy">Today, I am </span>
        <span class="inputcontainer"><input class="textinput" name="todayiam" value="" placeholder="[whatchya gonna do?]">.</span> 
        <span class="copy">I'm doing this because </span>
        <span class="inputcontainer"><input class="textinput" name="why" value="" placeholder="[c'mon, why?]">.</span>
<span class="copy"> It's important that I </span>
        <span class="inputcontainer"><input class="textinput" name="whatusaid" value="" placeholder="[what you said you'd do]"></span>
        <span class="copy"> because </span>
        <span class="inputcontainer"><input class="textinput" name="because" value="" placeholder="[the big picture]">.</span>
      </p>    
<span class="copy">Shoot me an email with my awesome new declaration at</span>
        <span class="inputcontainer"><input class="textinput" name="email" value="" placeholder="[your email]"></span>

      <p class="line">
        <button class="button">Send to E-mail</button>
      </p>
 <p class="line">
        <button class="button">Copy to post in comments</button>
      </p>
    </div>
  </div>
</form>

Any assistance will be greatly appreciated.

Update:

<?php
       // from the form
       $name = trim(strip_tags($_POST['name']));
       $email = trim(strip_tags($_POST['email']));
       $message = htmlentities($_POST['todayiam']);

       // set here
       $subject = "Contact form submitted!";
       $to = 'email@gmail.com';

       $body = <<<HTML
$message
HTML;

       $headers = "From: $email\r\n";
       $headers .= "Content-type: text/html\r\n";

       // send the email
       mail($to, $subject, $body, $headers);

       // redirect afterwords, if needed
       header('Location: index.html');
?>

Another update

I have changed $headers = "From: $email\r\n"; to $headers = "From: email@gmail.com" . "\r\n" ; to set a static from address (my support email) and the email is still identifying as from CGI Mailer. I have verified that it's not a caching issue and the correct files are being used.

<?php 
if (isset($_POST['name'], $_POST['todayiam'], $_POST['why'], $_POST['whatusaid'], $_POST['because'], $_POST['email']) {
    // We enter this statement only if all the fields has been properly defined, this to avoid getting undefined index
    $name = $_POST['name'];
    $todayIam = $_POST['todayiam'];
    $why = $_POST['why'];
    $whatYouSaid = $_POST['whatusaid'];
    $because = $_POST['because'];
    $email = $_POST['email'];

    // We define the variables for using in mail()
    $to  = 'email@gmail.com';
    $to .= ', '.$email; // You wanted them to recieve a copy
    $subject = "Contact form submitted!";

    // You can put a lot more headers, check out the mail() documentation
    $headers = "From: email@gmail.com" . "\r\n" ;
    $headers .= "Content-type: text/html\r\n";

    // Compose a $message from all the variables
    $message = "My name is $name. ";
    $message .= "Today, I am $todayIam.";
    $message .= "I'm doing this because $why.";
    $message .= "It's important that I $whatYouSaid";
    $message .= "because $because.";

    if (mail($to, $subject, $message, $header)) {
        // Mail was successfully sent! Do something here
    }
}
?>

Before you posted your answer, I was working on this script:

<?php
 // from the form
       $name = trim(strip_tags($_POST['name']));
       $email = trim(strip_tags($_POST['email']));
       $message = htmlentities($_POST['todayiam']);


       // set here
       $subject = "Contact form submitted!";
       $to = 'email@gmail.com';

       $body = <<<HTML
$message
HTML;

       $headers = "From: $email\r\n";
       $headers .= "Content-type: text/html\r\n";

       // send the email
       mail($to, $subject, $body, $headers);

       // redirect afterwords, if needed
       header('Location: index.html');
?>

The $email = trim(strip_tags($_POST['email'])); field was pulling the user's email and using it for the sent from as noted in the $header... so it was working fine. With the new script, that you posted, I can't get it to work with my hard coded email OR the user's email as the FROM. Initially, I really wanted to understand what the differences were between the script, why one worked and the other didn't, but now... I'd really just like my email to be hard coded as the FROM. I'll worry about the differences later. As I said before, I really have tried to get this to work in many different forms... I am sure it's something simple that I am over looking as a novice to PHP. Thanks.

imjustarah
  • 161
  • 1
  • 6
  • 1
    1. You're gonna need a `submit`-button to actually trigger the `action` in the form. 2. Take a look at `POST` and [`mail()`](http://php.net/manual/en/function.mail.php). Give it a few tries after reading up on it, and if you still struggle, come back with a *specific* question. :) Nobody is going to code for you, but we'll gladly help you get on the right track, *after* you've tried some on your own. – Qirel Sep 28 '15 at 17:59
  • What's odd is, the current button actually does go to the test todayiam.php page when clicked... I thought I needed a submit button also, but didn't understand why it still did that. // I'm also not sure if the predefined text will need its own value/name -- a little confused as to how I can get that preset text in to the email itself (so it has my text, plus the user's text combined to make a full statement). Still messing with the random forms. – imjustarah Sep 28 '15 at 18:07
  • Right, so I'll give you a hint. In PHP, you can get the value of the name like `$name = $_POST['name'];` (works by the name of the input). You can then compose a mail, just check out [`mail()`](http://php.net/manual/en/function.mail.php), it has examples on how you can work that out. Give it a go, Google some examples, and if you can't figure it out, edit your post with what you tried, and maybe we'll be able to give you the right push :) – Qirel Sep 28 '15 at 18:12
  • Cool cool, looking at that now. I think I may have gotten it to work -- still trying to get the predefined text :D – imjustarah Sep 28 '15 at 18:14
  • And by predefined text you mean the placeholders? – Qirel Sep 28 '15 at 18:15
  • I mean the, "Today, I am" and "I'm doing this because" ... so all of the things that are preset in the front end style. So that when it's emailed, all of the content comes through as a complete thought and not just as what the user submitted as their answers. :) Will make a second note about the php I got to work, but still having issues with multiple values in the $message section. – imjustarah Sep 28 '15 at 18:32
  • Please, don't post batches of code in comments.. Update your post instead, so much easier to read! -- A variable can consist of variables. You can do like `$message = "My name is $name. Today, I am $todayiam"` and so on. – Qirel Sep 28 '15 at 18:37
  • Ok, $message = htmlentities($_POST['todayiam']); I am able to pull in the todayiam field, however, I am having trouble understanding how to pull multiple things in there. $message only pulls in the todayiam and nothing else, even if placed like $message = htmlentities($_POST['todayiam'], ['name']); etc. I see that $message is displayed below in order to format the email, would I need to add other defining elements like $message2 = (for the second line) and then display $message2 below in the $body in order to display multiple lines? – imjustarah Sep 28 '15 at 18:39
  • @Qirel, that's what I was trying to do and promptly deleted it when I realized what happened -- within seconds before I saw your post. Thank you for trying to assist with that though. :) – imjustarah Sep 28 '15 at 18:40
  • Ok, so... ['name'] is the same as $name? – imjustarah Sep 28 '15 at 18:42
  • I'll post you an answer instead, seems to be clogging up the comments here. Give me a few minutes. – Qirel Sep 28 '15 at 18:42

1 Answers1

1

Right, so after a bit of discussion from comments, I decided to post an answer instead, giving a bit more detail where it's more readable.

Your PHP code is missing the combination of all fields into $message. This can easily be done, as you can put a variable inside a string in PHP. I'll show you how. I'm also going to show you how you can avoid undefined indexes.

<?php 
if (isset($_POST['name'], $_POST['todayiam'], $_POST['why'], $_POST['whatusaid'], $_POST['because'], $_POST['email']) {
    // We enter this statement only if all the fields has been properly defined, this to avoid getting undefined index
    $name = $_POST['name'];
    $todayIam = $_POST['todayiam'];
    $why = $_POST['why'];
    $whatYouSaid = $_POST['whatusaid'];
    $because = $_POST['because'];
    $email = $_POST['email'];

    // We define the variables for using in mail()
    $to  = 'email@gmail.com';
    $to .= ', '.$email; // You wanted them to recieve a copy
    $subject = "Contact form submitted!";

    // You can put a lot more headers, check out the mail() documentation
    $headers = "From: $email\r\n";
    $headers .= "Content-type: text/html\r\n";

    // Compose a $message from all the variables
    $message = "My name is $name. ";
    $message .= "Today, I am $todayIam.";
    $message .= "I'm doing this because $why.";
    $message .= "It's important that I $whatYouSaid";
    $message .= "because $because.";

    if (mail($to, $subject, $message, $header)) {
        // Mail was sucsessfullly sent! Do something here
    }
}
?>
Qirel
  • 25,449
  • 7
  • 45
  • 62
  • Ok, so that makes a lot more sense. Seems like I was heading in the right direction. I wasn't quite sure how you would define the items like, $email = $_POST['email']; to reference them as $email, but now that you've posted this -- I feel kinda silly. Thankfully, I was heading down the right path lol. Question on the first line where you have the IF statement, that throws a browser error unless I comment it out, even if the form is completely filled. I had a theory that your labels didn't match mine (I updated two), but they do. Not sure why that is. – imjustarah Sep 28 '15 at 19:12
  • 1
    It's a check to make sure that you only do stuff when the form is actually filled, to avoid mail being sent, or having errors when you access `todayiam.php` without submitting the form. You can do other stuff, this was just an example. You really *should* have a check like that. – Qirel Sep 28 '15 at 19:14
  • Sure thing, working to see why it doesn't work and what I may need to change. I do appreciate this -- as I understand quite a bit more clearly now. – imjustarah Sep 28 '15 at 19:32
  • One last thing, I can't get the sender to change from CGI-Mailer to the user's email (as I had it previously) using the $email function you have -- I assume it should work. I've also found some information here http://stackoverflow.com/questions/13949834/php-contact-form-reply-to-sender and changed my code a couple different times based off of this and other suggestions online, but it still isn't working. Ideas? – imjustarah Sep 28 '15 at 20:40
  • Well, technically the user didn't send the email, the *server* did - on request by the user. As I said in my answer, there are a lot of different types of headers one can set. If you want, I can update my answer with some of them. Depends on what you want and what you need, actually, You can add `Reply-To` as well. – Qirel Sep 28 '15 at 21:41
  • I had a simple form before that was working with the user's email for the $email variable in the header as the from, but now I can't get it to work. I'm even trying to hardcore my email as the from email and I can't seem to get it to work either. Don't know what in the world I'm doing wrong. I can post the code if you'd like, unless you have a quick response to hard coding a from email instead of using the $email. I'd really just like for it not to say from CGI Mailer in the users inbox. Thanks. – imjustarah Sep 28 '15 at 23:13
  • $headers = "From: $email\r\n"; can simply be changed to $headers = "From: webmaster@example.com" . "\r\n" ; per the W3 Schools, but I can't seem to get it to work... tons of examples I'm seeing have it set like that too, or move the header up, I've also tried with reply to, etc. Should the above work okay? – imjustarah Sep 28 '15 at 23:54
  • Well, that change only makes the "from" a static value, instead of a dynamic one (with a variable). What exactly are you looking for, or what is the issue? – Qirel Sep 29 '15 at 18:16
  • I have updated the initial post to show the code and questions/issues I have. Thanks. – imjustarah Sep 29 '15 at 20:06
  • Still having issues with the from field in this -- I just want a static email address listed. No matter what I try I either make it so the form no longer works or CGIMailer is still the from. I've updated the code above with more info. Sorry, I am still learning PHP, but I'd imagine that the examples I've found online should work fine, but I miss be missing something. – imjustarah Oct 20 '15 at 01:46
  • 1
    Not 100% sure what exactly the issue is, but if I understand you correctly, you might need a 5th parameter in the `mail()`-function, something like this: `mail($to, $subject, $message, $header, "-f $from")`. Some servers are blocking what you're attempting, as it could mean you send people spam emails that appears to be from a certain company or person. Take a look at [this StackOverflow post](http://stackoverflow.com/a/2014104/4535200). – Qirel Oct 20 '15 at 14:33
  • 1
    well... you're amazing! All of the internet scouring and I never found anything like that post. Makes total sense from a security standpoint. I just threw in a valid email address that I created on the server and boom! It works. Thanks! – imjustarah Nov 20 '15 at 04:39
  • Happy to be of help! Hope you learned something! Additionally, you might want to take a look at [PHPMailer](https://github.com/PHPMailer/PHPMailer), it can do a lot more customization than the regular `mail()` function, provided you can send from an SMPT server. – Qirel Nov 21 '15 at 03:34