Simple HTML/PHP Contact Form Not Working - php

I am trying to create simple HTML/PHP Contact form, But this is not working correctly. Pl check this below given code that's I'm actually trying.
My simple form process
HTML form,
<form action="send_mail.php" method="post" onsubmit="return validateForm();" name="myform" >
<input type="hidden" name="recipient" value="email">
<input type="hidden" name="subject" value="FormMail E-Mail">`
<table cellspacing="2" cellpadding="2" border="0">
<tr>
<td align="left">Nome:
<input type="text" name="name" id="name" size="50"></td>
</tr>
<tr>
<td align="left">Email:
<input type="text" name="email" id="email" size="50"></td>
</tr>
<tr>
<td align="left">Mensagem</td></tr>
<tr>
<td><textarea style="resize:none;" cols="44" rows="8" name="message" id="message"></textarea></td>
</tr>
<tr>
<td align="left">
<input name="submit" type="submit" value="Aceder"><input type="reset" value="Recompor">
<input type="hidden" name="redirect" value="thankyou.html"></td>
</tr>
</table>
</form>
PHP Code
if(isset($_POST['submit']))
{
$to = "xyz#abc.com";
$subject = "Email from Xyz company";
$name_field = $_POST['name'];
$email_field = $_POST['email'];
$message = $_POST['message'];
$body = "From: $name_field\n E-Mail: $email_field\n Message:\n $message";
mail($to, $subject, $body);
}
else
{
echo "Failure!";
}
?>
I am using this code, plz suggest me if there is any error in my code and any changes required..
Thank you.

quotation marks end on subject?
if(isset($_POST['submit']))
{
$to = "xyz#abc.com";
$subject = "Email from Xyz company; // quotation marks end?
$name_field = $_POST['name'];
$email_field = $_POST['email'];
$message = $_POST['message'];
$body = "From: $name_field\n E-Mail: $email_field\n Message:\n $message";
mail($to, $subject, $body);
}
else
{
echo "Failure!";
}
?>

In your PHP Code on the line 4 the quotation marks are missing at the end of the line before ;.
Correct line 4:
$subject = "Email from Xyz company";

Change above PHP code to this;
<?php
if(isset($_POST['submit']))
{
$to = "xyz#abc.com";
$subject = "Email from Xyz company";
$name_field = $_POST['name'];
$email_field = $_POST['email'];
$message = $_POST['message'];
$body = "From: $name_field\n E-Mail: $email_field\n Message:\n $message";
mail($to, $subject, $body);
}
else
{
echo "Failure!";
}
?>

Try this
<?php
if(isset($_POST['submit']))
{
$to = "xyz#abc.com";
$subject = "Email from Xyz company";
$name_field = $_POST['name'];
$email_field = $_POST['email'];
$message = $_POST['message'];
$body = "From: $name_field\n E-Mail: $email_field\n Message:\n $message";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=utf8' . "\r\n";
if( !mail($to, $subject, $body, $headers) )
{
echo "Failure!";
}
}
?>

Related

Why can't I see the email address from contact form?

I receive an email but it will read from unknown. Everything else seems to work. Here is a link to the page link I don't know much about php.
<?php
if (isset($_POST['submit'])) {
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$mailTo = "contact#podmtg.com";
$headers = "From: ".$mailFrom;
$txt = "You have recieved an email from ".$name.".\n\n".$message;
mail($mailTo, $subject, $txt, $headers);
header("Location: contact.html?mailsend");
}
?>
<form action="contactform.php" method="post" onsubmit="return confirm('Are you sure you want to submit this form?');">
<input type="text" name="name" placeholder="Name" required="required">
<input type="text" name="email" placeholder="Email" required="required">
<textarea name="message" placeholder="Write message here..." required="required"></textarea>
<button type="submit" name="submit">SUBMIT</button>
</form>
In this you have mention this `$headers = "From: ".$mailFrom; but not $mailFrom found. Use $email in place of $mailFrom.
I changed the code. You guys were right. Thank you!
<?php
if (isset($_POST['submit'])) {
$name = $_POST['name'];
$mailFrom = $_POST['email'];
$message = $_POST['message'];
$mailTo = "contact#podmtg.com";
$headers = "From: ".$mailFrom;
$txt = "You have recieved an email from ".$name.".\n\n".$message;
mail($mailTo, $headers, $txt);
header("Location: index.html?mailsend");
}
?>

How will I write php code for this code?

I'm struggling a little to create PHP for this HTML code. I'm more of a front end designer than back end any help? here is what have it should be basic yet I have tried myself to get it to work seem to make it worse each time...
HTML CODE
<form action="form.php" method="post" enctype="multipart/form-data">
<label></label>
<input name="name" required placeholder="Your Name">
<label></label>
<input name="email" type="email" required placeholder="Your Email">
<label></label>
<input name="tel" type="tel" placeholder="Your Contact Number">
<label></label>
<select name="treatment">
<option value="">Select...</option>
<option value="">Anti-Wrinkle Injections</option>
<option value="">Dermal Fillers</option>
<option value="">The Vampire Facelift</option>
<option value="">Other Treatments</option>
</select>
<label></label>
<textarea name="message" cols="20" rows="5" required placeholder="Message"></textarea>
<input id="submit" name="submit" type="submit" value="Submit">
</form>
PHP CODE
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: website';
$to = 'email';
$subject = 'Email Inquiry';
$body = "From: {$name}\n E-Mail: {$email}\n Message:\n {$message}";
if ($_POST['submit']) {
if (mail ($to, $subject, $body, $from)) {
echo '<p>Thank you for your email! You will now be redirected to the Home Page.</p>';
} else {
echo '<p>Oops! An error occurred. Try sending your message again.</p>';
}
}
?>
This is what the PHP should be:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: website';
$to = 'an email address';
$subject = 'Email Inquiry';
$body = "From: $name\n E-Mail: $email\n Message:\n $message";?>
Its a ? not a / at the end
I just tested the code, using this php, and it works correctly.
Try This.
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: website';
$to = 'an email address';
$subject = 'Email Inquiry';
$body = "From: ".$name."<br/>E-Mail: ".$email."<br/> Message:<br/>". $message;
?>
You can use the braces style.
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: website';
$to = 'an email address';
$subject = 'Email Inquiry';
$body = "From: {$name}\n E-Mail: {$email}\n Message:\n {$message}";
I hope you also have in mind the safety xss attacks. If not, I suggest a small tutorial like this.

Processing a form on the same page

So, I've been messing with this for a day now and I've read a lot tutorials and even posts on stackoverflow. I know it's possible to have all your php form processing steps on the same page as your form, but for some reason, my form won't send an email. Any help?
<div id="mc_embed_signup">
<form method="post" action="index.php" enctype="text/plain" name="emailform">
<input type="text" name="name" placeholder="First Name or Alias"><br>
<input type="text" name="email" placeholder="Email Address"><br>
<textarea name="message" rows="10" size="50" placeholder="Placeholder Text."></textarea><br>
<input type="submit" value="Submit →" name="submit" class="button round">
</form>
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = $_POST['email'];
$to = 'email#address.com'; //set to the default email address
$subject = 'Hello';
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
$headers = "From: $email" . "\r\n" .
"Reply-To: $email" . "\r\n" .
"X-Mailer: PHP/" . phpversion();
if($_POST['submit']) {
mail ($to, $subject, $body, $headers); //mail sends it to the SMTP server side which sends the email
echo "<p>Your message has been sent!</p>";
}
else {
echo "<p>Something went wrong, go back and try again!</p>";
}
?>
</div>
Remove the enctype="text/plain" that's a main factor.
Plus, you also need to check if fields were left empty using (if)empty().
Not doing so, you may receive emails that either do not contain the user's email, name, message or a combination of all.
Assuming this file is called index.php if not, use action=""
<div id="mc_embed_signup">
<form method="post" action="index.php" name="emailform">
<input type="text" name="name" placeholder="First Name or Alias"><br>
<input type="text" name="email" placeholder="Email Address"><br>
<textarea name="message" rows="10" size="50" placeholder="Placeholder Text."></textarea><br>
<input type="submit" value="Submit →" name="submit" class="button round">
</form>
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = $_POST['email'];
$to = 'email#example.com'; //set to the default email address
$subject = 'Hello';
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
$headers = "From: $email" . "\r\n" .
"Reply-To: $email" . "\r\n" .
"X-Mailer: PHP/" . phpversion();
if(isset($_POST['submit']) && !empty($_POST['name']) && !empty($_POST['email']) && !empty($_POST['message']) ){
// check if mail was sent
if(mail ($to, $subject, $body, $headers)){ //mail sends it to the SMTP server side which sends the email
echo "<p>Your message has been sent!</p>";
}
else {
echo "<p>Mail was not sent. Check your logs.</p>";
}
}
else {
echo "<p>Something went wrong, go back and try again!</p>";
}
?>
</div>
This might help,
if(isset($_POST['submit'])) {
if(empty($_POST[name]) && empty($_POST[email]) && empty($_POST[message])){
echo 'Fields cannot be empty';
} else{
$name = $_POST['name'];
$to = $_POST['email'];
$subject = "Hello";
$message = $_POST['message'];
$from = $_POST['email'];
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
mail ($to, $subject, $body);
}
}else{
echo "<p>Something went wrong, go back and try again!</p>";
}

PHP Contact form won't work

I've tried every php contact form tutorial but I can't get any of them to send the email properly. They all just show the code but don't send the email. This is the HTML code I have right now:
<form method="POST" action="scripts/contact.php">
<input id="name" name="name" placeholder="Name*" type="text" /> <br>
<input id="subject" name="subject" placeholder="Subject" type="text" /> <br>
<input id="email_address" name="email" placeholder="Email address*" type="text" /> <br>
<textarea id="message" name="message" placeholder="Your message*"></textarea>
<input id="submit" type="submit" name="submit" value="Send" />
</form>
and this is the php I have:
<?php
$name = $_POST['name'];
$subject = $_POST['subject'];
$email = $_POST['email'];
$message = $_POST['message'];
$to = "benmuschol#gmail.com";
$subject = "Hello World";
$message = "Name: $name \n Subject: $subject \n Email: $email \n Message: $message \n";
$headers = "From: $email" . $email;
mail($name,$email,$subject,$message,$headers);
echo "Mail Sent.";
?>
I have decent knowledge of HTMl but next to none of PHP. Any help would be greatly appreciated.
first you be sure that php file is exist in path scripts/contact.php
second look at phpinfo or php.ini for find is smptp server was setting or not
use following php code:
<?php
$name = $_POST['name'];
$subject = $_POST['subject'];
$email = $_POST['email'];
$message = $_POST['message'];
$to = "benmuschol#gmail.com";
$subject = "Hello World";
$message = "Name: $name \n Subject: $subject \n Email: $email \n Message: $message \n";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Additional headers
$headers .= 'From: '.$email. "\r\n";
if(mail($to, $subject, $message, $headers))
{
echo "Mail Sent Successfully.";
}
else
{
echo "Error in Mail Sent.";
}
?>

Contact form is not working?

I'm trying to create a simple contact page however when I submit the form, it leads me to the blank page contact-form.php. I used this article as reference: http://tangledindesign.com/how-to-create-a-contact-form-using-html5-css3-and-php/
What am I doing wrong?
Code on index.php (corner divs are purely for styling purposes)
<form action="contact-form.php" method="post" autocomplete="off" id="contact">
<div class="corner"></div>
<input type="text" required name="name" placeholder="Name" value="">
<div class="corner"></div>
<input type="text" required name="email" placeholder="Email" value="">
<div class="corner"></div>
<input type="text" required name="check" placeholder="Question of the day: What's 2 + 2 ?" value="">
<div class="corner"></div>
<textarea name="message" rows="25" cols="50" placeholder="Drop me a line!"></textarea>
<button class="send" type="submit" name="submit">Send</button>
</form>
Code on contact-form.php
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: MessageAnita';
$to = 'myemail#gmail.com';
$subject = 'Hello';
$check = $_POST['check'];
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
if ($_POST['submit'] && $check == '4') {
if (mail ($to, $subject, $body, $from)) {
echo '<p>Your message has been sent!</p>';
} else {
echo '<p>Something went wrong, go back and try again!</p>';
}
} else if ($_POST['submit'] && $check != '4') {
echo '<p>You answered the anti-spam question incorrectly!</p>';
}
?>
change the button tag to an input tag
< input type ="submit" name="submit"/>
In your form, change:
<button class="send" type="submit" name="submit">Send</button>
to:
<input type="submit" name="submit" value="Submit">
Plus, you may want to use this PHP mailing method, since the mail came into my SPAM when testing.
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = $_POST['email'];
$to = 'myemail#gmail.com';
$subject = 'Hello';
$check = $_POST['check'];
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
$headers = "From: $from" . "\r\n" .
"Reply-To: $from" . "\r\n";
if ($_POST['submit'] && $check == '4') {
if (mail ($to, $subject, $body, $headers)) {
echo '<p>Your message has been sent!</p>';
} else {
echo '<p>Something went wrong, go back and try again!</p>';
}
} else if ($_POST['submit'] && $check != '4') {
echo '<p>You answered the anti-spam question incorrectly!</p>';
}
?>
And if you want to send as HTML use:
$headers = "MIME-Version: 1.0\n";
$headers .= "Content-Type: text/html; charset=iso-8859-1\n";
$headers .= "From: $from" . "\r\n" .
"Reply-To: $from" . "\r\n";

Categories