I have put together a form with validation with help from this tutorial: http://www.w3resource.com/php/form/php-form-validation.php
I am hoping somebody can point out where I have gone wrong? As my validation works, I get a thankyou message but no mail comes through to my inbox :(
<?php
if (isset($_POST['submit'])) {
//checking name
if(empty($_POST['full_name']))
$msg_name = "*";
$name_subject = $_POST['full_name'];
//check phone number
if(empty($_POST['Phone_Num']))
$msg_Phone = "*";
$email_subject = $_POST['Phone_Num'];
//check email
if(empty($_POST['email_addr']))
$msg_email = "*";
$email_subject = $_POST['email_addr'];
$email_pattern = '/^\w+([\.-]?\w+)*#\w+([\.-]?\w+)*(\.\w{2,3})+$/';
preg_match($email_pattern, $email_subject, $email_matches);
if(!$email_matches[0])
$msg2_email = "Please enter a valid email address";
}
// validation complete
if(isset($_POST['submit'])){
if($msg_name=="" && $msg2_name=="" && $msg_email=="" && $msg2_email=="" && $msg2_Message=="")
$msg_success = "Thankyou for your enquiry";
//send mail
$to = "someone#email.com";
$subject = "Data collected thorugh from";
$message = "<p>".$_POST["full_name"]."</p><p>".$_POST["Phone_Num"]." </p><p>".$_POST["email_addr"]."</p><p>".$_POST["Message"]."</p>";
$from = "someone#email.com";
mail($to,$subject,$message);
}
?>
The PHP mail() function does not allow for sending email through SMTP with authentification. If you want do this, there is a PEAR package.
This article gives some explanation.
Related
I'm trying to create a contact form where the user will fill out their name, email, subject and message in order to contact me. It is suppose to send an email to my email account but every time i test it, it does not work. I was positive it was correct, but i guess it is not. Any help please?
<?php
//Get user input
$name = $_POST["name"];
$email = $_POST["email"];
$subject = $_POST["subject"]
$message = $_POST["message"];
//error messages
$missingName = '<p><strong>Please enter your name!</strong></p>';
$missingEmail = '<p><strong>Please enter your email address!</strong></p>';
$invalidEmail = '<p><strong>Please enter a valid email address!</strong></p>';
$missingSubject = '<p><strong>Please enter a Subject!</strong></p>';
$missingMessage = '<p><strong>Please enter a message!</strong></p>';
//if the user has submitted the form
if($_POST["submit"]){
//check for errors
if(!$name){
$errors .= $missingName;
}else{
$name = filter_var($name,FILTER_SANITIZE_STRING);
}
if(!$email){
$errors .= $missingEmail;
}else{
$email = filter_var($email, FILTER_SANITIZE_EMAIL);
if(!filter_var($email,FILTER_VALIDATE_EMAIL)){
$errors .=$invalidEmail;
}
}
if(!$subject){
$errors .= $missingSubject;
}else{
$message = filter_var($subject, FILTER_SANITIZE_STRING);
}
if(!$message){
$errors .= $missingMessage;
}else{
$message = filter_var($message, FILTER_SANITIZE_STRING);
}
//if there are any errors
if($errors){
//print error message
$resultMessage = '<div class="alert alert-danger">' . $errors .'</div>';
}else{
$to = "fanonxr#gmail.com";
$subject = "Contact";
$message = "
<p>Name: $name.</p>
<p>Email: $email.</p>
<p>Subject: $subject.</p>
<p>Message:</p>
<p><strong>$message</strong></p>";
$headers = "Content-type: text/html";
if(mail($to, $subject, $message, $headers)){
$resultMessage = '<div class="alert alert-success">Thanks for your message. We will get back to you as soon as possible!</div>';
header("Location: index.php");
}else{
$resultMessage = '<div class="alert alert-warning">Unable to send Email. Please try again later!</div>';
}
}
echo $resultMessage;
}
?>
it seems you are missing the form action and mailto since its Html i think those should be included.
your validation looks good to me.
In terms of sending email, try finding some answers here:
PHP mail form doesn't complete sending e-mail
If that doesn't address your question, please provide some more info, like what mailer you're using. Hope this helps :)
I want to send the email from the server when someone submits the contact form. Here is the sample php code.
<?php
if($_SERVER['REQUEST_METHOD'] == "POST"){
$name = strip_tags(trim($_POST['name']));
$email = strip_tags(trim($_POST['email']));
$dept = strip_tags(trim($_POST['dept']));
$message = strip_tags(trim($_POST['message']));
if((!isset($email) || empty($email)) || (!isset($name) || empty($name)) || (!isset($dept) || empty($dept)) || (!isset($message) || empty($message))){
echo -1;
return;
}
if(filter_var($email, FILTER_VALIDATE_EMAIL)===false){
echo -2;
return;
}
$to = 'farooqahmadkhan003#gmail.com';
$email_subject = "Splendenti Eye Wear | Contact Us";
$email_body = "Contacted by\nName: $name\nEmail: $email\nDepartment: $dept\nMessage: $message";
$headers = "noreply#farooq.com\n"; // This is the email address the generated message will be from. We recommend using something like noreply#yourdomain.com.
$headers .= "Reply-To: ".$_POST['email'];
if(mail($to,$email_subject,$email_body,$headers)){
echo 1; // email sent
}
else{
echo "failed to send email";
}
}else{
echo "Invalid Request";
}
?>
but the mail function is returning false. Can anybody guide??
I suggest you to use PHPMailer to send emails from your PHP files.
Check this link and download it. There are many documentations about PHPMailer online. You can control all the details about your mail with PHPMailer.
try to use # to suppress warnings:
if(#mail($to,$email_subject,$email_body,$headers)){
echo 1; // email sent
}
If this doesn't help you have a configuration issue.
I am trying to set up a simple contact form for my website. The end goal of the form is for the user to submit their information and have the server process and deliver the email as if it came directly from the user. In other words, if John Doe, whose email address is johndoe#example.com, fills out the form, then the email would read as if John Doe sent it from his email address, johndoe#example.com. What's mind-boggling to me is that I figured my server might not have authorization to send on the user's behalf; however, an older PHP form that I'm trying to move away from executes just fine.
Currently, this is my mail processing form PHP:
<?php
if (($_SERVER['REQUEST_METHOD'] == 'POST') && (!empty($_POST['action']))):
if (isset($_POST['myname'])) { $myname = $_POST['myname']; }
if (isset($_POST['myemail'])) {
$myemail = filter_var($_POST['myemail'], FILTER_VALIDATE_EMAIL );
}
if (isset($_POST['mysubject'])) { $mysubject = $_POST['mysubject']; }
if (isset($_POST['mycomments'])) {
$mycomments = filter_var($_POST['mycomments'], FILTER_SANITIZE_STRING );
}
if (isset($_POST['reference'])) { $reference = $_POST['reference']; }
if (isset($_POST['requesttype'])) { $requesttype = $_POST['requesttype']; }
$formerrors = false;
if ($myname === '') :
$err_myname = '<div class="error">Sorry, your name is a required field</div>';
$formerrors = true;
endif; // input field empty
if ($mysubject === '') :
$err_myname = '<div class="error">Please enter a subject for your message.</div>';
$formerrors = true;
endif; // input field empty
if ( !(preg_match('/[A-Za-z]+, [A-Za-z]+/', $myname)) ) :
$err_patternmatch = '<div class="error">Sorry, the name must be in the format: Last, First</div>';
$formerrors = true;
endif; // pattern doesn't match
if (!($formerrors)) :
$to = 'example#example.com';
$subject = "$mysubject";
$message = "\nName: $myname\n\nEmail: $myemail\n\nSubject: $mysubject\n\nComments: $mycomments\n";
$from = $myname.'<'.$myemail.'>';
$headers = 'From: '.$from."\r\n".
'Reply-To: '.$myemail."\r\n";
if (mail($to, $subject, $message, $headers)):
$msg = "Thanks for filling out our form";
else:
$msg = "Problem sending the message";
endif; // mail form data
endif; // check for form errors
endif; //form submitted
?>
Currently the reply-to works just fine, but the email received via the form indicates LastName#hostdomain.com. Thank you for your contributions.
We have a PHP form on our website that has worked for years. When users fill the form out in sends us the details, and sends them an email and then re-directs the user to a "thank-you" page. However, if a user puts their email address as #gmail.com the form fails to send, it doesnt show the thank-you page.
This is really bizarre. We are using PHPMailer and validate if the form has been sent using:
if($mail->Send()) {
$mailsent = true;
If a user enters #hotmail.com or at #yahoo.com everything is fine. But enter #gmail.com and $mailsent is always false.
In this situation where does the problem lye? It seems to me that our web hosts SMTP connection to Gmail is failing. Would this seem right?
Here is the code:
<?php
error_reporting(E_ALL);
ini_set("display_errors", 1);
$name = '';
$email = '';
$mailsent = false;
$referer = '';
if(isset($_POST['name'])){
include('phpmailer/class.phpmailer.php');
$name = $_POST['name'];
$email = $_POST['email'];
$subject = 'Quote request from ' . $name;
$body = 'A quote request has been received from a user with following details:' . "\n\n" .
"Name: $name \n" .
"Email: $email \n" .
"----------------------------------------------------\n\n" .
"Place: UK".
$body .= "\n----------------------------------------------------\n\n";
$body .= "Refering: ".$referer." \n";
$mail = new PHPMailer();
$mail->Subject = $subject;
$mail->From = $email;
$mail->FromName = $name;
$mail->Body = $body;
$mail->AddAddress("dean#example.com", "Example");
if($mail->Send()) {
$mailsent = true;
} else {
$error[] = 'There was some error. Please try later.';
}
}
?>
I have the below PHP contact form that has a CAPTCHA code to ensure is correct. However, when I reply to the email from the website it puts a random email which i believe is the server admin, however, I want it to be the persons email who sent the form in. below is the code, could you possibly be able to help me?
<?php session_start();
if(isset($_POST['Submit'])) { if( $_SESSION['chapcha_code'] == $_POST['chapcha_code'] && !empty ($_SESSION['chapcha_code'] ) ) {
$youremail = 'info#example.com';
$fromsubject = 'www.example.co.uk';
$title = $_POST['title'];
$fname = $_POST['fname'];
$mail = $_POST['mail'];
$phone = $_POST['phone'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$to = $youremail;
$mailsubject = 'Message from Website'.$fromsubject.' Contact Page';
$body = $fromsubject.'
The person that contacted you is: '.$fname.'
Phone Number: '.$phone.'
E-mail: '.$mail.'
Subject: '.$subject.'
Message:
'.$message.'
|---------END MESSAGE----------|';
echo "Thank you for your message. I will contact you shortly if needed.<br/>Go to <a href='/index.html'>Home Page</a>";
mail($to, $subject, $body);
unset($_SESSION['chapcha_code']);
} else {
echo 'Sorry, you have provided an invalid security code';
}
} else {
echo "You must write a message. </br> Please go to <a href='/contact.html'>Contact Page</a>";
}
?>
You'll need some headers so the from address is the users mail.
Also refer to the mail docs
try this
$headers = "From: $mail\r\n";
$headers .= "Reply-To: $mail\r\n";
mail($to, $subject,$body,$headers);