Jump to content

Recommended Posts

Okay.  Below is my PHP code for the mailform on my site.  I'm getting the e-mail, but the weird thing is the from info.  It SHOULD be "firstname lastname - email", but all I'm getting is a "-.email"... you'll see. Example of the problem is below, followed by the code.

 

Thank you in advance for helping a complete noob.

 


 

FROM: [email protected]

 

Firstname = Test

Lastname = Sample

phone number = 123-456-7890

cell = 123-456-7890

e-mail = [email protected]

message = This is a test message to demonstrate the weird FROM filed I get in my e-mails.

 


 

<?php

$firstname = $_POST['firstname'];

$lastname = $_POST['lastname'];

$phone = $_POST['phone'];

$cell = $_POST['cell'];

$email = $_POST['email'];

$comments = $_POST['comments'];

$valNoBot = $_POST['hiddenField'];

 

/*Sending Email*/

if ($valNoBot == "ihateyoubot"){

$to = "[email protected]";

$subject = "New Mail";

$message = "Form1 Information \n\nFirstname = $firstname\r\nLastname = $lastname\r\nphone number = $phone\r\ncell = $cell\r\ne-mail = $email \nmessage = $comments";

$from = "$firstnamename $lastnamename - $email";}

else {

print "<h3>Go away you stinkin' bot!</h3> \n";

} // end if

 

 

if(mail($to, $subject, $message, "From: $from"))

header("location: contactsuccess.html");

else

header("location: contactfailure.html");

?>

 

Link to comment
https://forums.phpfreaks.com/topic/159443-having-weird-issue-with-php-mailform/
Share on other sites

$from = "$firstnamename $lastnamename - $email";}

 

should be....

 

$from = "$firstname $lastname - $email";}

 

Holy crapsticks!

 

Do you have any idea how many times I looked at this code to figure out that problem????

Wow.  I feel like a tool.

 

Thanky uo so much!

<?php
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$phone = $_POST['phone'];
$cell = $_POST['cell'];
$email = $_POST['email'];
$comments = $_POST['comments'];
$valNoBot = $_POST['hiddenField'];

/*Sending Email*/
if ($valNoBot == "ihateubot"){
$to = "[email protected]";
$subject = "New Mail";
$message = "Form1 Information \n\nFirstname = $firstname\r\nLastname = $lastname\r\nphone number = $phone\r\ncell = $cell\r\ne-mail = $email \nmessage = $comments";
$from = "$firstname $lastname - $email";}
else {
print "<h3>Go away you stinkin' bot!</h3> \n";
} // end if


if(mail($to, $subject, $message, "From: $from"))
header("location: contactsuccess.html");
else
header("location: contactfailure.html");
?>

solution:

 

<?php

function clean($text){
$text=trim($text);
$text.=strip_tags('allowed tags'$text);
$text.=htmlspecialchars($text,ENT_NOQUOTES);
$text.=mysql_real_escape_string($text);
$text.=filter_var($text,FILTER_SANITIZE_STRING);
}
$firstname = clean($_POST['firstname']);
$lastname = clean($_POST['lastname']);
$phone = $_POST['phone'];
$cell = clean($_POST['cell']);
$email = $_POST['email'];
$comments = clean($_POST['comments']);
$valNoBot = clean($_POST['hiddenField']);

/*Sending Email*/
if ($valNoBot == "ihateubot"){
$to = "[email protected]";
$subject = "New Mail";
$message = "Form1 Information \n\nFirstname = $firstname\r\nLastname = $lastname\r\nphone number = $phone\r\ncell = $cell\r\ne-mail = $email \nmessage = $comments";
$from = "$firstname $lastname-$email";}
str_replace('.','',$from);//replacing dots with nothing
else {
print "<h3>Go away you stinkin' bot!</h3> \n";
} // end if


if(mail($to, $subject, $message, "From: $from"))
header("location: contactsuccess.html");
else
header("location: contactfailure.html");
?>

Couple things:

 

[*]I wasn't trying to be a smart ass with my last post.  I was sincerely asking.

[*]I didn't write the actual argument in line 5.  It was code given to me by another helpful member of this forum

[*]I followed the link in your post, and admittedly, I'm lost reading it.

[*]I did state in my inital post that I am a total noob to using PHP (Or web programming, for that matter)

[*]I'm counthing the {'s in my if/else statements, and they seem to be adding up.  I'm obviously missing something, but I'm not sure what.

[*]I am very thankful for your help.  Please do not think otherwise.

 

Here's my code as it looks right now:

<?php

function clean($text){
$text=trim($text);
$text.=strip_tags('allowed tags'$text);
$text.=htmlspecialchars($text,ENT_NOQUOTES);
$text.=mysql_real_escape_string($text);
$text.=filter_var($text,FILTER_SANITIZE_STRING);
}
$firstname = clean($_POST['firstname']);
$lastname = clean($_POST['lastname']);
$phone = $_POST['phone'];
$cell = clean($_POST['cell']);
$email = $_POST['email'];
$comments = clean($_POST['comments']);
$valNoBot = clean($_POST['hiddenField']);

/*Sending Email*/
if ($valNoBot == "ihateubot"){
$to = "[email protected]";
$subject = "New Contact";
$message = "Form1 Information \n\nFirstname = $firstname\r\nLastname = $lastname\r\nphone number = $phone\r\ncell = $cell\r\ne-mail = $email \nmessage = $comments";
$from = "$firstname $lastname-$email";}
str_replace('.','',$from);//replacing dots with nothing
else {
print "<h3>Go away you stinkin' bot!</h3> \n";
} // end if


if(mail($to, $subject, $message, "From: $from"))
header("location: contactsuccess.html");
else
header("location: contactfailure.html");
?>

I didn't think you were being a smartass.  I was just clarifying the intention of my post, as it seemed to not be clear.

 

error #1) $text.=strip_tags('allowed tags'$text);

 

That is incorrect.  stip_tags requires 1 argument, and an optional 2nd.  As it stands right now, you have a string 'allowed tags' mashed together with a variable $text.  That's what you are getting your first error from.  I'm going to assume you just want to strip all tags from $text, so it should be like so:

 

$text.=strip_tags($text);

 

If there were some tags you do wish to allow, that's where the 'allowed tags' argument comes in, but it should look like this:

 

$text.=strip_tags($text, 'allowed tags');

 

arguments should be separated by a comma.  Also, you would actually want to replace 'allowed tags' with the tags you want to allow.  For instance, if you want to strip everything except anchor tags, you would do this:

 

$text.=strip_tags($text, '<a>');

 

error #2)

/*Sending Email*/

if ($valNoBot == "ihateubot"){

$to = "[email protected]";

$subject = "New Contact";

$message = "Form1 Information \n\nFirstname = $firstname\r\nLastname = $lastname\r\nphone number = $phone\r\ncell = $cell\r\ne-mail = $email \nmessage = $comments";

$from = "$firstname $lastname-$email";}

str_replace('.','',$from);//replacing dots with nothing

else {

 

The part in red is your problem.  You have a closing bracket for your if statement at the top there, then you have an expression (the str_replace), then you have an else.  The else needs to come immediately after the closing bracket.  I'm going to assume that the str_replace is supposed to be inside the 'if' condition so it should be like this:

 

/*Sending Email*/
if ($valNoBot == "ihateubot"){
$to = "[email protected]";
$subject = "New Contact";
$message = "Form1 Information \n\nFirstname = $firstname\r\nLastname = $lastname\r\nphone number = $phone\r\ncell = $cell\r\ne-mail = $email \nmessage = $comments";
$from = "$firstname $lastname-$email";
str_replace('.','',$from);//replacing dots with nothing
} else {

 

 

 

I may.  The code we're all looking at right now was given by dark freaks.  My original code did not have strip tags or any mysql calls.  I have no DB set up on my web site, but my host probably allows it.

 

I'm not sure what Dark freaks was trying to strip with his strip tags as I see no other assignment to 'allowed tags'.  The end result was to remove the periods I was getting in the e-mails sent to me by the form. 

 

Again, being a huge novice at this, I wasn't sure how strip tags was accomplishing it, or why the mysql call would be there either, I was just putting faith in the community here as you've helped a lot of people :)

Now I get this error:

 

Fatal error: Call to undefined function: filter_var() in /sendhappymail.php on line 8

 

And so we're all working on the same page, here's my code now, followed by the last known working code:

 

<?php

function clean($text){
$text=trim($text);
$text.=strip_tags($text, 'allowed tags');
$text.=htmlspecialchars($text,ENT_NOQUOTES);
$text.=mysql_real_escape_string($text);
$text.=filter_var($text,FILTER_SANITIZE_STRING);
}
$firstname = clean($_POST['firstname']);
$lastname = clean($_POST['lastname']);
$phone = $_POST['phone'];
$cell = clean($_POST['cell']);
$email = $_POST['email'];
$comments = clean($_POST['comments']);
$valNoBot = clean($_POST['hiddenField']);

/*Sending Email*/
if ($valNoBot == "ihateubot"){
$to = "[email protected],";
$subject = "New Mail";
$message = "Form1 Information \n\nFirstname = $firstname\r\nLastname = $lastname\r\nphone number = $phone\r\ncell = $cell\r\ne-mail = $email \nmessage = $comments";
$from = "$firstname $lastname-$email";
str_replace('.','',$from);}//replacing dots with nothing 
else {
print "<h3>Go away you stinkin' bot!</h3> \n";
} // end if


if(mail($to, $subject, $message, "From: $from"))
header("location: contactsuccess.html");
else
header("location: contactfailure.html");
?>

 

<?php
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$phone = $_POST['phone'];
$cell = $_POST['cell'];
$email = $_POST['email'];
$comments = $_POST['comments'];
$valNoBot = $_POST['hiddenField'];

/*Sending Email*/
if ($valNoBot == "ihateubot"){
$to = "[email protected]";
$subject = "New Mail";
$message = "Form1 Information \n\nFirstname = $firstname\r\nLastname = $lastname\r\nphone number = $phone\r\ncell = $cell\r\ne-mail = $email \nmessage = $comments";
$from = "$firstname $lastname - $email";}
else {
print "<h3>Go away you stinkin' bot!</h3> \n";
} // end if


if(mail($to, $subject, $message, "From: $from"))
header("location: contactsuccess.html");
else
header("location: contactfailure.html");
?>

if you do not have PHP 5 just remove the filter_var function  :P

 

i would suggest reading up on  strip_tags()this will explain what html tagsare allowed in the second arguement, and thanks for the admin for the correction. also if you do not have aDB connection mysql_real_escape_string()is not needed.

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.