0

Using only PHP6 (no Javascript), there have been numerous failed attempts to format a plain text email. The primary issue seems to be either: 1. the failure to successfully construct returns and line breaks in the email text and/or, 2. the use of a mal-formed character set (charset) parameter.

Your help in resolving this matter would be appreciated. Earlier failed attempts included copying and pasting several relevant code snippets found via searching StackOverflow. Subsequent failed attempts can be seen - commented and otherwise - in the code provided here. Note that the email data is extracted (via PHP6 in the head section of the HTML5 document) from the simple HTML5 form also provided here. The PHP6 and the HTML5 occur in a single [1] file.

Please note that both the code and the HTML5 were originally indented properly when copied to paste here.

The questions asked here are different than the double/single quotation mark matter, but consistently using the double/single quotes where appropriate does help part of the problem. For example, a duplicate email is sent when the browser's refresh button is clicked (in Firefox), even thoug the form data is cleared and the submit button is not clicked again. There is also the matter of the earlier referenced character set (i.e., which is more universally accepted by email services).

<!DOCTYPE HTML>

<html>
<head>
    <title>Learning PHP6</title>


<?php 

   $field_display_feedback = "";
   echo "Inside PHP and about to enter IF: \$field_display_feedback = ( " . $field_display_feedback . " ) <br />";

   if (isset($_POST['SubmitButton']))
      {
      $field_suggestion = $_POST['YourSuggestion']; 
      $field_name = $_POST['YourName'];
      $field_phone = $_POST['YourPhone'];
      $field_email = $_POST['YourEmail'];

      $header_subject = 'Suggestion Submission';
      $header_to = 'JM@JustMe.com';
      $header_from = "From: Just Me <JM@JustMe.com>" . "\r\n";
      // $header_from .= "Content-type: text/plain; charset=iso-8859-1";
      $header_from .= "Content-Type: text/plain; charset=\"utf-8\"\r\n";

      $body_1 = 'Suggestion: ' . $field_suggestion . '\r\n';
      $body_2 = 'Suggester\'s Name: ' . $field_name . '\r\n';
      $body_3 = 'Suggester\'s Phone: ' . $field_phone . '\r\n';
      $body_4 = 'Suggester\'s Email: ' . $field_email . '\r\n';
      $total_body = $body_1 . $body_2 . $body_3 . $body_4 . '\r\n';

      $total_string = $header_from . $header_to . '\r\n' . $header_subject . '\r\n' . $total_body;
      mail($header_to, $header_subject, $total_body, $header_from);

      // Display a message that states Thank You!
      $field_display_feedback = 'Your Request Has Been Sent';
      echo "Completed YES condition for IF <br /> \$field_display_feedback = ( " . $field_display_feedback . " ) <br /> \$total_string = ( " . $total_string . " ) <br />";

      }else{

      // Display a cleared message area.          
      $field_display_feedback = "";
      echo "Outside IF <br /> \$field_display_feedback = ( " . $field_display_feedback . " ) <br /> \$total_string = ( " . $total_string . " ) <br />";
      }

?>


</head>

<body>

   <form method="post" action="index.php">
      <input style="margin: 0px 0px 0px 0px; padding: 0px; width: 260px; height: 18px; border: none; background-color: #ffffff; color: #cc0033; text-align: center;" type="text" value="<?php echo $field_display_feedback; ?>" name="DisplayFeedback" />
      <br /><textarea style="margin: 10px 0px 0px 0px; padding: 5px; width: 260px; height: 70px; border: none; background-color: #efefef;" placeholder="Your Suggestion Here ..." name="YourSuggestion" required></textarea>
      <br /><input style="margin: 10px 0px 0px 0px; padding: 5px; width: 260px; height: 18px; border: none; background-color: #efefef;" placeholder="Your Name Here ..." type="text" name="YourName" required />
      <br /><input style="margin: 10px 20px 0px 0px; padding: 5px; width: 260px; height: 18px; border: none; background-color: #efefef;" placeholder="Your Phone # ..." type="tel" name="YourPhone" required />
      <br /><input style="margin: 10px 20px 0px 0px; padding: 5px; width: 260px; height: 18px; border: none; background-color: #efefef;" placeholder="Your Email ..." type="email" name="YourEmail" required />
      <br /><input style="margin: 10px 0px 0px 0px;" class="button" type="submit" name="SubmitButton" value="Submit Suggestion" onmouseover="this.className='button_hover'" onmouseout="this.className='button'" />     
   </form>

   <!-- /*   Consider the condition:                   */ -->
   <!-- /*      if (isset($_POST['SubmitButton']))     */ -->
   <!-- /*   Some say this condition is better:        */ -->  
   <!-- /*      $_SERVER['REQUEST_METHOD'] == 'POST'   */ -->


</body>
</html>
Ambassador
  • 43
  • 5
  • 1
    learn basic php strings: `'`-quoted strings do **NOT** honor metacharacters. `'\r'` is just `r` as far as PHP is concerned. `"\r"` is a carriage return. – Marc B Sep 23 '15 at 21:29
  • Thank you - yes, I am learning php. Note; however, that line 21 does use the double quotes and it does not sem to work here. Both quotation mark types were attempted. Again, you are correct in that I have much to learn about php. Which of the two character set lines is the correct line? – Ambassador Sep 23 '15 at 22:42
  • your $header stuff is correct. there it's using `"`, but your body will just have literal `r` and `n` characters in them, since `'\r\n'` is nothing special to PHP. in `'`-quoted strings, the ONLY metacharacters are ``\\`` and `\'`. – Marc B Sep 24 '15 at 15:31

0 Answers0