HTML form working but need a php validation - php

i need a help with this. What i need to do to validate the email in this form? This is a landing page, I want to get the email of a visitor before he/she can visit my content here is this demo.
Can I get some tips or the right code to use for this page?
<form id="contact-form" action="send.php">
<input type="text" name="email" placeholder="you#yourmail.com" class="cform-text" size="40" title="your email" required>
<input type="submit" value="Enter Now" class="cform-submit">
</form>
send.php file:
$visitorEmail = $_GET['email'];
$email_from = "doctordongok#gmail.com";
$email_subject = "New Form Submission! - From: " + $visitorEmail;
// edit here
$email_body = "New visitor - $visitorEmail";
$to = "doctordongok#gmail.com";
$headers = "From: $email_from \r \n";
$headers .= "Reply-To: $visitorEmail \r \n";
mail($to, $email_subject, $email_body, $headers);
header("Location: http://www.de-signs.com.au");
Thank you!

Use the filter_var() function of PHP:
if(filter_var($visitorEmail, FILTER_VALIDATE_EMAIL) === false) {
// Invalid E-Mail address
} else {
// OK
}
So to mockup your new script. It will look something like this
$visitorEmail = $_GET['email'];
if(filter_var($visitorEmail, FILTER_VALIDATE_EMAIL) === false) {
die('Sorry that\'s no valid e-mail address');
} else {
$email_from = 'doctordongok#gmail.com';
$email_subject = 'New Form Submission! - From: ' . $visitorEmail;
// edit here
$email_body = 'New visitor - ' . $visitorEmail;
$to = 'doctordongok#gmail.com';
$headers = 'From: $email_from' . PHP_EOL;
$headers .= 'Reply-To: $visitorEmail' . PHP_EOL;
mail($to, $email_subject, $email_body, $headers);
header('Location: http://www.de-signs.com.au');
exit;
}

Your best bet is probably to validate on the client side using a Javascript and on the server side as well using a regular expression on both ends.
Validating an email with Javascript

HTML5's new email type for input is here to help you.
<input type="email" name="email" placeholder="you#yourmail.com" class="cform-text" size="40" title="your email" required />

Related

PHP contact form configuration [duplicate]

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 10 months ago.
Hi guys I run my website and it all work except contact us form.
HTML
<div class="border"></div>
<form class="contact-form" action="mail.php" method="post">
<div class="fisrtthree">
<input type="text" name="fname" class="contact-form-text" placeholder="First name" required>
</div>
<div class="fisrtthree">
<input type="text" name="lname" class="contact-form-text" placeholder="Last name" required>
</div>
<div class="fisrtthree">
<input type="email" name="email" class="contact-form-text" placeholder="Email" required>
</div>
<div>
<textarea class="contact-form-text" name="message" rows="6" maxlength="3000" placeholder="Your message" required></textarea>
</div>
<div>
<button type="submit" id="fcf-button" class="contact-form-btn">Send</button>
<div>
</form>
PHP
<?php
if(isset($_POST['message']) == false) { // If there's no message
echo "Uh oh. Looks like you didn't actually include a message, friend.<br><br>";
die();
}
$destination = "##gmail.com"; // Put your email address here
$subject = "Message from your website!"; // Fill in the subject line you want your messages to have
$fromAddress = "##domain.com"; // Fill in the email address that you want the messages to appear to be from
// Use a real address to reduce the odds of getting spam-filtered.
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$email = $_POST['email'];
$message = str_replace("\n.", "\n..", $_POST['message']); // Prevents a new line starting with a period from being omitted
$message = "First Name: ". $fname ."\n Last Name: ". $lname ."\n Email: ". $email ."\n Message: ".$message."\n";
$headers = array();
$headers[] = "MIME-Version: 1.0";
$headers[] = "Content-type: text/plain; charset=iso-8859-1";
$headers[] = "From: " . $fromAddress;
$headers[] = "Subject: " . $subject;
$headers[] = "X-Mailer: PHP/".phpversion();
mail($destination, $subject, $message, implode("\r\n", $headers));
Thanks for your message:
echo $message; ?>
Go back home
?>
As you see I tried to get message from ##domain.com and received it in mygmail#gmail.com. but it didn't work, I received nothing in my inbox. is there anything to do with my gmail and my hosted email.
Your email is not being sent. You can try to capture the error that happened via php's last error function, after sending the email :
print_r(error_get_last());
Firstly, ensure that your email service provider makes allowance for the use of less secure apps, and make sure you have this feature enabled (Be sure to disable it later). My 'from' email was a Gmail account where non-secure apps can be enabled at this address: https://myaccount.google.com/lesssecureapps?pli=1
Secondly, ensure that your local mail server is correctly set up. If you are using XAMPP follow the directions here: https://www.geeksforgeeks.org/how-to-configure-xampp-to-send-mail-from-localhost-using-php/
Next, name the button on your form in order to detect the form submission, I named the button 'submit'.
Then in mail.php use this code
<?PHP
if(isset($_POST['submit']) && empty($_POST['message'])) {
// If there's no message
echo "Uh oh. Looks like you didn't actually include a
message, friend.<br><br>";
die();
}
$destination = "fihriabdelali#gmail.com";
$subject = "Message from your alfidomain.com!";
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$email = $_POST['email'];
$fromAddress=$email;
$message = str_replace("\n.", "\n..", $_POST['message']);
// Prevents a new line starting with a period from being omitted
$message = "First Name: ". $fname ."\n Last Name: ".$lname ."\n Email: ". $email ."\n Message: ".$message."\n";
$headers = array();
$headers[] = "MIME-Version: 1.0";
$headers[] = "Content-type: text/plain; charset=iso-8859-1";
$headers[] = "From: " . $fromAddress;
$headers[] = "Subject: " . $subject;
$headers[] = "X-Mailer: PHP/".phpversion();
mail($destination, $subject, $message, implode("\r\n",
$headers));
// mail($to,$subject,$msg,$headers);
echo "Email successfully sent.";
?>
If you are not Abdelali make sure you set $destination to "youremail#domain.com"

display a message after submit and refresh

i'm trying to get a message to appear after my form has been submitted, i can get the form to submit and the page to then refresh but unsure how to add the message after the refresh.
i have no knowledge of PHP so sorry is its and obvious answer
thanks
my html & PHP if needed
<form action="action.php" method="POST" target="my_iframe" onsubmit="setTimeout(function(){window.location.reload();},10);">
<input type="text" id="name" name="name" placeholder="name" required="required">
<input type="email" id="email" name="email" placeholder="email" required="required">
<input type="tel" id="phone" name="phone" placeholder="phone">
<div class="form-row" style="display:none;"><input type="hidden" name="url" placeholder="URL"></div>
<textarea id="message" name="message" placeholder="message" style="height:200px" required="required"></textarea>
<input id="submit" type="submit" value="submit">
</form>
<iframe name="my_iframe" width="1" height="1" style="border:none"></iframe>
<?php
$to = 'zoeharrisondesign#gmail.com';
$subject = 'New Message Recieved!';
$from = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];
$name = $_POST['name'];
//check honeypot
if( !empty( $honeypot ) ) {
echo 'There was a problem';
}
// 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";
// Create email headers
$headers .= 'From: '.$from."\r\n".
'Reply-To: '.$from."\r\n" .
'X-Mailer: PHP/' . phpversion();
// Compose a simple HTML email message
$message = "$message \r\n |
From $name \r\n |
Tel: $phone";
// Sending email
if(mail($to, $subject, $message, $headers)){
echo 'Your message has been sent successfully.';
} else{
echo 'Unable to send email. Please try again.';
}
?>
Update your php sending email to this
if (mail($myEmail, $subject, $message)){
$success = "Message sent successful";
}else{
$success = "Message Sending Failed.";
}
Then add this php codes in your html codes add this code to display a message. Put it on top of your form html tag.
<?php
if (isset($success)){ echo "<div>" . $success . "</div>";}
?>
You may even add some styling on the message if you prefer.

Upon submitting form, scroll to vertical position on page

I have created a simple html/php form where visitors on my site can write their name, email and message and then send the message to my email. Problem is that when they submit the email, my site then performs a full refresh (it looks like) and therefore just reloads to the top of my site. I would like for the user to remain at the same scroll position after submit, so that they can instantly see whether the submit was succesful or not. So either a solution that prevents the refresh or some other solution that automatically scrolls down vertical to the form.
Can you tell me if this is possible using php? Or do I have to use some jquery/ajax solution?
Below is the code I am using. I am a complete novice, so please be gentle.
<form action="" method="post" id="form">
<div class="contact-info-group">
<label for="name"><span>Your name</span>
<input type="text" id="name" name="name" autocomplete="off" value="<?php echo $name; ?>"></label>
<label for="email"><span>Your email</span>
<input type="email" id="email" name="email" autocomplete="off"></label>
</div>
<label for="message"><span>Your message</span>
<textarea id="message" name="message"></textarea></label>
<input id="button1" type="submit" class="button next" name="contact_submit" value="Send message">
<?php
// Check for header injections
function has_header_injection($str) {
return preg_match("/[\r\n]/", $str);
}
if (isset ($_POST['contact_submit'])) {
$name = trim($_POST['name']);
$email = trim($_POST['email']);
$msg = $_POST['message'];
// Check to see if $name or $email have header injections
if (has_header_injection($name) || has_header_injection($email)) {
die();
}
if (!$name || !$email || !$msg) {
echo '<div class="contact-warning"><h2>! Error - Please note that all of the above fields are required !</h2></div>';
exit;
}
// Add the recipient email to a variable
$to = "email#email.com";
// Create a subject
$subject = "Message via website.com - $name";
// Construct the message
$message = "Name: $name\r\n";
$message .= "Email: $email\r\n";
$message .= "Message: \r\n\r\n$msg";
// Clean up the message
$message = wordwrap($message, 72);
// Set the mail headers into a variable
$headers = "MIME-Version 1.0\r\n";
$headers .= "Content-type: text/plain; charset=iso-8859-1\r\n";
$headers .= "From: $name <$email> \r\n";
$headers .= "X-Priority: 1\r\n";
$headers .= "X-MSMail-Priority: High\r\n\r\n";
// Send the email
mail($to, $subject, $message, $headers);
echo '<div class="contact-warning"><h2>Thank you for your message. We will get back to you shortly.</h2></div>';
}
?>
</form>

Using HTML and PHP to send form data to an email

Like the title says, sending a form to my email. I get no errors, but the email never comes. Here's my HTML form (I don't think you'll need anything else, but there is also some formatting and Java verification in the file).
<form method="POST" name="contactform" action="contact-form-handler.php">
<p>
<label for='name'>Your Name:</label>
<br>
<input type="text" name="name">
</p>
<p>
<label for='email'>Email Address:</label>
<br>
<input type="text" name="email">
<br>
</p>
<p>
<label for='message'>Message:</label>
<br>
<textarea name="message"></textarea>
</p>
<input type="submit" value="Submit">
<br>
</form>
And here's my PHP. Obviously I took my email out and put in EMAIL instead, but other than that this is my complete PHP file. The thank you PHP file pulls up the submitted page just fine, and I get no errors. Just no email either.
<?php
$errors = '';
$myemail = 'EMAIL#gmail.com';
if(empty($_POST['name']) ||
empty($_POST['email']) ||
empty($_POST['message']))
{
$errors .= "\n Error: all fields are required";
}
$name = $_POST['name'];
$email_address = $_POST['email'];
$message = $_POST['message'];
if (!preg_match(
"/ ^[_a-z0-9-]+(\.[_a-z0-9-]+)*#[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i",
$email_address))
{
$errors .= "\n Error: Invalid email address";
}
if( empty($errors))
{
$to = '$myemail';
$email_subject = "Contact form submission: $name";
$email_body = "You have received a new message. ".
" Here are the details:\n Name: $name \n ".
"Email: $email_address\n Message \n $message";
$headers = "From: $myemail\n";
$headers .= "Reply-To: $email_address";
mail($to,$email_subject,$email_body,$headers);
//redirect to the 'thank you' page
header('Location: contact-form-thank-you.html');
}
?>
Thanks ahead of time for any help you can provide! I can give the rest of my HTML file or my other PHP file if you need it, this is where all the real functionality lies though.
*PHP to send form data to an email i have used this code as well as its work for me .you can try *
<?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);
?>
after that you can addded your futher code
Try add in top file:
error_reporting(E_ALL);
and edit your code, see:
if(mail($to,$email_subject,$email_body,$headers)){
//redirect to the 'thank you' page
header('Location: contact-form-thank-you.html');
} else {
echo 'Error!';
}
Read this:
http://www.php.net/manual/en/function.error-reporting.php
http://www.php.net/errorfunc
http://php.net/manual/pt_BR/function.set-error-handler.php
http://www.php.net/register_shutdown_function
you have the variable $myemail as a string value after $to = Remove the parentheses and your code will work
The problem is with your From field in your $headers variable.
you can't just put any email address in there. For example: you are supposed to put an existing email address of your server that you create. Or if you want to use a gmail account in from field, you need to configure your gmail username and password with your server first before using that email.
The simplest solution is to create a new email address on your hosting server and then put that email address in your from field in $headers variable.
I've already mentioned it in details here.

PHP Mail form not sending emails on Linux server

This is my code and my form... it acts like the email was sent, but it never arrives.
I would like to know what could possibly be wrong... Anyone?
The website is hosted on a Linux server, and I don't really know if the server could be blocking the emails because of some kind of incompatibility... I don't really know what it could be.
<?php
if($_POST)
{
//check if its an ajax request, exit if not
if(!isset($_SERVER['HTTP_X_REQUESTED_WITH']) AND strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) != 'xmlhttprequest') {
die();
}
$to_Email = "myemail#gmail.com"; //Replace with recipient email address
$subject = 'Ah!! My email from Somebody out there...'; //Subject line for emails
//check $_POST vars are set, exit if any missing
if(!isset($_POST["userName"]) || !isset($_POST["userEmail"]) || !isset($_POST["userPhone"]) || !isset($_POST["userMessage"]))
{
die();
}
//Sanitize input data using PHP filter_var().
$user_Name = filter_var($_POST["userName"], FILTER_SANITIZE_STRING);
$user_Email = filter_var($_POST["userEmail"], FILTER_SANITIZE_EMAIL);
$user_Phone = filter_var($_POST["userPhone"], FILTER_SANITIZE_STRING);
$user_Message = filter_var($_POST["userMessage"], FILTER_SANITIZE_STRING);
//additional php validation
if(strlen($user_Name)<4) // If length is less than 4 it will throw an HTTP error.
{
header('HTTP/1.1 500 Name is too short or empty!');
exit();
}
if(!filter_var($user_Email, FILTER_VALIDATE_EMAIL)) //email validation
{
header('HTTP/1.1 500 Please enter a valid email!');
exit();
}
if(!is_numeric($user_Phone)) //check entered data is numbers
{
header('HTTP/1.1 500 Only numbers allowed in phone field');
exit();
}
if(strlen($user_Message)<5) //check emtpy message
{
header('HTTP/1.1 500 Too short message! Please enter something.');
exit();
}
//proceed with PHP email.
$headers = 'From: '.$user_Email.'' . "rn" .
'Reply-To: '.$user_Email.'' . "rn" .
'X-Mailer: PHP/' . phpversion();
#$sentMail = mail($to_Email, $subject, $user_Message .' -'.$user_Name, $headers);
if(!$sentMail)
{
header('HTTP/1.1 500 Couldnot send mail! Sorry..');
exit();
}else{
echo 'Hi '.$user_Name .', Thank you for your email! ';
echo 'Your email has already arrived in my Inbox, all I need to do is Check it.';
}
}
?>
Here's my form:
<fieldset id="contact_form">
<legend>My Contact Form</legend>
<div id="result"></div>
<input type="text" name="name" id="inputt" placeholder="Enter Your Name" />
<input type="text" name="email" id="inputt" placeholder="Enter Your Email" />
<input type="text" name="phone" id="inputt" placeholder="Phone Number" />
<textarea name="message" id="message" placeholder="Enter Your Name"></textarea>
<button class="submit_btn" id="submit_btn">Submit</button>
</fieldset>
Your $headers are wrong, you're appending "rn" instead of "\r\n". Try this instead:
$headers = 'From: '.$user_Email. "\r\n" .
'Reply-To: '.$user_Email. "\r\n" .
'X-Mailer: PHP/' . phpversion();
According from your comment out of the maillog you need to adjust your php.ini to include the --r argument to your sendmail_path.
By default it usually is:
sendmail_path = /usr/sbin/sendmail -t -i
Apparently you are using another argument that also requires the --r argument. Appending that to your sendmail_path should get a long way.

Categories