<?php
if (isset($_POST["sendMessage"])) {
$firstName = trim($_POST['firstName']);
$lastName = trim($_POST['lastName']);
$email = trim($_POST['email']);
$phone = trim($_POST['phone']);
$message = trim($_POST['message']);
$from = 'somebody#gmail.com';
$to = 'someone#gmail.com';
$subject = 'webmaster#example.com';
$txt = 'Prottyasha School';
$headers = "From: somebody#gmail.com" . "\r\n" .
"CC: somebody#gmail.com";
$body = "From: First-Name: $firstName\n Last-Name: $lastName\n
E-Mail: $email\n Phone: $phone\n Message: $message";
//echo "success?";
if (mail($to, $subject, $body, $headers)) {
$result = '<div class="alert alert-success">Thank You! I will be in touch</div>';
} else {
$result = '<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later</div>';
}
echo $result;
}
?>
It works fine but i want to make it validated. suppose someone give a invalid phone number or email then it will message its invalid mail or phone number. i want to make it full validated. anyone please help me.
I don't think it's possible to check if the mail was successfully delivered. mail() returns boolean true when a mail has been accepted for delivery or sent to the local email service. There's no way to know if that email was delivered to the recipient.
Related
I am trying to adjust my code to able to reply to sender email from PHP contact form. please check my code below to give advise. Thank you
<?php
$marke = $_POST['marke'];
$modell = $_POST['modell'];
$name = $_POST['name'];
$adresse = $_POST['adresse'];
$telefon = $_POST['telefon'];
$email = $_POST['email'];
$to = 'myemail#gmail.com';
$from = 'myemail#gmail.com';
$subject = 'Contact Form';
$body = "marke: $marke\n modell: $modell\n name: $name\n adresse: $adresse\n
email: $email\n telefon: $telefon\n";
?>
<?php
if ($_POST['submit']) {
if (mail ($to, $subject, $body, $from)) {
header("Location: http://www.website.com/sent.php");
} else {
echo '<p>Oops! An error occurred. Try sending your message again.</p>';
}
}
?>
First make headers
$headers = "From: $from\r\nReply-to: $email";
Than fix calling of mail function to be
mail ($to, $subject, $body, $headers)
Didn't tried it from times when it was PHP 4 but it will probably work as you expected...
Addition:
I just checked on php.net... go to this url http://php.net/manual/en/function.mail.php and check "Example #2 Sending mail with extra headers."
<?php
if (isset($_POST["sendMessage"])) {
$firstName = $_POST['firstName'];
$lastName = $_POST['last-name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];
$from= 'info#address.com';
$to = 'some#gmail.com';
$subject = 'Message from Contact Demo ';
$txt= 'demo';
$headers = "From: webmaster#example.com" . "\r\n" .
"CC: somebodyelse#example.com";
$body = "From: First-Name: $firstName\n Last-Name: $lastName\n
E-Mail: $email\n Phone: $phone\n Message: $message";
$errFirstName='';$errLastName='';$errEmail='';$errPhone='';$errMessage='';
// If there are no errors, send the email
if (mail ($to, $subject,$txt,$headers)) {
$result='<div class="alert alert-success">Thank You! I will be in touch</div>';
} else {
$result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later</div>';
}
}
?>
i am trying to make a mail validation but it will not work.Here some text which will give a success message or error message.but it show nothing . i can't understand my fault. please help someone.
Try this code on your live server
$firstName = "sunny";
$lastName = "khatri";
$email = '******#gmail.com';
$phone = '********';
$message ='Hello this is test';
$from= '*****#gmail.com';
$to = '***#gmail.com';
$subject = 'Message from Contact Demo ';
$txt= 'demo';
$headers = "From: ******#*****.com" . "\r\n" .
"CC: ********#live.in";
$body = "From: First-Name: $firstName\n Last-Name: $lastName\n
E-Mail: $email\n Phone: $phone\n Message: $message";
$errFirstName='';$errLastName='';$errEmail='';$errPhone='';$errMessage='';
// If there are no errors, send the email
if (mail ($to, $subject,$txt,$headers)) {
$result='<div class="alert alert-success">Thank You! I will be in touch</div>';
} else {
$result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later</div>';
}
echo $result;
Just use default function from PHP manual:
<?php
$email_a = 'joe#example.com';
$email_b = 'bogus';
if (filter_var($email_a, FILTER_VALIDATE_EMAIL)) {
echo "This ($email_a) email address is considered valid.\n";
}
if (filter_var($email_b, FILTER_VALIDATE_EMAIL)) {
echo "This ($email_b) email address is considered valid.\n";
} else {
echo "This ($email_b) email address is considered invalid.\n";
}
?>
First You Have To Check Email Is Valid Using This Php Function
$email = test_input($_POST["email"]);
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$emailErr = "Invalid email format";
}
Then If Its Not Valid Give A Error
Else
if (mail ($to, $subject,$txt,$headers)) {
$result='<div class="alert alert-success">Thank You! I will be in touch</div>';
} else {
$result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later</div>';
}
try it..
Ok so I am trying to create a contact form that validates user input to keep hackers from submitting codes and trying to require number, text, and email only. I have already styled the form and imported my php file. The contact from will send it to my gmail account. but everytime I test the php It allows for any type of data to be entered no matter if it is supposed to be a number and letters are submitted and the other way around. If I could get some help in telling me where I went wrong that would be great. I am a beginner at programming and only have the knowledge I recieved from school but I'm pretty good at html and css but having problems with the php validation. The form sends the email but like I said it allows any and all input.
<?php
$name = $_POST['name'];
$tel = $_POST['tel'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: www.webdesignheros.com';
$to = 'heenanwrk#gmail.com';
$subject = 'Service Email for HeenanTech';
$tel = filter_input(INPUT_POST, 'tel', FILTER_SANITIZE_INT);
$name = filter_input(INPUT_POST, 'name', FILTER_SANITIZE_STRING|FILTER_FLAG_NO_ENCODE_QUOTES);
$email = filter_input(INPUT_POST, 'email', FILTER_VALIDATE_EMAIL);
$message = filter_input(INPUT_POST, 'message', FILTER_SANITIZE_STRING|FILTER_FLAG_NO_ENCODE_QUOTES);
$body = "From: $name\n Phone: $tel\n Email: $email\n Message: $message\n";
?>
<?php
if ($_POST['submit']){
if(mail($to, $subject, $body, $from)){
echo'<p>Thank you for your email!</p>';
} else {
echo '<p> Oops! Something went wrong, try sending your message again</p>';
}
}
?>
Additionally the form can be found at [http://webdesignheros.com/Contact.html][1]
[1]: http://webdesignheros.com/Contact.html and if someone could tell me how to reject certain input before the submit that would be awesome too. like if an invalid entry was input and they move on to the next input it would reject it and not let the submit button be pushed. would i use the pattern="a-z" in the html or would i need to add javascript for that?
<?php
if (isset($_POST["submit")){
$name = $_POST["name"];
$tel = $_POST["tel"];
$email = $_POST["email"];
$message = $_POST["message"];
$from = "From: www.webdesignheros.com";
$to = "heenanwrk#gmail.com";
$subject = "Service Email for HeenanTech";
$body = "From: $name\n Phone: $tel\n Email: $email\n Message: $message\n";
mail($to, $subject, $body, $from);
if(mail($to, $subject, $body, $from)){
echo'<p>Thank you for your email!</p>';
} else {
echo '<p> Oops! Something went wrong, try sending your message again</p>';
}
}
?>
I made a PHP Contact Form using this tutorial and it works great, but I've encountered one potential security risk / inconvenience. Each email I receive comes from my admin login name.
I added $headers as this thread instructed, but to no avail.
My Current PHP:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];
$from = $_POST['email'];
$to = 'myClientsEmail#gmail.com';
$subject = 'Estimate Contact Form';
$headers = "From: $email\r\n"; /* I added this */
$headers .= "Reply-To: $email\r\n"; /* and this */
$body = "From: $name\n Phone: $phone\n E-Mail: $email\n Message:\n $message";
if ($_POST['submit']) {
if (mail ($to, $subject, $body, $from, $headers)) {
echo '<p>Your message has been sent!</p>';
} else {
echo '<p>Something went wrong, go back and try again!</p>';
}
}
?>
What exactly am I missing? Any help is greatly appreciated. Thank you!
Your mail() function call has an extra parameter it looks like. The correct mail() call should be:
if (mail($to, $subject,$body,$headers)) {
....
}
So just remove the $from portion and it should be good.
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);