Validation an Email in PHP? - php

<?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..

Related

how to reply to sender email using php contact form

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."

Allowing someone to reply to an email sent from a site with a php contact form

I have created a php contact form to allow someone to contact a company from a form on their website.
The code works fine and the email sends without a problem. The only problem is that when the email is sent and the company wish to replay to it, it sends to a strange email rather than to the email that the user entered within the contact form. Is there any way round this?
Here is the php code for the contact form.
<?php
if (isset($_POST["submit"])) {
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'site user';
$to = 'example#example.com';
$subject = 'Message from a site user';
$body ="From: $name\n E-Mail: $email\n Message:\n $message";
if (!$_POST['name']) {
$errorName = 'Please enter your name';
}
if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
$errorEmail = 'Please enter a valid email address';
}
if (!$_POST['message']) {
$errorMessage = 'Please enter your message';
}
if (!$errorName && !$errorEmail && !$errorMessage) {
if (mail ($to, $subject, $body, $from)) {
$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>';
}
}
}
?>
Add Reply To in your header,
Instead of passing $from as it is, do something like:
$from= 'From: webmaster#example.com' . "\r\n" .
'Reply-To: webmaster#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();

Validate full contact page

<?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.

PHP Unknown sender

Hello I am fairly new to PHP and do not know a lot at the moment. I have modified a contact form an have come into some problems regarding the mail going straight to junk.
I assume this is for the reason that (unknown sender) keeps displaying in the email header. I would appreciate it if someone could help me correct this. The following is the code that I have implemented into the website:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: Wirral PT Enquiry';
$to = 'joebloggs#hotmail.com';
$subject = 'Wirral PT Enquiry';
$human = $_POST['human'];
$headers = "enquiry#wirralpt.co.uk";
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
if ($_POST['submit']) {
if ($name != '' && $email != '') {
if ($human == '2') {
if (mail ($to, $subject, $body, $from)) {
echo '<p>Your message has been sent!</p>';
} else {
echo '<p>Something went wrong, go back and try again!</p>';
}
} else if ($_POST['submit'] && $human != '4') {
echo '<p> 1+1=2!! </p>';
}
} else {
echo '<p>You need to fill everything!!</p>';
}
}
?>
$from = 'From: Wirral PT Enquiry'; should contain the 'from' email address, not just the name:
$from = 'From: Wirral PT Enquiry <enquiry#wirralpt.co.uk>';
Try that?
try using
$headers = "Reply To :enquiry#wirralpt.co.uk";
Might work for you
also,
$headers = "From :enquiry#wirralpt.co.uk";
try both of these with you relevant email IDs
Change your headers to this:
$headers = 'From: enquiry#wirralpt.co.uk' . "\r\n" .
'Reply-To: enquiry#wirralpt.co.uk' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
and your mail should look this this:
mail ($to, $subject, $body, $headers)
This error can also be caused if while using SMTP settings for PhpMailer, $mail->IsSMTP(); is missed

PHP mailer Custom acknowledgement page?

I'm not a PHP guy so I googled and found some simple code for a contact form via kirupa.com Everything works great, however I'd love to serve up a custom thank you / acknowledgement page instead users getting a simple echo text "Data has been submitted to your#email.com!" on a white blank page.
My contact form:
http://leongaban.com/contact.html
Here is the PHP code:
<?php
if(isset($_POST['submit'])) {
$to = "your#email.com";
$subject = "Form My website";
$name_field = $_POST['name'];
$email_field = $_POST['email'];
$message = $_POST['message'];
$body = "From: $name_field\n E-Mail: $email_field\n Message:\n $message";
echo "Data has been submitted to $to!";
mail($to, $subject, $body);
} else {
echo "error! :'(";
}
?>
How should I edit this code to serve the users a thankyou.html page after they send an email?
if(isset($_POST['submit']))
{
$to = "your#email.com";
$subject = "Form My website";
$name_field = $_POST['name'];
$email_field = $_POST['email'];
$message = $_POST['message'];
$body = "From: $name_field\n E-Mail: $email_field\n Message:\n $message";
if(mail($to, $subject, $body))
{
header("Location: http://yourdomain/thankyoupage.php");
}
else
{
echo "Error !";
}
}
You could use header for redirecting to page:
if(mail($to, $subject, $body)) {
header("Location: http://your/thankyou/page/url");
exit;
}
else{
...
}
Did you mean something like this
use header function http://php.net/manual/en/function.header.php
<?php
if(isset($_POST['submit'])) {
$to = "your#email.com";
$subject = "Form My website";
$name_field = $_POST['name'];
$email_field = $_POST['email'];
$message = $_POST['message'];
$body = "From: $name_field\n E-Mail: $email_field\n Message:\n $message";
mail($to, $subject, $body);
header('Location: thankyou.html');
}
else {
echo "error! :'(";
}
?>

Categories