Jump to content

Omit null fields in email


cruizefest

Recommended Posts

Below is the code I'm using, and I would like for when the information is sent to email that it omits empty fields including the field name.  I'm not real sure on how to do this.  Any help would be great!!!!

 

Thanks!!!!!

 

]<?php
$SendFrom =    "Form Feedback <[email protected]>";
$SendTo =      "[email protected]";
$SubjectLine = "Feedback Submission";
$ThanksURL =   "thanks.html";  //confirmation page
  



foreach ($_POST as $Field=>$Value)
   $MsgBody .= "$Field: $Value\n";
   $MsgBody .= "\n" . @gethostbyaddr($_SERVER["REMOTE_ADDR"]) . "\n" .
   $_SERVER["HTTP_USER_AGENT"];
$MsgBody = htmlspecialchars($MsgBody, ENT_NOQUOTES);  //make safe


mail($SendTo, $SubjectLine, $MsgBody, "From: $SendFrom");
header("Location: $http://www.yourdomain.org");


?>
[/code

Link to comment
https://forums.phpfreaks.com/topic/247447-omit-null-fields-in-email/
Share on other sites

$SendFrom =    "Form Feedback <[email protected]>";
$SendTo =      "[email protected]";
$SubjectLine = "Feedback Submission";
$ThanksURL =   "thanks.html";  //confirmation page
  



foreach ($_POST as $Field=>$Value){
   if(!empty($Value)){
   $MsgBody .= "$Field: $Value\n";
   $MsgBody .= "\n" . @gethostbyaddr($_SERVER["REMOTE_ADDR"]) . "\n" .
   $_SERVER["HTTP_USER_AGENT"];
   }
}
$MsgBody = htmlspecialchars($MsgBody, ENT_NOQUOTES);  //make safe


mail($SendTo, $SubjectLine, $MsgBody, "From: $SendFrom");
header("Location: $http://www.yourdomain.org");


 

also, I see that you are using a lot of capital letters in your variable names.. word of caution.. variable names are case sensitive.. so $Variable does not equal $variable..

I normally always use lower case characters in my variables..

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.