So I have built my website and I planned on using a php based contact page to send emails to my email address I made this and I receive an e-mail but no content or summary...
so here is my html:
<form action="Php/sendemail.php" method="POST">
<div id="form-inner">
<input type="text" class="input" name="name" id="name" placeholder="Name">
<input type="email" class="input" name="email" id="email" placeholder="E-mail">
<textarea class="input textarea" name="message" id="message" placeholder="Your message here"></textarea>
<input type="submit" class="button" value="Send message" id="button">
</div>
</form>
and here is my php:
$subject = "Contact form submitted!";
$to = 'email#example.com';
$body = <<<HTML
$message
HTML;
$headers = "From: $email\r\n";
$headers .= "Content-type: text/html\r\n";
mail($to, $subject, $message, $headers);
header('Location: /Contact.php');
?>
I really do not understand why this is happening if you can please help ! Thnaks
This is because you are not geeting the value from the $_POST superglobal:
$body = <<<HTML
$message
HTML;
should be:
$message = $_POST['message'];
$body = <<<HTML
$message
HTML;
Of course there are other ways to do this like just putting $_POST['message']; in the message body but this illustrates how to get the submitted form values.
(You will also want to do things like clean up (i.e. trim(), etc) the submitted values and also defend against header injections as well).
Related
First of all I am wondering if there is a way to make it so the page don't refresh on form submission but the boxes etc. are still all cleared.
But my main issue is, once I submit a form it sends an email. BUT.... now every time I refresh the page or go back on the website it sends the email again. I can also click the submit several times. Any help is greatly appreciated.
Code
<?php
if(isset($_POST['SubmitButton'])){
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$formcontent="From: $name \n Message: $message";
$recipient = "reviews#itssimplytech.co.uk";
$subject = "Simply Tech Contact Form";
$headers = "From: $email \r\n".
"Reply-To: $.email \r\n".
'X-Mailer: PHP/' . phpversion();
mail($recipient, $subject, $formcontent, $headers) or die("Error!");
}
?>
<form class="contact-form" action="" method="post">
<input type="text" name="name" placeholder="Name" required>
<input type="email" name="email" placeholder="Email" required>
<textarea rows="5" name="message" placeholder="Say Something..." required></textarea>
<input type="submit" name="SubmitButton" value="Submit">
</form> <!-- /.contact-form -->
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
So I am trying to make a contact form for my website but i keep getting the internal server error 500. If anyone could look through the code and tell me if I am doing anything wrong that would be great. Thanks!
HTML name is index.html:
<form method="post" action="send_contact.php">
<p class="full-row">
<label for="name">Your Name:</label>
<input type="text" id="name" name="name">
</p>
<p class="full-row">
<label for="email">Email:</label>
<input type="text" id="email" name="email">
</p>
<p class="full-row">
<label for="subject">Subject:</label>
<input type="text" id="subject" name="subject">
</p>
<p class="full-row">
<label for="message">Message:</label>
<textarea name="message" id="message" rows="6"></textarea>
</p>
<input id="submit" class="mainBtn" type="submit" name="send" value="Send Message" onClick="">
</form>
PHP name is send_contact.php:
<?php
// Contact subject
$subject ="$subject";
// Details
$message="$detail";
// Mail of sender
$mail_from="$user_mail";
// From
$header="from: $name <$mail_from>";
// Enter your email address
$to ="example#example.com";
$link = "<script>window.open('confirm.html')</script>";
$send_contact=mail($to,$subject,$message,$header);
// Check, if message sent to your email
// display message "We've recived your information"
if($send_contact){
header('confirm.html');
}
else {
echo "ERROR";
}
?>
To get variable values in PHP, use $_POST['variableName']
for example to fetch this:
<input type="text" name="email">
You will need this:
$email= $_POST['email'];
<?php
// Contact subject
$subject = $_POST['subject'];
// Details
$message=$_POST['message'];
// Mail of sender
$mail_from=$_POST['email'];
// From
$header="from: ".$_POST['name']." <".$_POST['email'].">";
// Enter your email address
$to ="example#example.com";
$send_contact=mail($to,$subject,$message,$header);
// Check, if message sent to your email
// display message "We've recived your information"
if($send_contact){
header('Location: confirm.html');
}
else {
echo "ERROR";
}
?>
Also here are additional headers to help your email going in the inbox rather than the junk box:
$to = "webtest#website.com";
$subject = "Test Email";
$message = "Test Email";
// normal headers
$num = md5(time());
$headers = "From: Me <me#website.com>\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: multipart/mixed; ";
$headers .= "boundary=".$num."\r\n";
$headers .= "--$num\r\n";
// This two steps to help avoid spam
$headers .= "Message-ID: <".time()." TheSystem#".$_SERVER['SERVER_NAME'].">\r\n";
$headers .= "X-Mailer: PHP v".phpversion()."\r\n";
// With message
$headers .= "Content-Type: text/html; charset=UTF-8\r\n";
$headers .= "Content-Transfer-Encoding: 8bit\r\n";
$headers .= "".$message."\n";
$headers .= "--".$num."\n";
#mail($to, $subject, $message, $headers);
Cheers!
You are sending the form with post so in php you can access it like this $name = $_POST['name'].
You can access the data sent through the associative array [$_POST][1], like so:
$subject = $_POST["subject"];
HTML:
<form method="post" action="send_contact.php">
<p class="full-row">
<label for="name">Your Name:</label>
<input type="text" id="name" name="name"/>
</p>
<p class="full-row">
<label for="email">Email:</label>
<input type="email" id="email" name="email"/>
</p>
<p class="full-row">
<label for="subject">Subject:</label>
<input type="text" id="subject" name="subject"/>
</p>
<p class="full-row">
<label for="message">Message:</label>
<input type="text" name="message" id="message"/>
</p>
<input id="submit" class="mainBtn" type="submit" name="send" value="Send Message"/>
</form>
PHP:
//Check if the values requested were provided
if (isset($_POST["name"]) && isset($_POST["email"]) && isset($_POST["subject"]) && isset($_POST["message"])) {
$name = $_POST["name"];
$email = $_POST["email"];
$subject = $_POST["subject"];
$message = $_POST["message"];
$header = "from: $name <$email>";
$to = "example#example.com";
if (mail($to, $subject, $message, $header)) {
header("Location: confirm.html");
} else {
echo("Error");
}
}
?>
As others have suggested, check your error logs if you're still running in to trouble.
The modal form below gets stuck on submitting.... It also doesn't actually send the message to my email. I am relatively new to PHP, but I assume this problem pertains to my sendmessage.php file(below). For whatever reason, the tutorial states to not apply an action. What coud I be doing wrong?
Just FYI, my header contains scripts to jquery.fancybox.js?v=2.0.6 and ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js
sendmessage.php file:
<?php
$sendto = "jjaylad#me.com";
$usermail = $_POST['email'];
$content = nl2br($_POST['msg']);
$subject = "New Feedback Message";
$headers = "From: " . strip_tags($usermail) . "\r\n";
$headers .= "Reply-To: ". strip_tags($usermail) . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html;charset=utf-8 \r\n";
$msg = "<html><body style='font-family:Arial,sans-serif;'>";
$msg .= "<h2 style='font-weight:bold;border-bottom:1px dotted #ccc;'>New User Feedback</h2>\r\n";
$msg .= "<p><strong>Sent by:</strong> ".$usermail."</p>\r\n";
$msg .= "<p><strong>Message:</strong> ".$content."</p>\r\n";
$msg .= "</body></html>";
if(#mail($sendto, $subject, $msg, $headers)) {
echo "true";
} else {
echo "false";
}
?>
Contact form code:
<form id="contact" name="contact" action="#" method="post">
<label for="email">Your E-mail Address:</label>
<input type="email" id="email" name="email" class="txt">
<br>
<label for="msg">Inquiry:</label>
<textarea id="msg" name="msg" class="txtarea"></textarea>
<button id="send">Send E-mail</button>
</form>
The form action should point to sendmessage.php
<form id="contact" name="contact" action="sendmessage.php" method="post">
<label for="email">Your E-mail Address:</label>
<input type="email" id="email" name="email" class="txt">
<br>
<label for="msg">Inquiry:</label>
<textarea id="msg" name="msg" class="txtarea"></textarea>
<button id="send">Send E-mail</button>
</form>
I think you may be missing a file.
I get a 404 error when submitting. http://www.jjayladslair.com/sendmessage.php does not exist.
Double check the filenames?
*edit:
Just noticed that you are in fact using $.ajax to process the form. It's most definitely a case of a missing file, or incorrect filename. The 404 error causes a jQuery exception which then stops the execution of the Javascript function.
That's why the form gets stuck on submit.
I'm not receiving mails on the email mail#example.com. Below is my form code and my send-mail.php code. Can anyone help me with this cause everything seems working great bu i'm not receiving any emails. I'm using localhost as the server.
Contact form:
<form id="contactForm" action="#" method="post">
<p>Email us by filling in the form below. Make sure you fill in the message and all fields.</p>
<fieldset>
<div>
<input name="name" id="name" type="text" class="form-poshytip" title="Enter your name" />
<label>Name</label>
</div>
<div>
<input name="web" id="web" type="text" class="form-poshytip" title="Enter your surname" />
<label>Surname</label>
</div>
<div>
<input name="email" id="email" type="text" class="form-poshytip" title="Enter your email address" />
<label>Email</label>
</div>
<div>
<textarea name="comments" id="comments" rows="5" cols="20" class="form-poshytip" title="Enter your comments"></textarea>
</div>
<!-- send mail configuration -->
<input type="hidden" value="mail#example.com" name="to" id="to" />
<input type="hidden" value="Enter the subject here" name="subject" id="subject" />
<input type="hidden" value="send-mail.php" name="sendMailUrl" id="sendMailUrl" />
<!-- ENDS send mail configuration -->
<p><input type="button" value="Send" name="submit" id="submit" /> <span id="error" class="warning">Message</span></p>
</fieldset>
</form>
<p id="sent-form-msg" class="success">Form data sent. Thanks for your feedback.</p>
<!-- ENDS form -->
and here is the send-mail.php
<?php
//vars
$subject = $_POST['subject'];
$to = explode(',', $_POST['to'] );
$from = $_POST['mail#example.com'];
//data
$msg = "NAME: " .$_POST['name'] ."<br>\n";
$msg .= "EMAIL: " .$_POST['email'] ."<br>\n";
$msg .= "WEBSITE: " .$_POST['web'] ."<br>\n";
$msg .= "COMMENTS: " .$_POST['comments'] ."<br>\n";
//Headers
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=UTF-8\r\n";
$headers .= "From: <".$from. ">" ;
//send for each mail
foreach($to as $mail){
mail($mail, $subject, $msg, $headers);
}
?>
$_POST['subject'];
$_POST['to'];
$_POST['myemail#gmail.com'];
$_POST['name'];
$_POST['email'];
$_POST['web'];
$_POST['comments'];
I didn’t find any of these elements in your form. That's the reason why nothing is happening.Try
echo '<pre>';
print_r($_POST);
This will give you the posted array when the form is submitted.
i have some suggestion.if u have kept the 'to' address as hidden in the form then why cant u try keeping it directly in sendmail function and in $from you try to keep
<?php
$to="kurtfarrugia92#gmail.com";
$from =$_POST['field_name'];
// not the mail id because i didn't see any field with name as "kurtfarrugia92#gmail.com"
?>
You cannot use this function to send mail from localhost. I am not sure but you should try PHP mailer for this task.
Can you provide the proper syntax to pass the html form variables to php mailer?
The php is:
$comment = $_POST['comment'];
$email = $_POST['email'];
$to = 'sssff#gmail.com';
$subject = 'From Website';
$message = $comment;
$headers = 'From: $email';
mail($to, $subject, $message, $headers);
$message needs to contain the contents of $comment
$headers needs to display the contents of $email as return address
Can anyone help me with the proper syntax? Thank you
EDIT
To clarify, the email i receive from the php mailer does not contain the from address contained by $email, nor does the message contain the comments from $comment.
The email is sent fine, but does not contain those crucial elements.
If you'd like to look at the form it is:
<form class="cmxform" id="commentForm" method="POST" action="">
<label for="cname">Name</label>
<input id="cname" type="text" name="name" size="60" class="required" minlength="2" />
<label for="cemail">E-Mail</label>
<input id="cemail" type="text" name="email" size="60" class="required email" />
<label for="curl">URL</label>
<input id="curl" type="text" name="url" size="60" class="url" value="" />
<label for="ccomment">Your comment</label>
<textarea id="ccomment" type="text" name="comment" cols="72" rows="8" class="required"></textarea>
<div id="button2"><input class="submit" id="submit_btn" type="submit" value="Send Email"/></div>
</form>
Thank you for your help
$headers = 'From: $email';
^--- ^---
should be " instead. Single quoted strings do not interpolate variables, so you're using a literal $email as your From address, not someone#example.com.
$comment = $_POST['comment'];
$email = $_POST['email'];
$to = "$email";
$from = 'sender email';
$subject = 'message subject';
//Begin HTML Email Message
$message = "$comment";
//end of message
$headers = "From: $from\r\n";
$headers .= "Content-type: text\r\n";
mail($to, $subject, $message, $headers);