Contact email won't send?

HTML:

[code]

<input type="text" name="email" id="email" size="30" placeholder="Enter your e-mail address." required/>

<textarea  name="message" id="message" placeholder="Type your message here" required></textarea>

<input type="text" name="captcha" placeholder="House pet that says 'meow'" required/>

<input id="submit" type="submit" name="submit" value="Send" />  
[/code]

php:

<?php $to = "username@gmail.com"; $from = $_POST['email']; $name = $_POST['name']; $message = $_POST['message']; $headers = "From: $from"; $subject = "You have a message sent from your site"; $body = "$name from $from says: $message"; mail($to, $subject, $body, $headers); ?>

I get an email in my box, but it says “from says:” which means the variables aren’t making it to the email. Any ideas?

Your form has no action. Where are the form variables supposed to go?

Adding action=“scripts/contactform.php” to my form didn’t prove any results

@Popo - I’ve removed your actual e-mail address from your post. Presumably you don’t want it on the Internet for all to see.

1 Like

I have this

<!doctype html>
<html>
<head>
<title>asdf</title>
</head>
<body>
<form id="contact" name="contact" method="post" action="testing-form-action.php">  
    <input type="text" name="name" id="name" size="30" placeholder="Enter your name." required/>
    <input type="text" name="email" id="email" size="30" placeholder="Enter your e-mail address." required/>

<textarea  name="message" id="message" placeholder="Type your message here" required></textarea>

<input type="text" name="captcha" placeholder="House pet that says 'meow'" required/>

<input id="submit" type="submit" name="submit" value="Send" />
</form>
</body>
</html>

And then this

<?php
    $to = "email@email.com"; 
    $from = $_POST['email']; 
    $name = $_POST['name']; 
    $headers = "From: ".$from; 
    $subject = "You have a message sent from your site"; 
    $body = $name." from ".$from." says: ".$message;
echo $to." to<br>";
echo $from." from<br>";
echo $name." name<br>";
echo $headers." headers<br>";
echo $subject." subject<br>";
echo $body." body<br>";
?>

http://www.codefundamentals.com/test/testing-form.php

Seems to echo out fine.

Hm, still didn’t work. still no variables passing

<?php $to = "easdfadf0@gmail.com"; $from = $_POST['email']; $name = $_POST['name']; $message = $_POST['message']; $subject = "You have a message sent from your site"; $body = $name." from ".$from." says: ".$message; mail($to, $subject, $body); ?>

[code]

<input type="text" name="email" id="email" placeholder="Enter your e-mail address." required/>

<textarea  name="message" id="message" placeholder="Type your message here" required></textarea>

<input type="text" name="captcha"  placeholder="House pet that says 'meow'" required/>

<input id="submit" type="submit" name="submit" value="Send" />  
[/code]

It’s gotta be HTML problem or something? Could this be server issue? The variables have to be working, im getting the email so that works, the title of the email is there, the varaibles from the HTML aren’t getting passed along for whatever reason

I don’t spot an error as to why variables are not displayed… Just wanted to note that the FROM address SHOULD NOT be something a user puts in the form. It should be a hosted email address for the domain, e.g. info@mydomin.com
You can use a reply to header with users email.

I only use the $from so I know who to contact, which is why its a part of $body. I get the email in my inbox from my server, unless I’m misunderstanding you?

I was referring to

$headers = "From: $from";

See https://en.wikipedia.org/wiki/Email_spoofing
Most hosts will not allow sending of email from an address other than the domain.

I know until I whitelisted my shared-host domain my Hotmail email account sent them to the Trash (i.e. SPAM) folder.

But Popo is getting emails, only without “message”

Maybe some kind of obscure naming conflict? i.e. would changing “message” to “form_message” work?

Or maybe newlines from the textarea are being considered as being email delimiters? *
* this is my “best guess” as to what the problem is

Are you sure you have method=“post” on your form tag?
It’s as if POST is not available.

If you add the following to the top of scripts/contactform.php does it show post array?

echo "<pre>";
print_r($_POST);  
echo "</pre>";
exit;

What if you change this line:

$body = $name." from ".$from." says: ".$message;

to this?

$body = "$name from $from says: $message";

Of course, the next step is to add some security!

Your suggestion is how he originally had it in OP, plus it’s better not to quote variables. :wink:
Seems to me that variables are set as empty strings and POST is not available for some reason.

Are you submitting the form with javascript in some way that is not sending post?

Duh, silly me. :blush:

Ah, OK, hadn’t heard that. Any particular reason?

I’m sure it was here in the forum a few years back that I was told (by a BIG DOG) that I shouldn’t use double quotes on strings and variables should not be quoted. Since that time I have changed my writing style so where in most cases I use single quotes and concatenation around variables

header("location: ".$page); 

or

$content = 'This is a ' . $variable . ' that is not in quotes.';
1 Like

That saves PHP having to parse the text looking for variables and is therefore more efficient.

1 Like

Lol, yes, I have it there.I believe the HTML was above unless I am remembering bad

<form id="contact" action="scripts/contactform.php" name="contact" method="post">

edit: I just realized I was replying to the wrong person, but yes, I have post in my method form attribute

If you go to reesecodes.com/scripts/contactform.php you can see the result, it shows Array ( )

here is my javascript. I wasn’t having any issues with it so I didnt even think to look there.

jQuery.validator.addMethod('answercheck', function (value, element) { return this.optional(element) || /^\bcat\b$/.test(value); }, "type the correct answer -_-"); jQuery.validator.addMethod('answercheck', function (value, element) { return this.optional(element) || /^\bcat\b$/.test(value); }, "type the correct answer -_-"); // validate contact form $(function() { $('#contact').validate({ rules: { name: { required: true }, email: { required: true, email: true }, message: { required: true }, answer: { required: true, answercheck: true } }, messages: { name: { required: "Please enter your name." }, email: { required: "Please enter your e-mail." }, message: { required: "Please write a message" }, answer: { required: "Sorry, wrong answer!" } }, submitHandler: function(form) { $(form).ajaxSubmit({ type:"POST", data: $(form).serialize(), url:"scripts/contactform.php", success: function() { $('#contact :input').attr('disabled', 'disabled'); $('#contact').fadeTo( "fast", 0.15, function() { $(this).find(':input').attr('disabled', 'disabled'); $(this).find('label').css('cursor','default'); $('#error').fadeIn(); }); }, error: function() { $('#contact').fadeTo( "fast", 0.15, function() { $('#success').fadeIn(); }); } }); } }); });