Im trying to create a contact form using php,and having some troubles.
the condition: isset($_POST['submit']) always produces false, even if I just submit a blank page.
here is my code:
contact.html part:
<form action='emailto.php' method='POST' enctype='text/plain'>
First Name:<br>
<input type='text' name='firstname'><br>
Last Name:<br>
<input type='text' name='lastname'><br>
Email Address:<br>
<input type='text' name='emailadd' ><br>
Subject:<br>
<input type='text' name='subject' ><br>
Message:<br>
<textarea name='message' rows=5 cols=30 ></textarea><br><br>
<input type='submit' value='Send' name='submit'>
</form>
emailto.php:
<?php
if (isset($_POST['submit'])) {
$to = "emailaddress";
$subject = $_POST['subject'];
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$Emailadd = $_POST['emailadd'];
$Message = $_POST['message'];
$Body= "";
$Body .= $firstname;
$Body .= $lastname;
$Body .= "\n";
$Body .= $Emailadd;
$Body .= "\n";
$Body .= $Message;
mail($to,$subject,$Body);
echo "Mail Sent! <a href='contact.html'>Go Back</a>";
} else {
header("Location: contact.html");
exit(0);
}
?>
Besides, what's weird is that if I delete the if-else statement in emailto.php,
after submitting, an error message will occur: undifined index: subject, firstname, lastname, emailadd, message...
I was totally confused..
Looking forward to hear some advice.
Thanks in advance.
Remove this enctype='text/plain' from your form and it will start working; I guarantee it. And if it fails, then you need to find out why that is and making sure that mail is available for you to use.
Sidenote: You should also check if any of the inputs are empty (and required which helps).
http://php.net/manual/en/function.empty.php
and use proper (full) headers for mail():
http://php.net/manual/en/function.mail.php
Otherwise, your mailout may be treated as spam or rejected altogether.
There should be a valid From: <email> as part of mail's 4th argument.
I.e. and from the manual:
<?php
$to = 'nobody#example.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: webmaster#example.com' . "\r\n" .
'Reply-To: webmaster#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
?>
"Besides, what's weird is that if I delete the if-else statement in emailto.php, after submitting, an error message will occur: undifined index: subject, firstname, lastname, emailadd, message..."
Again; this is caused by enctype='text/plain' in the form which isn't a valid enctype for using the POST array.
Edit:
Add an if/else to mail(). If it echos "Houston we have a problem", then there's a problem on your end.
If it echo "Mail Sent!" but no mail is received, then look at your spam box. Mail has done its job and you need to find out why it was never sent/received.
<?php
if (isset($_POST['submit'])) {
$to = "emailaddress";
$subject = $_POST['subject'];
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$Emailadd = $_POST['emailadd'];
$Message = $_POST['message'];
$Body= "";
$Body .= $firstname;
$Body .= $lastname;
$Body .= "\n";
$Body .= $Emailadd;
$Body .= "\n";
$Body .= $Message;
if(mail($to,$subject,$Body)){
echo "Mail Sent! <a href='contact.html'>Go Back</a>";
} else { echo "Houston, we have a problem"; }
} else {
header("Location: contact.html");
exit(0);
}
?>
Related
I am trying to send a message from HTML form. But for some reason I am not getting anything. Could someone please help me ?
Here is my HTML form:
<form method="post" action="subb.php">
<div class="field half first">
<label for="Name">Name</label>
<input type="text" name="Name" id="name" />
</div>
<div class="field half">
<label for="Email">Email</label>
<input type="text" name="Email" id="email" />
</div>
<div class="field">
<label for="Message">Message</label>
<textarea name="Message" id="message" rows="5"></textarea>
</div>
<ul class="actions">
<button type "submit" name="submit" id="submit" class="button submit">Send message</button>
and the PHP:
<?php
$Name = $_POST['Name'];
$Email = $_POST['Email'];
$Message = $_POST['Message'];
$to = "combatstriker111#gmail.com";
$subject="new message";
mail($to , $subject , $Message, "From :" . $Name . $Email);
echo "Your message has been Sent";
?>
I have named the PHP file subb.php and listed them both in the same directories but its still not working for some reason. Any help is very much appreciated.
Something in your code was wrong mail($to , $subject , $Message, "From :" . $Name . $Email);
Mail function SYNTAX :
mail ( string $to , string $subject , string $message [, string $additional_headers [, string $additional_parameters ]] )
So,
<?php
if(isset($_POST['submit'])) {
$Name = $_POST['Name'];
$Email = $_POST['Email'];
$Message = "Name : ".$Name."<br />"
$Message .= $_POST['Message'];
$to = "combatstriker111#gmail.com";
$subject="new message";
// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Additional headers
$headers .= 'To: Name <$to>' . "\r\n";
$headers .= 'From: $Name <$Email>' . "\r\n";
$headers .= 'Cc: birthdayarchive#example.com' . "\r\n";
$headers .= 'Bcc: birthdaycheck#example.com' . "\r\n";
if(mail($to, $subject, $Message, $headers)) {
echo "Your message has been Sent";
} else {
echo "Mesage Error";
}
}
?>
Note : Use any mail library for prevent vulnerable to header injection like PHPMailer
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];;
$subject = $_POST['subject'];
$message = $_POST['message'];
$to = 'info#fullertoncomputerepairwebdesign.com';
$subject = 'Message From Website';
$headers = 'From: info#fullertoncomputerepairwebdesign.com' . "\r\n" .
'Reply-To: info#fullertoncomputerepairwebdesign.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
$themessage = "Name: ".$name."</br>Email: ".$email."</br>Phone: ".
$phone."</br>Subject: ".$subject.
"</br>Message: ".$message;
//echo $themessage;
if(mail($to, $subject, $themessage, $headers)){
echo "message sent";
header( 'Location: http://www.fullertoncomputerepairwebdesign.com/contactus.php?smresponse=Message Sent' ) ;
}else{
echo "we have a error charlie!";
}
;
test this out and if it doesn't work it means your hosting blocks mail function.
I am trying to create a simple contact form that takes user-inputted data and e-mails it to me. however, when I use the following code and the user presses submit, the page is redirected to a blank page (/form.php) - not even the "Thank you!" shows up on it, nor is the e-mail even sent. Can someone point out any errors I'm making? thanks!
PHP:
<?php
if (isset($_POST['email'])) {
$myEmail = "myemail#gmail.com";
$name = $_POST['name'];
$message = $_POST['message'];
$email = $_POST['email'];
$subject = "Message from $email";
$message = "
Name: $name
E-mail: $email
Message:
$message
";
/* Sends to e-mail. */
mail($myEmail, $subject, $message, "hello");
?>
Thank you!
<?php
}
?>
HTML:
<form id="form" method="post" name="contact-form" action="form.php">
Name: <br>
<input type="text" name="name" /><br><br>
Email:<br>
<input type="text" name="email" /><br><br>
Message:<br>
<textarea name="message" placeholder="Tell me anything!"></textarea><br><br>
<input type="submit" value="Send">
</form>
You're declaring $message with your message (as part of the mail() header) then you're including $message inside it, in turn hashing and bashing your header.
Try this method instead.
<?php
if (isset($_POST['email'])) {
$myEmail = "myemail#gmail.com";
$name = $_POST['name'];
$message = $_POST['message'];
$email = $_POST['email'];
$subject = "Message from $email";
$message2 = "
Name: $name
E-mail: $email
Message:
$message
";
/* Sends to e-mail. */
if(mail($myEmail, $subject, $message2, "hello")){
echo "<b>Thank you</b>"; // HTML bold text.
}
else{
echo "<h2>Sorry, there was an error.</h2>";
}
}
?>
If you want to send email as HTML you need to use the following:
$headers = "From: $email" . "\r\n";
$headers .= 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
then change what is above:
if(mail($myEmail, $subject, $message2, "hello"))
to read as:
if(mail($myEmail, $subject, $message2, $headers))
Fixed 2 problems.
<?php
if (isset($_POST['email'])) {
$myEmail = "myemail#gmail.com";
$name = $_POST['name'];
$message = $_POST['message'];
$email = $_POST['email'];
$subject = "Message from $email";
$message = "
Name: ".$name."
E-mail: ".$email."
Message:
".$_POST['message']."
";
/* Sends to e-mail. */
mail($myEmail, $subject, $message);
?>
Thank you!
<?php
}
?>
I know there is a few of these posts already up, and I read through them, but was unable to find the solution to my problem. The PHP and HTML looks good so I am not exactly sure why I am not receiving any email from the submitted contact form.
Here is the PHP:
<?php
$Fname = $_POST ['Fname'];
$Lname = $_POST ['Lname'];
$email = $_POST ['email'];
$message = $_POST ['message'];
$to = 'myName#mywebsite.com';
$subject = 'Contact From My Website';
$msg = " First Name: $Fname/n" .
"Last Name: $Lname/n" .
"Email: $email/n" .
"Message: $message";
mail ($to, $subject, $msg,"From: " . $Fname . $Lname);
$confirm = "Thank you for contacting us! Please allow 48 hours for a representative to respond. Click <a href='contact.php'>here</a> to return to the previous page.";
?>
And here is the HTML form code:
<form id="form1" name="form1" method="post" action="send.php">
<tr>
<label><td width="160px" class="labels">First Name: </td>
<td class="input"><input type="text" name="Fname" id="Fname"/></td>
</label>
</tr>
<tr>
<label><td class="labels">Last Name: </td>
<td class="input"><input type="text" name="Lname" id="Lname"/></td>
</label>
</tr>
<tr>
<label><td class="labels">Email: </td>
<td class="input"><input type="email" name="email" id="email"/></td>
</label>
</tr>
<tr>
<label><td class="labels">Message: </td>
<td class="input"><textarea name="message" id="message" cols="30" rows="5"></textarea></td>
</label>
</tr>
<tr>
<td class="labels"><input type="submit" name="submit" id="submit" value="Submit"/></td>
</tr>
</form>
Any help would be greatly apreciated
EDIT:
There were many errors.
Improper use of \n, mail() headers and trying to echo a success message at the end.
Here is a tested and working mail handler script.
NOTE: I added an if{isset condition.
Change email#example.com with your own E-mail address.
<?php
if(isset($_POST['submit']))
$Fname = $_POST['Fname'];
$Lname = $_POST['Lname'];
$email = $_POST['email'];
$message = $_POST['message'];
$to = "email#example.com"; // <<< change this to your own E-mail address
$subject = "Contact From My Website";
$msg = "First Name: $Fname\n" . "Last Name: $Lname\n" . "Email: $email\n" . "Message: $message";
$headers = "From: $email" . "\r\n" .
"Reply-To: $email" . "\r\n" .
"X-Mailer: PHP/" . phpversion();
mail($to, $subject, $msg, $headers);
echo "Thank you for contacting us! Please allow 48 hours for a representative to respond. Click <a href='contact.php'>here</a> to return to the previous page.";
?>
Supplement
Please read up on the mail( ) function and its proper use of headers by visiting:
http://php.net/manual/en/function.mail.php
There are plenty of examples on that page.
One major error is your use of /n and all need to be changed to \n if anything.
That alone will ultimately make your handler fail.
"From" should not include the name, instead it should include the from email address.
So, instead of
"From: " . $Fname . $Lname
You should be doing
"From: ". $myEmail
Here's a valid header from the php manual:
$headers = 'From: webmaster#example.com' . "\r\n" .
'Reply-To: webmaster#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
I guess Puggan and Fred get credit for this answer as-well, since they have posted out the obvious but not yet made it an answer.
You should also make sure that the id of the message labels match to $_POST['msg'], so you should not be using 'msg' in one place and 'message' in another.
FYI: skip the spaces ' ' between the function name and the parameter arguments. i.e.
$_POST ['stuff'];
Add
$headers = "From:" . $Fname . $Lname;
and use this:
mail ($to, $subject, $msg, $headers);
It seems everything is in place:
PHP:
<?php
if (!empty($_POST['name'])){
$msg = "name". $_POST['name'];
}else{
$fname = NULL;
echo "Name Required.<br />";
}
if (!empty($_POST['email'])){
$msg = "email". $_POST['email'];
}else{
$lname = NULL;
echo "Email Required.<br />";
}
if (!empty($_POST['www'])){
$msg = "Website". $_POST['www'];
}else{
$lname = NULL;
echo "Website Required.<br />";
}
if (!empty($_POST['comment'])){
$msg = "Comment". $_POST['comment'];
}else{
$email = NULL;
echo "A comment is required.<br />";
}
$recipient = "myemail#gmail.com";
$subject = "Form Feedback";
$mailheaders = "Reply-to". $_POST['email'];
//send the mail
mail($recipient, $subject, $msg, $mailheaders);
?>
HTML:
<div id="contact" style="height:280px; margin:1px 0;">
<form id="contactLP" method="post" action="inc/php/contact_validate.php">
<div class="align"><input type="text" name="name" tabindex="1" /></div>
<div class="align"><input type="text" name="email" tabindex="2" /></div>
<div class="align"><input type="text" name="www" tabindex="3" /></div>
<div class="align"><textarea id="txta" name="comment" cols="15" rows="5" tabindex="4"></textarea></div>
<span style="color:transparent;">test</span>
<br><br>
<div class="align"><input type="submit" class="submit" name="sendForm" id="SubmitContact" value="" tabindex="5" /></div>
</form>
</div><!--CONTACT-->
When I fill it out correctly and submit, it says "Thanks for your message" or something similiar, but then I get nothing in email.
I tried running this both on a server on the internet, along with on my local server running on my workstation.
Am I doing something wrong above???????
Yes, you are "name; $_POST['name'] "; should be "name". $_POST['name']; in every instance you use that string.
Your $msg is only holding the current value.
Try something like this for all your value assignment to $msg variable
$msg .= "Comment". $_POST['comment'];
mail function
You seem to have screwed up the $mailheaders variable slightly (reply-to section), try this code in a stand alone script. If even it fails, you may have to check your mail function and how it is set up on the server. (change the email addresses obviously)
<?php
$to = 'nobody#example.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: webmaster#example.com' . "\r\n" .
'Reply-To: webmaster#example.com';
mail($to, $subject, $message, $headers);
I have a very simple contact form on my site, Im looking for users to be able to just put a check mark next to "CC:" to have it CC them without creating a whole new field for that they have to fill out again.
Here is the HTML:
<form action="send.php" method="post" name="form1">
Name: <input name="name" size="35"><br/>
E-mail:<input name="email" size="35"><br/>
CC: <input input type="checkbox" name="mailcc"><br/>
Comment: <textarea cols="35" name="comment" rows="5"></textarea> <br />
<input name="Submit" type="submit" value="Submit">
</form>
And here is the PHP:
<?php
$name = $_REQUEST['name'] ;
$email = $_REQUEST['email'] ;
$comment = $_REQUEST['comment'] ;
mail( "me#me.com", "Message Title", "Name: $name\n Email: $email\n Comments: $comment\n " );
echo "Message Sent! Thanks!"
?>
Ive been trying to add some items from this site:
http://w3mentor.com/learn/php-mysql-tutorials/php-email/send-email-with-cccarbon-copy-bccblind-carbon-copy/
But it wants to create a text field for CC which means the user would have to enter their email twice.
Ive also tried $mailheader.= "Cc: " . $email ."\n"; but I cant get that to work either.
Make the checkbox have a value (value="1") in HTML.
Add a variable ($mailheader) to the end of mail() function, as the last parameter.
So essentially:
$name = $_POST['name'] ;
$email = $_POST['email'] ;
$comment = $_POST['comment'] ;
if ($_POST['mailcc'] == 1) {
$mailheader .= "CC: $name <$email>";
}
mail("me#me.com", "Message Title", "Name: $name\n Email: $email\n Comments: $comment\n ", $mailheader);
echo "Message Sent! Thanks!";
Is the Cc address you are testing with the same as the "to" address(me#me.com on your example)?
I did a quick test and with this code i get only one mail:
<?php
$to = "my#address.com";
$subject = "Testing";
$message = "Testing message";
$headers = "Cc: my#address.com";
mail($to, $subject, $message, $headers);
But with this i get a copy to my other email account:
<?php
$to = "my#address.com";
$subject = "Testing";
$message = "Testing message";
$headers = "Cc: my#otheraddress.com";
mail($to, $subject, $message, $headers);