the mail() function
Moderators: macek, egami, gesf
by ninJo » Wed Dec 14, 2011 2:34 am
Hello, this is my code:
- Code: Select all
<?php
$name = $_REQUEST['name'] ;
$fam = $_REQUEST['fam'] ;
$email = $_REQUEST['email'] ;
$country = $_REQUEST['country'] ;
$tel = $_REQUEST['tel'] ;
$city = $_REQUEST['city'] ;
$message = $_REQUEST['message'] ;
$message = "
name: $name \n
last-name: $fam \n
mail: $email \n
country: $country \n
city: $city \n
phone: $tel \n
message: $message \n";
if($result == 'success')
{
mail( "my_mail@mail.com", "New message",
$message, "From: $email");
}
if ( ! empty($_POST['contact']))
{
$valid = array
(
'name' => array('/(.+){2,}/', 'Error.'),
'fam' => array('/(.+){2,}/', 'Error.'),
'tel' => array('/(.+){5,}/', 'Error'),
'city' => array('/(.+){3,}/', 'Error'),
'email' => array('/^[-_a-z0-9\'+*$^&%=~!?{}]++(?:\.[-_a-z0-9\'+*$^&%=~!?{}]+)*+@(?:(?![-.])[-a-z0-9.]+(?<![-.])\.[a-z]{2,6}|\d{1,3}(?:\.\d{1,3}){3})(?::\d++)?$/iD', 'Error.'),
'message' => array('/(.+){10,}/', 'Error.'),
);
$errors = array();
foreach ($valid as $field => $data)
{
$regex = $data[0];
$message = $data[1];
$input = trim($_POST[$field]);
if (empty($input) OR ! preg_match($regex, $input))
{
$errors += array($field => $message);
}
}
$result = empty($errors) ? 'success' : 'errors';
echo json_encode(array
(
'result' => $result,
'errors' => $errors,
));
exit;
}
?>
It's for this mailing form (
http://coreyworrell.com/blog/article/cr ... -ajax-form)
But it doesn't send mails.. What I am doing wrong?
-
ninJo
- New php-forum User

-
- Posts: 1
- Joined: Tue Nov 29, 2011 12:11 am
by TheProdigyGuy » Wed Dec 14, 2011 3:25 pm
Hi ninJo.
You have no HTML Submission form on that code.
Here is my way:
BTW,message format will be:
- Code: Select all
########################################
User IP: 192.168.0.1
Browser: Mozilla/5.0 (Windows NT xx; rv:x.x.x.x.x) Gecko/xxxxxx xxxxxx Chrome xxxxxx
Name: UFO
Surname: Anonymouse
Country: Mercury
City: not exist
Tel No: 00012121
Contact Mail Address: admin@localhost.com
Message: This is a first message!
Hey Humaaaaaaaans!
It Works!))
########################################
Here is :
- Code: Select all
<?php
error_reporting('off');
$page=<<<HTML
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Contact Form</title>
</head>
<body>
<center><br /><br /><br /><br /><br />
<form method="post" action="">
<label> Name*
<input type="text" name="Adiniz" id="Adiniz" />
</label>
<p>
<label> Surname*
<input type="text" name="Surname" id="Surname" />
</label>
</p>
<p>
<label> Country*
<input type="text" name="country" id="country" />
</label>
</p>
<p>
<label> City*
<input type="text" name="city" id="city" />
</label>
</p>
<p>
<label> Tel No*
<input type="text" name="tel" id="tel" />
</label>
</p>
<p>
<label> Email*
<input type="text" name="Email" id="Email" />
</label>
</p>
<p>
<label> Message *<br />
</label>
<label>
<textarea name="Body" cols="31" rows="10" wrap="virtual" id="Body"></textarea>
</label>
</p>
<p>
<input type="submit" name="submit" value="submit" />
</p>
</form>
</center>
</body>
</html>
HTML;
echo $page;
//echo count($_POST);
$yourmailaddr='test@mymail.com'; // Enter your Valid e-mail address here.
if (count($_POST)==0) die;
if (isset($_POST['Adiniz']) && isset($_POST['Surname']) && isset($_POST['country']) && isset($_POST['city']) && isset($_POST['tel']) &&isset($_POST['Email']) && isset($_POST['Body']) && !empty($_POST['Adiniz']) && !empty($_POST['Surname']) && !empty($_POST['country']) && !empty($_POST['city']) && !empty($_POST['tel']) && !empty($_POST['Email']) && !empty($_POST['Body']))
{
$adi=htmlspecialchars($_POST['Adiniz']);
$soyadi= htmlspecialchars($_POST['Surname']);
$country=htmlspecialchars($_POST['country']);
$city=htmlspecialchars($_POST['city']);
$telno=htmlspecialchars($_POST['tel']);
//!empty($_POST['country']) && !empty($_POST['city']) && !empty($_POST['tel'])
if (!filter_var(htmlspecialchars($_POST['Email']), FILTER_VALIDATE_EMAIL)) die('<script>alert("Your Mail Address is not Correct!");location.replace("?error");</script>');
$emailadresi=filter_var(htmlspecialchars($_POST['Email']), FILTER_VALIDATE_EMAIL);
$metni=htmlspecialchars($_POST['Body']);
if (@mail($yourmailaddr,"You've got NeW Feedback!",
str_repeat('#',40) . PHP_EOL .
'User IP: ' . $_SERVER['REMOTE_ADDR'] . PHP_EOL .
'Browser: ' . htmlspecialchars($_SERVER['HTTP_USER_AGENT']) .PHP_EOL .
'Name: ' . $adi . PHP_EOL .
'Surname: ' . $soyadi . PHP_EOL .
'Country: ' . $country . PHP_EOL .
'City: ' . $city . PHP_EOL .
'Tel No: ' . $telno . PHP_EOL .
'Contact Mail Address: ' . $emailadresi . PHP_EOL .
'Message: ' . wordwrap($metni,70) . PHP_EOL . str_repeat('#',40)).PHP_EOL)
{
die('<script>alert("Your FeedBack Sent! Thank You!");location.replace("?success");</script>');
}
else
{
die('<script>alert("Theris and error! Please try Again.");location.replace("?error");</script>');
}
}
else
{
die('<script>alert("Can not send your Feedback!.Reason: Incorrect Input!");location.replace("?error");</script>');
}
?>
-
TheProdigyGuy
- New php-forum User

-
- Posts: 215
- Joined: Wed Dec 07, 2011 5:25 pm
Return to PHP coding => Mail
Who is online
Users browsing this forum: No registered users and 2 guests