Hello I have made a contact form on my website but even if the user doesn't type in any of the details and presses the submit button it sends me a blank email with nothing in it from Unknown Sender.
Anyone know why this is happening? I have added form validation so it shouldn't be sending anything.
HTML Code:
<form action="mail.php" method="POST">
<font color="red">*</font> Name <input type="text" name="name" required>
<font color="red">*</font> Phone <input type="text" name="phone" required>
<font color="red">*</font> Email <input type="text" name="email" required>
<font color="red">*</font> Message <input type="text" name="message" placeholder="I am looking for..." required><br />
<input type="image" src="images/Landing_Pages/submit.png" border="0" alt="Submit" />
</form>
PHP Code:
<?php $name = $_POST['name'];
$phone = $_POST['phone'];
$preferred = $_POST['preferred'];
$email = $_POST['email'];
$formcontent="From: $name \n Phone: $phone \n Email: $email \n Message: $message \n Preferred Contact: $preferred \n Email: $email";
$recipient = "bwebb#webbmaster.com.au";
$subject = "New Request Southbank";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
?>
You have to check if post data is not empty
if (!empty($name) && !empty($phone) && !empty($preferred) && !empty($email)) {
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
}
Edit
<?php
$name = $_POST['name'];
$phone = $_POST['phone'];
$message = $_POST['message'];
$preferred = $_POST['preferred'];
$email = $_POST['email'];
$formcontent="From: $name \n Phone: $phone \n Email: $email \n Message: $message \n Preferred Contact: $preferred \n Email: $email";
$recipient = "bwebb#webbmaster.com.au";
$subject = "New Request Southbank";
$mailheader = "From: $email \r\n";
if (!empty($name) && !empty($phone) && !empty($message) && !empty($preferred) && !empty($email)) {
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
}
?>
Do something like :
$errorflag = 0 ;
if(empty($name)){$errorflag = 1 ;$error = "Input your Name plz!"; };
if(empty($phone)){$errorflag = 1 ;$error = "Input phone"; }
...
if(!$errorflag) {
#mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
}
check every input you need and then send
Related
So I have used the code:
<form action="../scripts/mail.php" method="POST">
<p>Name</p> <input type="text" name="name">
<p>Email</p> <input type="text" name="email">
<p>Message</p><textarea name="message" rows="6" cols="25"></textarea><br />
<input type="submit" value="Send"><input type="reset" value="Clear">
</form>
and the php file linked is
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$formcontent=" From: $name \n Phone: $phone Message: $message";
$recipient = "email#live.com";
$subject = "Contact Form";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo "Thank You!" . " -" . "<a href='form.html' style='text-decoration:none;color:#ff0099;'> Return Home</a>";
?>
The error when testing is: {"code":"MethodNotAllowedError","message":"POST is not allowed"}
What am I doing wrong?
There is no $phone variable defined yet you're trying to use it. Also see this in relation to Adobe products: https://community.adobe.com/t5/dreamweaver/quot-code-quot-quot-methodnotallowederror-quot-quot-message-quot-quot-post-is-not-allowed-quot-php/td-p/8967727?page=1
Try this, just to be double paranoid:
<form method="post" action="../scripts/mail.php">
<p>Name</p> <input type="text" name="name">
<p>Email</p> <input type="text" name="email">
<p>Message</p><textarea name="message" rows="6" cols="25"></textarea><br />
<input type="submit" value="Send"><input type="reset" value="Clear">
</form>
mail.php :
if(isset($_POST['submit'])) {
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$formcontent=" From: $name \n Email: $email Message: $message";
$recipient = "email#live.com";
$subject = "Contact Form";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo "Thank You!" . " -" . "<a href='form.html' style='text-decoration:none;color:#ff0099;'> Return Home</a>";
}
Is it a webpage or website?
If it is a web page you can install xampp(link - https://www.apachefriends.org/download.html). you have to move your file into "xampp/htdocs/" and then open "xampp control panel", click the start button in apache. Then go to your web browser and type as "localhost/yourfile.php/".Then the error message will not show.
if you want to send email watch this video -https://www.youtube.com/watch?v=KA2UB3pxEtg&t=26s
I've found some similar questions to my query, but none that fully match my situation.
I had a basic mail() PHP setup for use on the contact form on my website but, turns out this is blocked on my hosting server to avoid spam/abuse.
The web host recommends using SMTP as a workaround, but the tutorial they link to doesn't seem to provide a working solution for me!
This is my HTML:
<form action="php/mail.php" method="POST">
<p class="form-field-title">Name</p>
<input class="contact-field focus-hide" type="text" id="name" name="name" required autocomplete="off">
<p class="form-field-title">Email Address</p>
<input class="contact-field focus-hide" type="email" id="email" name="email" required autocomplete="off">
<p class="form-field-title">Phone</p>
<input class="contact-field focus-hide" type="text" id="number" name="number" autocomplete="off">
<p class="form-field-title">Message</p>
<textarea class="contact-field focus-hide" id="message" name="message" data-gramm_editor="false" required autocomplete="off"></textarea>
<input class="contact-button" type="submit" value="Send Message">
</form>
And this is my original (non-SMTP) mail.php code:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$number = $_POST['number'];
$message = $_POST['message'];
$formcontent="Name: $name \nEmail: $email \nPhone Number: $number \nMessage: $message";
$recipient = "email#email.com";
$subject = "Website Contact Form Enquiry";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
header('Location: ../thanks.html');
exit();
?>
I have the email server details for my web hosting, but none of the SMTP solutions I've found are working for me!
Is anyone able to help me? I've seen a few things about using PEAR, but their website doesn't seem to be working...?
Thanks!
If your form is in an email, I think you can't post data (maybe I'm wrong). But you can send it to your script with method get.
<form action="php/mail.php" method="GET">
</form>
Then update your php :
<?php
$name = $_GET['name'];
$email = $_GET['email'];
$number = $_GET['number'];
$message = $_GET['message'];
$formcontent="Name: $name \nEmail: $email \nPhone Number: $number \nMessage: $message";
$recipient = "email#email.com";
$subject = "Website Contact Form Enquiry";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
header('Location: ../thanks.html');
exit();
?>
It should do the job.
My form is sending a blank email (field data empty) to me every time i visit my site or refresh it.
I had a look at a few posts mentioning 'Post/Redirect/Get', but that confused me more than helped, and im not sure if it pertains to my exact issue.
This was a basic responsive form i found online and had to find the PHP to actually send the form data. (Changing it to suit my needs)
I'm hoping there's just an error i've missed somewhere but i cant seem to find it.
Any help would be greatly appreciated. Thanks in advance.
Here is the basic form (minus css):
PHP:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];
$formcontent=" From: $name \n Phone: $phone \n Email: $email \n Message: $message";
$recipient = "MY EMAIL HERE";
$subject = "Portfolio Contact Form";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo "Thank You!";
?>
HTML:
<form id="contact-form" action="/" method="post">
<h3>Say Hello!</h3>
<h4>Fill in the form below, and I'll get back to you as soon as i can. Thanks!</h4>
<div>
<label>
<span>Name: (required)</span>
<input placeholder="Please enter your name" type="text" name="name" tabindex="1" required autofocus>
</label>
</div>
<div>
<label>
<span>Email: (required)</span>
<input placeholder="Please enter your email address" type="text" name="email" tabindex="2" required>
</label>
</div>
<div>
<label>
<span>Telephone: (required)</span>
<input placeholder="Please enter your number" type="text" name="phone" tabindex="3" required>
</label>
</div>
<div>
<label>
<span>Message: (required)</span>
<textarea placeholder="Include all the details you can" type="text" name="message" tabindex="5" required></textarea>
</label>
</div>
<div>
<button name="submit" type="submit" id="contact-submit">Send Email</button>
</div>
</form>
use isset
<?php
if(isset($_POST['submit']))
{
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];
$formcontent=" From: $name \n Phone: $phone \n Email: $email \n Message: $message";
$recipient = "MY EMAIL HERE";
$subject = "Portfolio Contact Form";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo "Thank You!";
}
?>
it will post your email only when it submitted by any ..
i have post this one for other in stackoverflow link code link
this is my code.let try , i wish it will help you
<?PHP
$email = $_POST["emailaddress"];
$to = "you#youremail.com";
$subject = "New Email Address for Mailing List";
$headers = "From: $email\n";
$message = "A visitor to your site has sent the following email address to be added to your mailing list.\n
Email Address: $email";
$user = "$email";
$usersubject = "Thank You";
$userheaders = "From: you#youremailaddress.com\n";
$usermessage = "Thank you for subscribing to our mailing list.";
mail($to,$subject,$message,$headers);
mail($user,$usersubject,$usermessage,$userheaders);
?>
I am trying to add php to my contact us page to make sure the message is at least a few characters long for it to be sent. I have tried if (strlen()> 2) but it is just letting messages go through anyway.enter code here
<div id="phillya11">
<a href="#" style="text-decoration:none;color:#FFF5ee">
Contact us
</a>
</div>
<div id="soon4">
<form action="index.php" method="POST">
<p>Name</p> <input type="text" name="name">
<p>Email</p> <input type="text" name="email">
<p>Message</p>
<textarea style="width:475px; height:175px;margin-left:7%" name="message"></textarea><br />
<input type="submit" value="Send"><input type="reset" value="Clear">
</form>
</div>
PHP:
<?php
if (strlen($message)> 2)
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$formcontent="From: $name \n Message: $message";
$recipient = "myemail";
$subject = "Contact Form";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo "Thank You!";
?>
jQuery:
$("#phillya11").click(function ()
{
$("#soon4").show();
$("#soon").hide();
$("#soon1").hide();
$("#soon2").hide();
$("#soon3").hide();
$('#close4').click(function ()
{
$("#soon4").hide();
});
Your $message is not set yet so the if statement will fail. Change it to the post message. You also need to check the message is set before you check its string length.
Note also the use of { } with the if statement when the statement is true the content between the curly braces executes.
<?php
//$message is not set here until inside the if statment
//there for we should check $_POST['message']
//if $_POST['message'] has the criteria you seek
//THEN $message will be set as shown at line 5 which i marked for you to see
if (isset($_POST['message']) && strlen($_POST['message'])> 2) {
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message']; //line 5 $message now set
$formcontent="From: $name \n Message: $message";
$recipient = "myemail";
$subject = "Contact Form";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo "Thank You!";
} else {
echo 'Your message must be longer than 2 characters!';
}
?>
This method uses the if message is less than 2 characters, then do not process the execution for the mail() function. If the message is more than that, then it will proceed in executing the mail() function.
<?php
if (strlen($_POST['message']) < 2) {
die("Sorry, try again.");
}
else {
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$formcontent="From: $name \n Message: $message";
$recipient = "myemail";
$subject = "Contact Form";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo "Thank You!";
}
?>
<?php
$message = $_POST['message'];
if (strlen($message)> 2) {
$name = $_POST['name'];
$email = $_POST['email'];
$formcontent="From: $name \n Message: $message";
$recipient = "myemail";
$subject = "Contact Form";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo "Thank You!";
}
?>
Try this
if (strlen($_POST['$message'])> 2) {
' do something;
}
I have a simple php form for contact purposes. However, it won't send the email or go to the correct page after submit. It redirects to the mail.php instead.
My contact form named contact.php is as follows:
<form id="contact-form" action="mail.php" method="POST">
<fieldset>
<label><span class="text-form">Your Name:</span> <input type="text" name="name"></label>
<label><span class="text-form">Your Email:</span><input type="text" name="email"></label>
<label><span class="text-form">Your Contact No:</span><input type="text" name="contact"></label>
<div class="wrapper">
<div class="text-form">Your Message:</div>
<textarea name="message" rows="6" cols="25"></textarea><br />
<div class="clear">
</div>
<div class="buttons">
<a class="button" href="#"><input class="button" type="submit" value="Send"></a>
<a class="button" href="#"><input class="button" type="reset" value="Clear"></a>
</div>
</fieldset>
</form>
And the php code named mail.php is as follows:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
&contact = $_POST['contact'];
$formcontent="From: $name \n Message: $message";
$recipient = "info#whatever.co.za";
$subject = "Contact form message";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
header("Location:contact.php");
?>
What am i doing wrong. It just wont send the message to email or redirect to the correct page??
Your syntax is ever so slightly wrong. &contact on line 5 should be $contact. I assume you're on a production server, so error reporting would be disabled and you wouldn't get any warnings.
Try using # before mail function
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$contact = $_POST['contact'];
$formcontent="From: $name \n Message: $message";
$recipient = "info#whatever.co.za";
$subject = "Contact form message";
$mailheader = "From: $email \r\n";
#mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
header("location: contact.php");
?>
this will let you pass even if mail function produce any issue. Also please check if there are any other header are not sent using http://php.net/manual/en/function.headers-sent.php function.
I would strongly suggest to use PHPMailer or some other class to send any kind of email.
Please check the contact.php file whether some redirection is happening from contact.php to mail.php
I think there may be an issue sending mail from your server. The mail.php file is probably tripping up on the mail function and not doing the redirect.
Create a new file with just the php mail function in there (with your email address and a test message) and browse it from your web browser, does it send?
Try to use ob_start() at the start of script, and exit(); after header("Location: contact.php");
smth like this ...
<?php
ob_start();
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
&contact = $_POST['contact'];
$formcontent="From: $name \n Message: $message";
$recipient = "info#whatever.co.za";
$subject = "Contact form message";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
header("Location: contact.php");
exit();
?>
Change the mail.php to the following:
<?php
mail("info#whatever.co.za", "Contact form message", $_POST['message'], "From: ".$_POST['name']." <".$_POST['email'].">");
header("Location: contact.php");
?>