email validation and send email

the mail() function

Moderators: egami, macek, gesf

Post Reply
ny1234

Tue Jan 17, 2012 9:01 am

Hi,

I am trying to validate email then send the email if correct email. Below code is not working for me.

any help?

Thanks.


<?php
$errors = '';
$myemail = 'saf23@hotmail.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";
}
Else
mail("saf23@hotmail.com", "$subject", $message, "From:" . $email);
echo "Thank you for using our mail form";

?>


--i entered invalid email address but it cant verify that it is invalid. i was expecting error message. if valid email, then just send the email.

Thanks.

TheProdigyGuy
New php-forum User
New php-forum User
Posts: 215
Joined: Wed Dec 07, 2011 5:25 pm

Tue Jan 17, 2012 2:47 pm

Hello ny1234,
Just replace that

Code: Select all

preg_match(
"/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i",
$email_address))
With:

Code: Select all

if(!filter_var($email_address,FILTER_VALIDATE_EMAIL));
and it should work as expected.

ny1234

Wed Jan 18, 2012 9:43 am

Sorry. it didnt work.

Thanks.

TheProdigyGuy
New php-forum User
New php-forum User
Posts: 215
Joined: Wed Dec 07, 2011 5:25 pm

Wed Jan 18, 2012 11:44 am

ny1234,Dear you need understand that you have to create Simple HTML FORM firstly and then pass all your $_POST['message'] etc. to that PHP script.
Ofcourse it will not work for you without HTML Form.

Post Reply
  • Information
  • Who is online

    Users browsing this forum: No registered users and 1 guest