Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Formatting message in feedback form 1

Status
Not open for further replies.

misi444

Technical User
Aug 21, 2009
14
LV
The website feedback form I use create plain text emails, unformatted:

Name: John Doe
email: sample@sample.com
phone: 1234567890
comments: text text text text text

I want to improve this, by adding some formatting, just to make message more accurate, for example by adding <pre> tags and HTML. How to make this correctly in this code?

Below is PHP code:
Code:
<?
 
if (!$_POST){
        $return_from_module = draw_forma();
} else {
        $error = check_data();
        if ($error){
        $return_from_module = draw_forma($error,$_POST);
        } else {
        $return_from_module = draw_success();
        }
}
 
 
function draw_success(){
 
$body ="";
foreach($_POST as $field=>$value)
{
        if($field!="submit")    $body .="$field: $value\r\n";
}
 
if(mail ("info@website.com",'from WEBsite sample.com',$body,"From:website"))
return 'Thank You!';
}
 
function check_data(){
        $error = '';
        $error_mark = 'style="color:red"';
        if (!$_POST["comments"])$error['comments'] = $error_mark;
    if (!isset($_POST["email"]) OR (!preg_match('/\b(?:mailto:)?([A-Z0-9._%+-]+@(?:[A-Z0-9-]+\.)+[A-Z]{2,7})\b/i', $_POST["email"]))) { $error['email'] = $error_mark; }
        if (!$_POST["Name"]) $error['Name'] = $error_mark;
        if (!$_POST["phone"]) $error['phone'] = $error_mark;
        return $error;
}
 
function draw_forma($error=null,$POST=null){
        $html='
        <form method=post action="">
    <style>
        #form-table TD {vertical-align:top;padding-top:5px}
        .zvezda {color:red}
    </style>
 
        #INS:message:name:text_above_form#<br><br>
        <table cellspacing=0 border="0">
        <tr>
                <td  valign=top>
    <table border=0 id=form-table>
            <tr>
                <td '.@$error["Name"].'>#ins:message:name:vards#<span class=zvezda>*</span></td>
                        <td><input type="text" class=input value="'.$POST["Name"].'" name="Name" size=35></td>
            </tr>
            <tr>
                <td '.@$error["email"].'>#ins:message:name:email#<span class=zvezda>*</span></td>
                        <td><input type="text" class=input value="'.$POST["email"].'" name="email" size=35></td>
            </tr>
                <tr>
                <td '.@$error["phone"].'>#ins:message:name:phone#<span class=zvezda>*</span></td>
                        <td><input type="text" class=input value="'.$POST["phone"].'" name="phone" size=35></td>
            </tr>
            <tr>
                <td '.@$error["comments"].'>#ins:message:name:Enquiry#<span class=zvezda>*</span></td>
                        <td><textarea name="comments" cols=42 rows=5>'.$POST["comments"].'</textarea></td>
            </tr>
                </table>
 
                </td>
        </tr>
        <tr>
                <td valign=top align=center>
                <input type="submit" value=" Send form " name="submit">
                </td>
        </tr>
</table>
#INS:message:name:form#
 
 
        </form>
        ';
        return $html;
}
 
?>
 
You'll need to construct your $body variable with the HTML you need to produce the desired formatting.

$body="<pre>$field </pre> <b>This text is bold $value</b>";

etc...


Note, that some email clients may strip out HTML or ignore it completely.


----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
so how to add html correctly? This doesn't work
(and I'm not sure for this first rows: $body ='<html>...<table>';):
Code:
function draw_success(){

$body ='
<html>
<head>
</head>
<body>
<table>';

foreach($_POST as $field=>$value)
{
        if($field!="submit")    $body .="<tr><td>$field</td><td>$value</td></tr>";
}

$body .= "</table>";
$body .= "</body></html>";

$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
$headers .= "From: website@domain.com\r\n";

if(mail ("test@domain.com",'Feedback from website domain.com', $body, $headers))
return 'Mail sent successfully.';
}
 
if you are going to send html or multipart email you are better off using phpmailer.
 
OK, if stay with plain text email format, how to add vertical space(one row) between 'phone' and 'comment' fields?

Name: John Doe
email: sample@sample.com
phone: 1234567890

comments: text text text text text

Also, which is correct syntax here:

[tt]$body .="$field: $value\r\n";[/tt]
or
[tt]$body .= $field .": ". $value . "\r\n";[/tt]
 
You technically should not need the <html> and <body> tags.

If your email client supports HTML it should render the table correctly.

As for the breaks, both are correct.

You can also try the <br> html tag if HTML is indeed being parsed.

If the HTML is not being shown, then there is something wrong in the email client side, and not the PHP side.




----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
try this for plain text

Code:
foreach($_POST as $field=>$value)
{
        if($field!="submit") {
          $body .= "$field \t $value\r\n";
        }
}

it is better however to do things the other way around. set the fields you wish to have and check whether the input is present

Code:
$pfields = array('Name', 'FirstName', 'Tel');
foreach ($_POST as $f=>$v){
  if (in_array($f, $pFields)){
    $body .=  "$field \t $value\r\n";
  }
}
 
Code:
foreach($_POST as $field=>$value)
{
        if($field!="submit") {
          $body .= "$field \t $value\r\n";
        }
}
this works ok for plain text, except one details: all the rows aligned fine - the same spacing from left side, except 1st row('Name'). Also, colon required after fields name. How it should be?

The second snippet doesn't work for me, could you clarify details?
 
 http://www.freeimagehosting.net/uploads/1156a49050.gif
c'mon. show some ingenuity

Code:
foreach($_POST as $field=>$value){
        if($field!="submit") {
          $body .= "\r\n$field: \t $value";
        }
}
 
yes, this works good. I want also get visitors IP address and Date-Time Stamp in email sent to me. I need include IP in email sent to me, not in the form.

$ip = getIP();
$host = gethostbyaddr($_SERVER['REMOTE_ADDR']);

How to implement this correctly?
 
we're not here to do your work for you. all the answers are clearly set out in the php manual.
 
yes, sure. I tried to add this lines

$body .= "\r\nDate: ".date("%Y-%m-%d")
$body .= "IP: ".$_SERVER['REMOTE_ADDR'];
$body .= "Host: ".gethostbyaddr($_SERVER['REMOTE_ADDR']);

here:
Code:
<?
 
if (!$_POST){
        $return_from_module = draw_forma();
} else {
        $error = check_data();
        if ($error){
        $return_from_module = draw_forma($error,$_POST);
        } else {
        $return_from_module = draw_success();
        }
}
 
 
function draw_success(){
 
$body ="";
foreach($_POST as $field=>$value)
{
        if($field!="submit")    $body .="$field: $value\r\n";
}

$body .= "\r\nDate: ".date("%Y-%m-%d")
$body .= "IP: ".$_SERVER['REMOTE_ADDR'];
$body .= "Host: ".gethostbyaddr($_SERVER['REMOTE_ADDR']);
 
if(mail ("info@website.com",'from WEBsite sample.com',$body,"From:website"))
return 'Thank You!';
}
But Contact page with feedback form has disappeared, just empty page.

 
turn error_display and error_reporting on and debug the error messages that you get.

please read the FAQ's in this forum about debugging.
 
sorry, problem was in missing semicolon:

[tt]$body .= "\r\nDate: ".date("%Y-%m-%d");[/tt]
 
this code not working:

Code:
[purple]
$pfields = array('Name', 'FirstName', 'Tel');
foreach ($_POST as $f=>$v){
  if (in_array($f, $pFields)){
    $body .=  "$field \t $value\r\n";
  }
}
[/purple]

due error: need be [tt]$pfields[/tt] not [tt]$pFields[/tt]:
Code:
$pfields = array('Name', 'email', 'phone','comments');
foreach ($_POST as $field=>$value){
  if (in_array($f, $pfields)){
    $body .=  "$field \t $value\r\n";
  }
}
 
Form without <html> and <body> tags should work also, not sure about syntax of 1st line, how this should be:

$body ="<table>"; $body ='<table>'; or $body .= "<table>"; ?

Code:
function draw_success(){

$body ="<table>";

foreach($_POST as $field=>$value)
{
        if($field!="submit")    $body .="<tr><td><tt>$field</tt></td><td><tt>$value</tt></td></tr>";
}

$body .= "</table>";

$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
$headers .= "From: website@domain.com\r\n";

if(mail ("test@domain.com",'Feedback from website domain.com', $body, $headers))
return 'Mail sent successfully.';
}
 
either of the first two will work without errors

the third will work but throw an E_NOTICE.

you might have found this out by trying before posting.
 
I tried your code before posting - it does not work.(it sent emails but without any data, it seems)
It began work after I have corrected syntax from $pFields to $pfields.
 
if you had error_reporting and error display turned on you would have debugged this in a second.

my reply @07h11 related to your question of 07h03.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top