SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Zealot AVdes's Avatar
    Join Date
    Jun 2005
    Location
    GA
    Posts
    106
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Keeping the text format for the $messge field

    I created an email script using the mail() function. Thanks to the help of the many talented individuals here I have got it working and secured it.

    But when anyone enters a coment such as:
    I'm still hungry!

    it comes out in the email as:
    I\'m still hungry!

    How do I make sure it keeps the text format.

    Here is my code
    Code:
    		
          //Checks single-line inputs:
          //Returns false if text contains newline characters
          function has_no_newlines($text) 
          {
          	return preg_match("/(%0A|%0D|\n+|\r+)/i", $text);
          }
                
          //Checks multi-line inputs:
          //Returns false if text contains newline followed by email-header specific strings
          function has_no_emailheaders($text)
          {
         		return preg_match("/(%0A|%0D|\n+|\r+)(content-type:|to:|cc:|bcc:)/i", $text);
          }
    
     		// Effectively turns off dangerous register_globals without having to edit php.ini
    		if (ini_get(register_globals))  // If register_globals is enabled
    		{ 
    			// Unset $_GET keys
      			foreach ($_GET as $get_key => $get_value) {
        		if (ereg('^([a-zA-Z]|_){1}([a-zA-Z0-9]|_)*$', $get_key)) eval("unset(\${$get_key});");
      			} 
      			// Unset $_POST keys
      			foreach ($_POST as $post_key => $post_value) {
        		if (ereg('^([a-zA-Z]|_){1}([a-zA-Z0-9]|_)*$', $post_key)) eval("unset(\${$post_key});");
     			 } 
     			 // Unset $_REQUEST keys
      			foreach ($_REQUEST as $request_key => $request_value) {
        		if (ereg('^([a-zA-Z]|_){1}([a-zA-Z0-9]|_)*$', $request_key)) eval("unset(\${$request_key});");
     			 }
    		}
    	
    	   // All form fields are automatically passed to the PHP script through the variable $_post Introduced in PHP version 4.1.0
    	   // for earlier versions, use the array $HTTP_POST_VARS
    		$name = $_POST['name'];
    		$mail = $_POST['mail'];
    		$telephone = $_POST['telephone'];
    		$city = $_POST['city'];
    		$state = $_POST['state'];
    		$country = $_POST['country'];
    		$comments = $_POST['comments'];
    		
    		$to = "someone@this.com";
    		$subject = "Technical Support";
    		$headers = "From: " . $name;
    		$message = "From: " . $name . "\r\n". $mail . "\r\n" . $telephone . "\r\n" . $city . "\r\n" . $state . "\r\n" . $country . "\r\n" . "\r\n" . $comments;		
    		
    		// PHP form validation: checks that the Email field contains a valid email address and the Name, Country, and Comments fields aren't empty
    		// preg_match performs a regular expression match. It's a very powerful PHP function to validate form fields and other strings - see PHP manual for details
    		if ($name == "") {
      			echo "<h4>You did not provide your name.</h4>";
      			echo "<a href='javascript:history.back(1);'>Back</a>";
    			} elseif (!preg_match("/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/", $mail)) {
      			echo "<h4>Invalid email address</h4>";
      			echo "<a href='javascript:history.back(1);'>Back</a>";
    			} elseif ($telephone == "") {
      			echo "<h4>You did not provide a phone number</h4>";
      			echo "<a href='javascript:history.back(1);'>Back</a>";
    			} elseif ($country == "") {
      			echo "<h4>It is helpfull for us to know which contry you are in.</h4>";
      			echo "<a href='javascript:history.back(1);'>Back</a>";
    			} elseif ($comments == "") {
      			echo "<h4>Please provide a brief descrition about how we can help you.</h4>";
      			echo "<a href='javascript:history.back(1);'>Back</a>";
    			} 
    			
    			/*
    			else {
    			echo "<h4>true</h4>";
    			}
    			*/
    		
    		// Sends the mail and outputs the "Thank you" string if the mail is successfully sent or the error string if it fails	
    			elseif (mail($to,$subject,$message,$headers)) {
    			echo "<h4>Thank you for your request for technical support information from Sandhill Scientific.<br />  Your message will be answered expeditiously.</h4>";
    			} else {
      			echo "<h4>We seem to be expiriencing technical dificulties.<br /> 
      			Plese feel free to contact us through email at:<br /> technicalsupport@sandhillsci.com<br />
      			by Fax: 303-470-2975 or by telephone: 303-470-7020<br />
      			Monday thorught Friday 6am-5pm MST<br />
      			(available after hours by calling in to the above phone number and following the message.)</h4>";
    			}
    		// error reporting enabled
    	   // error_reporting(E_ALL ^ E_NOTICE);
    	?>
    CSS Club mantra; "I hate IE6, I hate IE6, I hate IE6".
    http://www.avdes.com

  2. #2
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,788
    Mentioned
    151 Post(s)
    Tagged
    3 Thread(s)
    use
    PHP Code:
    stripslashes($message); 
    to remove the I'\m slash. Also you could look at nl2br() to retain line breaks and text formatting.
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  3. #3
    SitePoint Zealot AVdes's Avatar
    Join Date
    Jun 2005
    Location
    GA
    Posts
    106
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks. My form is now compleet.
    CSS Club mantra; "I hate IE6, I hate IE6, I hate IE6".
    http://www.avdes.com


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •