I am very new to PHP, I had this working on a windows server, But just recently migrated to a LINUX server and now the form shows as its sending but nothing is coming through? Any help would be greatly appreciated,, Thanks
HTML
<div class="wrapper">
<div id="contact_form">
<form action="html_form_send.php" method="POST">
<label>
<span>Name*:</span>
<br>
<input type="text" placeholder="Please enter your name" name="name" id="name" required autofocus>
</label>
<label>
<span>Phone:</span>
<input type="text" placeholder="Please enter your phone" name="phone" id="phone">
</label>
<label>
<span>Email*:</span>
<input type="email" placeholder="youremail#gmail.com" name="email" id="email" required>
</label>
<label>
<span>Message*:</span>
<input name="message" type="text" required id="message" placeholder="Message" value="">
</label>
<input class="sendButton" type="submit" name="Submit" value="Send">
</form>
</div>
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 \n";
$recipient = "dudetotal#gmail.com";
$subject = "Design Contact";
$mailheader = "From: $email \n";
mail($recipient, $subject,$formcontent, $mailheader) or die("Error!");header("Location: http://website.com/website.com/success.html");
?>
suggestions?
You should use ternary operators, and validate based on the value:
$name = isset($_POST['name']) ? $_POST['name'] : ""; // don't forget to sanitize
if ($name != "") {
// do logic
}
Also, it's pointless to have a header() after the die() statement as the die() statement will end all further execution.
If the email is still not sending, your mail configuration may not be set up properly.
Edit: also look into sanitization. Here is a link to get you started with it.
You can do something like this.
if ($_POST['name'] && $_POST['email'] && $_POST['phone'] && $_POST['message']) {
// your email sending logic is here:
} else {
echo "All fields are required..
}
Related
When using a html form to call php to send an email with the details in the form to an email address specified in the php. The php file name is added to the URL but no email is sent and the page shown in blank.
The website is being hosted on my.ionos.co.uk (1&1) using php 7.3.
<form method="post" action="contact-form.php">
<input class="contact" type="text" name="Name" placeholder="Name">
<input class="contact" type="text" name="Email" placeholder="Your Email Address">
<input class="contact" type="text" name="Subject" placeholder="Subject">
<textarea class="contact" name="Message" rows="10" placeholder="Message"></textarea>
<input class="contact-submit" type="submit" value="Send Message">
</form>
<?php
if(isset($_POST['submit'])) {
$name = $_POST['Name'];
$mailFrom = $_POST['Email'];
$subject = $_POST['Subject'];
$message = $_POST['Message'];
$mailTo = "X#hotmail.com";
$headers = "From: ".$mailFrom;
$txt = "You have recieved an email from ".$name.".\n\n".$Message;
if(mail($mailTo, $subject, $txt, $headers)){
echo "<h1>Mail send successfully</h1>";
} else {
echo "<h1>Mail failed to send<h1>";
}
header("Location: contact.html?mailsend");
}
?>
I'd appreciate it if someone could help, thanks in advance!
Try to change:
if(isset($_POST['submit'])) {
with
if(isset($_POST['Email'])) { //or another field in your form. If you want you can also add in your submit button: name="submit"
Checking out your code seems there is not an input with name submit. So you never enter in the if case.
So I'm trying to make a contact me form for a website and this is my first time making one of these forms. My issue is that for some odd reason my button is not sending anything and the button doesn't want to work.
This is the input form
<div>
<form id="contact-us" method="post" action="contact-form-handler.php">
<input name="name" type="text"class="form-control" placeholder="Your name" required>
<br>
<input name="phone" type="number" class="form-control" placeholder="Phonenumber no symbols or dashes"required>
<br>
<input name="email" type="email" class="form-control" placeholder="Your email" required>
<br>
<textarea name="message" class="form-control" placeholder="write us a message" rows="4" required></textarea>
<br>
<input type="submit" class="form-control-submit" value="SEND">
</form>
</div>
Here is my handler.
<?php
$name = $_POST['name'];
$visitor_email = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];
$email_from = 'website.com';
$email_subject = 'New message from customer';
$email_body = "name: .$name. \n".
"email: .$visitor_mail.\n".
"phone: .$phone. \n".
"message: .$message. \n";
$to= "email#gmail.com";
$headers= $email_from.;
$headers .= $visitor_email.;
mail($to,$email_subject,$email_body,$headers);
header("Location: index.html");
?>
I'm not sure why this isn't working but if anyone could help me that would be amazing.
<div>
<form id="contact-us" method="post" action="contact-form-handler.php">
<input name="name" type="text" class="form-control" placeholder="Your name" required>
<br>
<input name="phone" type="number" class="form-control" placeholder="Phonenumber no symbols or dashes" required>
<br>
<input name="email" type="email" class="form-control" placeholder="Your email" required>
<br>
<textarea name="message" class="form-control" placeholder="write us a message" rows="4" required></textarea>
<br>
<input name="submit" type="submit" class="form-control-submit" value="SEND">
</form>
</div>
Handler
if (isset($_POST['submit'])) {
$name = $_POST['name'];
$visitor_email = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];
$email_from = 'website.com';
$email_subject = 'New message from customer';
$email_body = "name: {$name} \n" .
"email: {$visitor_email}\n" .
"phone: {$phone} \n" .
"message: {$message} \n";
$to = "email#gmail.com";
$headers = $email_from;
$headers .= $visitor_email;
$res = mail($to, $email_subject, $email_body, $headers);
header("Location: index.html");
}
I found a few errors in your code:
$headers= $email_from.; // syntax error - need to remove dot before ;
$headers .= $visitor_email.; // syntax error - need to remove dot before; same here
In your email body, you used $visitor_mail variable, but your is $visitor_email
Also, I've added the name tag to your form and added an additional validation condition.
Please try to use this solution, I hope it will help you. This solution is working fine on my side
I am attempting to send the details in the fields of a contact form to my mail id. I dont seem to be getting any errors when I hit
submit but I am not receiving any mail.
Here is my code
<?PHP
$email = $_POST["email"];
$to = "myemail#example.com";
$subject = "New Email Address for Mailing List";
$headers = "From: $email\n";
$message = "A visitor to your site has sent the following details to you.\n
Name : $name
Email Address: $email
Phone number: $phone
Message: $message";
$user = "$email";
$usersubject = "Thank You";
$userheaders = "From: myemail#example.com\n";
$usermessage = "Thank you for contacting us.";
mail($to,$subject,$message,$headers);
mail($user,$usersubject,$usermessage,$userheaders);
?>
<form id="contact-form" class="contact-form" action="contact-form-info.php" enctype="text/plain" >
<div id="result" class="result"></div>
<input type="text" name="name" id="name" placeholder="Name *">
<input type="text" name="email" id="email" placeholder="E-mail *">
<input type="text" name="phone" id="phone" placeholder="Phone *" onChange="PhoneValidation(this)" ;>
<textarea cols="5" rows="5" name="message" id="message" placeholder="Message *"></textarea>
<input type="submit" class="btn-dark" value="SEND">
Your form is:
Encoding the data as text/plain, which isn't machine readable
Sending the data as GET not POST (which is what you are trying to read)
Remove the enctype attribute and add method="POST"
You are also never assigning values to $name or $phone.
Hi guys i'm having a issue i hope you guys can help with, i'm typing in all the fields and then upon pressing submit i'm getting just "Error!" on my screen.
Please see the code:
HTML
<h2 class="formhead">Contact Form</h2>
<br>
<form class="form" action="mail.php" method="POST">
<p class="name">
<input type="text" name="name" id="name" placeholder="John Doe" />
<label for="name">Name</label>
</p>
<br>
<p class="email">
<input type="text" name="email" id="email" placeholder="mail#example.com" />
<label for="email">Email</label>
</p>
<br>
<p class="number">
<input type="text" name="number" id="number" placeholder="0774XXXXXXX" />
<label for="name">Contact Number</label>
</p>
<br>
<p class="web">
<input type="text" name="web" id="web" placeholder="www.example.co.uk" />
<label for="name">Website</label>
</p>
<br>
<p class="message">
<textarea name="message" id="message" placeholder="Write something to us" /> </textarea>
</p>
<br>
<p class="submit">
<input type="submit" value="Send"/>
</p>
</form>
PHP
<?php $name = $_POST['name'];
$email = $_POST['email'];
$number = $_POST['number'];
$message = $_POST['message'];
$website = $_POST['web'];
$formcontent="From: $name \n Contact: $number \n Website: $web \n Message: $message";
$recipient = "enquiries#c(hidden)y.co.uk";
$subject = "Contact Form";
$mailheader = "From: $email ";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo "Thank You!" . " -" . "<a href='contact.html' style='text-decoration:none;color:#ff0099;'> Return Home</a>";
?>
Any help would be much appreciated!
Thanks
Sam
Your script always reporting 'Error!' because the mail() function always fails. That's because some index you're using in the php file doesn't match to the input names in your form:
Change these:
$website = $_POST['website'];
to:
$website = $_POST['web'];
Or change it in your form.
Also you have to specify a name for the message textarea:
<textarea name="message" id="message" placeholder="Write something to us" />
This may fail again if it can't connect to mailserver. This is probably you're case if The SMTP is Disabled.
As per my comment, here's an example of a better die statement:
<?
$your_function or die("Error! a") // Just replace the letter a with anything. It serves as a simple link to your function that only you know. so you can go back and check it
I am having a bit trouble getting the PHP script to work. I am making a very basic form just collecting name and email
<form action"email.php" method="POST" id="signup-form">
<p><input type="text" id="name" name="name" placeholder="name" /></p>
<p><input type="email" id="email" name="email" placeholder="email address" /></p>
<p class="form-note"><em>* we will only email you when our store is up</em></p>
<p><input type="submit" value="Get Notified" /></p>
</form>
My PHP script is
<?php
$error = false;
$sent = false;
if(isset($_POST['submit'])) {
if(empty($_POST['name']) || empty($_POST['email'])) {
$error = true;
}
else {
$to = "my.name#gmail.com";
$name = trim($_POST['name']);
$email = trim($_POST['email']);
$subject = "New Subscriber";
$message = "Name: $name \r\n Email: $email";
$headers = "From:" . $name;
$mailsent = mail($to, $subject, $message, $headers);
if($mailsent) {
$sent = true;
}
}
}
?>
I'm using a Linux hosting company, net registry. I tried to get the PHP errors turned on, but couldn't see how in my cpanel. The mail is not sent, but I have no way of seeing the error preventing it.
You have an error in your html. Try:
<form action="email.php"
Change your HTML to this and try
<form action="email.php" method="POST" id="signup-form">
<p><input type="text" id="name" name="name" placeholder="name" /></p>
<p><input type="email" id="email" name="email" placeholder="email address" /></p>
<p class="form-note"><em>* we will only email you when our store is up</em></p>
<p><input type="submit" value="Get Notified" /></p>
</form>