Content-Transfer-Encoding: 7bit - php

I have a problem here. I created a mail() PHP script to send attachments and you would normally send in your regular email provider. The problem is that I can only send files up to a certain number of characters. For example I can send a file with this name "New Text Document" but if I try to send a file with this name "New Document of Microsoft Word (3)" it never gets to my email.
Can someone please tell me why this happens?
error_reporting(-1);
if(empty($_POST) === false){
$errors = array();
$name = $_POST['name'];
$email = $_POST['email'];
$file = $_FILES['filename'];
if(empty($name) === true || empty($email) === true || empty($_POST['message']) === true){
$errors[] = 'Name, email and message are required!';
} else {
if(filter_var($email, FILTER_VALIDATE_EMAIL) === false){
$errors[] ='Not a valid email';
}
if(ctype_alpha($name) === false){
$errors[] ='Name must only contain letters';
}
}
$message;
if(empty($errors) === true){
if($_FILES['filename']['error'] == UPLOAD_ERR_OK){
$boundary = '-----' . md5(rand()) . '---';
$headers = array(
'MIME-Version: 1.0',
"Content-type: multipart/mixed; boundary=\"{$boundary}\"",
"From: {$email}"
);
$message = array(
"--{$boundary}",
'Content-type: text/html',
'Content-Transfer-Encoding: 7bit',
'',
chunk_split($_POST['message']),
"--{$boundary}",
"Content-type: {$file['type']}; name=\"{$file['name']}\"",
"Content-Disposition: attachment; filename=\"{$file['name']}\"",
'Content-Transfer-Encoding: base64',
'',
chunk_split(base64_encode(file_get_contents($file['tmp_name']))),
"--{$boundary}--"
);
$message = implode("\r\n", $message);
} else {
$headers = array(
"From: {$email}"
);
$message = &$_POST['message'];
}
//send email
var_dump(mail($email, 'Contact form', $message, implode("\r\n", $headers)));
echo $_FILES['filename']['name'];
/*//redirect user
header('Location: index.php?sent');
exit();*/
}
}
?
<!doctype html>
<html>
<head>
<title>A contact form</title>
</head>
<body>
<?php
if(isset($_GET['sent']) === true){
echo '<p>Thanks for contacting us</p>';
} else {
if(empty($errors) === false){
echo '<ul>';
foreach($errors as $error){
echo '<li>', $error ,'</li>';
}
echo '</ul>';
}
?>
<form action="" method="post" enctype="multipart/form-data">
<p>
<label for="name">Name:</label><br />
<input type="text" name="name" id="name" <?php if(isset($_POST['name']) === true){ echo 'value="', strip_tags($_POST['name']), '"';} ?>>
</p>
<p>
<label for="email">Email:</label><br />
<input type="text" name="email" id="email" <?php if(isset($_POST['email']) === true){ echo 'value="', strip_tags($_POST['email']), '"';} ?>>
</p>
<p>
<label for="message">Message:</label><br />
<textarea name="message" id="message"><?php if(isset($_POST['message']) === true){ echo strip_tags($_POST['message']); } ?></textarea>
</p>
<p>
List of files allowed: .pdf, .odt, .doc(x), xls(x), ppt(x), >xps, xml
</p>
<p>
<input type="file" name="filename">
</p>
<p>
<input type="submit" value="submit">
</p>
<?php
}
?>
</form>
</body>
</html>
It sends attachments but only if the file has for example the file has a name 10 characters, but if it has 15 it won't send

Related

Contact Form for my website not working in php [duplicate]

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 3 years ago.
I am trying to make a contact form for my website in php, but it seems something wrong somewhere, which i am unable to locate. Can you please help me to locate the error in the following code will be of great help and suggest further improvement on this.
Will of great help.
Thanks in advance.
<?php
// Message Vars
$msg = '';
$msgClass = '';
//check for submit
if (filter_has_var(INPUT_POST, 'submit')) {
//GET FORM DATA
$name = htmlspecialchars($_POST['name']);
$email = htmlspecialchars($_POST['email']);
$message = htmlspecialchars($_POST['message']);
// check required fields
if (!empty($email) && !empty($name) && !empty($message)) {
// passed
// check email
if (filter_var($email, FILTER_VALIDATE_EMAIL) == false) {
//failed
$msg = 'Please use a valid email';
$msgClass = 'alert-danger';
} else{
//passed
// reciepient email
$toEmail = 'myemail#gmail.com';
$subject = 'Contact Request From '.$name;
$body = '<h2> Contact Request</h2>
<h4> Name </h4><p>'.$name.'</p>
<h4> Email </h4><p>'.$email.'</p>
<h4> Message </h4><p>'.$message.'</p>';
// email headers
$headers = "MIME-Version: 1.0"."\r\n";
$headers .= "Content-Type: text/html;charset=UTF-8" . "\r\n";
// additional headers
$headers .= "From:" .$name. "<".$email.">". "\r\n";
if (mail($toEmail, $subject, $body, $headers)) {
// email sent
$msg = ' Your email has been sent';
$msgClass = 'alert-success';
}
}
# code...
} else {
//failed
$msg = ' Please fill in all fields';
$msgClass = 'alert-danger';
// failed
# code...
}
# code...
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Contact Us</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="container" >
<?php if ($msg != ''): ?>
<div class="alert <?php echo $msgClass; ?> "><?php echo $msg; ?></div>
<?php endif; ?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<div class="contact-form" >
<h1>Contact Us</h1>
<div class="txtb" >
<label>Name</label>
<input type="text"
name="name"
class="form-control"
value="<?php echo isset($_POST['name']) ? $name : ''; ?>">
</div>
<div class="txtb" >
<label>Email</label>
<input type="text"
name="email"
class="form-control"
value="<?php echo isset($_POST['email']) ? $email : ''; ?>" >
</div>
<div class="txtb" >
<label>Message</label>
<textarea name="message" class="form-control" ><?php echo isset($_POST['message']) ? $message : ''; ?></textarea>
</div>
<br>
<a type="submit" name="submit" class="btn" >Submit</a>
</form>
</div>
</body>
</html>
I don't know what type of the error you're seeing but there's a little thing to change
You need to change
<a type="submit" name="submit" class="btn" >Submit</a>
to
<input type="submit">

How can i solve Failing connect to mail server?

I am trying to create a working contact form, but this warning is showing when i submit form, Warning: mail(): Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set().How can i solve this issue ? can anybody help
The main code is here
<?php
if(empty($_POST) === false)
{
$errors = array();
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
if(empty($name) === true || empty($email) === true || empty($message) === true )
{
$errors[] = "Name, email and Message are required";
}
else if (filter_var($email, FILTER_VALIDATE_EMAIL) === false)
{
$errors[] = "This is not valid email address";
}
if (empty($errors)=== true)
{
$to = 'nobody#example.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: webmaster#example.com' . "\r\n" ;
mail($to, $subject, $message, $headers);
header('Location : index.php?sent');
exit();
} }
?>
<!DOCTYPE html>
<html>
<head>
<title>A contact Form</title>
</head>
<body>
<div class="container" style="width: 350px; margin: 0 auto;">
<?php
if(empty($errors) === false)
{
echo "<ul>";
foreach ($errors as $error) {
echo "<li>" .$error. "</li>";
}
echo "</ul>";
}
?>
<form action="" method="POST">
<p>
<label for ="name">Name:</label> </br>
<input type="text" name="name" id="name"
<?php if (isset($_POST['name']) === true)
{ echo 'value =" ', strip_tags($_POST['name']), '"'; } ?>>
</p>
<p>
<label for ="email">Email:</label> </br>
<input type="text" name="email" id="email"
<?php if (isset($_POST['email']) === true)
{ echo 'value =" ', strip_tags($_POST['email']), '"'; } ?>>
</p>
<p>
<label for ="message">Message:</label> </br>
<textarea name="message" id ="message">
<?php if (isset($_POST['message']) === true)
{ echo strip_tags($_POST['message']); } ?> </textarea>
</p>
<p>
<input type="submit" value="Submit">
</p>
</form>
</div>
</body>
</html>

Undefined variable Trying to fix my contact form

I have been trying to fix my contact form wherein the data can be sent via email. But i seem to have some errors at the start. It says in the web page "Undefined variable" yet. I'm only following a tutorial that i have been reading and i'm not yet adept in PHP. I'm using XAMPP at the moment in order to run my PHP
Here is the HTML Markup
<html>
<head>
<title>Contact Form</title>
<link rel="stylesheet" href="style.css" />
</head>
<body>
<h1>Contact Form</h1>
<p class="error"> There are some misisng fields.</p>
<?php if($error == true) { ?>
<?php } if($sent == true) { ?>
<p class="sent">Thank you for sending your message</p><?php } ?>
<div class="contactform">
<form name="contact" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<label for="name">Name:</label>
<input type="text" name="name" />
<label for="email">Email:</label>
<input type="email" name="email" />
<label for="comments">Comments:</label>
<textarea name="comments"></textarea>
<input type="submit" name="submit" class="submit" value="submit" />
</form>
</div>
Here is the PHP Code
<?php if($_POST['submit']) {
if(empty($_POST['name']) || empty($_POST['email']) || empty($_POST['comments'])) {
$error = true;
} else {
$to = "clestcruz#gmail.com";
$name = trim($_POST['name']);
$email = trim($_POST['email']);
$comments = trim($_POST['comments']);
$subject = "Contact Form";
$messages = "Name: $name \r\n Email: $email \r\n Comments: $comments";
$headers = "From:" . $name;
$mailsent = mail($to, $subject, $messages, $headers);
if($mailsent){
$sent= true;
}
}
}
?>
</body>
</html>
Undefine Variables
<?php if($error == true) { ?>
<?php } if($sent == true) { ?>
if($_POST['submit']) {
Try declaring the variables before you use it. PHP will give out a notice if you don't pass a value first.
<?php
$error=false;
$sent=false;
if(isset($_POST['submit'])) {
if(empty($_POST['name']) || empty($_POST['email']) || empty($_POST['comments'])) {
$error = true;
} else {
$to = "clestcruz#gmail.com";
$name = trim($_POST['name']);
$email = trim($_POST['email']);
$comments = trim($_POST['comments']);
$subject = "Contact Form";
$messages = "Name: $name \r\n Email: $email \r\n Comments: $comments";
$headers = "From:" . $name;
$mailsent = mail($to, $subject, $messages, $headers);
if($mailsent){
$sent= true;
}
}
}
?>
These lines:
<?php if($error == true) { ?>
<?php } if($sent == true) { ?>
appear near the top of your HTML, but as far as I can see, there's been no PHP executed at this point, so $error and $sent won't be defined.
This line:
if($_POST['submit']) {
is testing for a value, but unless your form has been submitted, it too won't be defined. You could test this more effectively with
if (isset($_POST['submit'])) {
// do stuff
}

Add input type=file element in my PHP

I have a form. With the following:
HTML:
<form name="feedback_form" method="post" action="" class="feedback_form">
<input type="text" name="field-name" value="İsim" title="İsim" class="field-name form_field">
<input type="text" name="field-email" value="Email" title="Email" class="field-email form_field">
<input type="text" name="field-subject" value="Başlık" title="Başlık" class="field-subject form_field">
<textarea name="field-message" cols="45" rows="5" title="Mesajınız..." class="field-message form_field">Mesajınız...</textarea>
<label for='uploaded_file'>Fotoğrafınızı Yükleyin:</label>
<input type="file" name="field-file" value="File">
<br>
<input type="reset" name="reset" id="reset2" value="Temizle" class="feedback_reset">
<input type="button" name="submit" class="feedback_go" id="submit2" value="Gönder">
</form>
PHP:
<?php
header('Content-Type: text/html; charset=utf-8');
function sendFeedback($feedback_email, $feedback_msg, $feedback_name, $feedback_subject, $feedback_file) {
/* EDIT THIS */
$admin_email = "mymail#gmail.com";
if ($feedback_subject == "Subject" || empty($feedback_subject) ) {
$subj = "Email from your site";
} else {
$subj = $feedback_subject;
}
/* //EDIT THIS */
$message = "
<html>
<head>
<title>Websitenizin emaili</title>
</head>
<body>
<p><a href='mailto:".$feedback_email."'>".$feedback_name."</a> send this message:</p>
<p>".$feedback_msg."</p>
<p>".$subject."</p>
</body>
</html>
";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=UTF-8' . "\r\n";
if ($feedback_name!=="Name" && $feedback_email!=="Email" && !empty($feedback_email) && !empty($feedback_msg) && !empty($feedback_name) ) {
if ($feedback_email == "mail_error") {
echo "<span class='ajaxok'>Geçersiz email adresi.</span>";
} else {
mail($admin_email, $subj, $message, $headers);
echo "<span class='ajaxok'>Teşekkürler. Mesajınız gönderilmiştir.</span>";
}
} else {
echo "<span class='ajaxalert'>Lütfen zorunlu alanları doldurunuz.</span>";
}
}
sendFeedback($_POST['email'], $_POST['message'], $_POST['name'], $_POST['subject'], $_POST['file']);
?>
When send a message on this form, send email, working. But only subject, message, name and email. I want add image upload in this php code. But i don't know how can i do it? Please help me.
In the form you have to put enctype="multipart/form-data".
In your php file you can access the file via $_FILES['file'];
Then take a look at this tutorial

PHP, trying to add captcha to existing form

I am not a PHP programmer, but have used it a touch, enough to put in a contact form. However, I am trying to add a captcha field, which now works but the form does not validate it - so it submits no matter what
Can anybody help please? sorry if the code is messy and thanks in advance
code at the top of my page
<?php session_start() ?>
<?php
//If the form is submitted
if(isset($_POST['submit'])) {
//Check to make sure that the name field is not empty
if(trim($_POST['name']) == '') {
$hasError = true;
} else {
$name = trim($_POST['name']);
}
//Check to make sure that the subject field is not empty
if(trim($_POST['subject']) == '') {
$hasError = true;
} else {
$subject = trim($_POST['subject']);
}
//Check to make sure sure that a valid email address is submitted
if(trim($_POST['email']) == '') {
$hasError = true;
} else if (!eregi("^[A-Z0-9._%-]+#[A-Z0-9._%-]+\.[A-Z]{2,4}$", trim($_POST['email'])))
{
$hasError = true;
} else {
$email = trim($_POST['email']);
}
//Check to make sure comments were entered
if(trim($_POST['message']) == '') {
$hasError = true;
} else {
if(function_exists('stripslashes')) {
$message = stripslashes(trim($_POST['message']));
} else {
$message = trim($_POST['message']);
}
/*captcha 2*/
if(isset($_POST["captcha"])) {
$hasError = true;
} else {
if($_SESSION["captcha"]==$_POST["captcha"]) {
}
}
//CAPTHCA is valid; proceed the message: save to database, send by e-mail ...
//If there is no error, send the email
if(!isset($hasError)) {
$emailTo = 'email address'; //Put your own email address here
$emailTo = 'email address'; //Put your own email address here
$body = "Name: $name \n\nEmail: $email \n\nSubject: $subject \n\nMessage:\n $message";
$headers = 'From: website form <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' .
$email;
mail($emailTo, $subject, $body, $headers);
$emailSent = true;
}
}
?>
Code in the form:
[php]<?php if(isset($hasError)) { //If errors are found ?>
<p class="error">Please check if you've filled all the fields with valid information. Thank you.</p>
<?php } ?>
<?php if(isset($emailSent) && $emailSent == true) { //If email is sent ?>
<p><strong>Email Successfully Sent!</strong></p>
<p>Thank you <strong><?php echo $name;?></strong> for contacting us. Your email was successfully sent and we will be in touch with you soon.</p>
<?php } ?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" id="contactform">
<div>
<p>
<label for="name">Name</label><br />
<input type="text" name="name" value="" id="name" class="required">
</p>
</div>
<div>
<p>
<label for="email">Email</label><br />
<input type="text" name="email" value="" id="email" class="required">
</p>
</div>
<div>
<p>
<label for="subject">Subject</label><br />
<input type="text" name="subject" value="" id="subject" class="required">
</p>
</div>
<div style="margin-bottom:25px;">
<p>
<label for="message">Message</label><br />
<textarea rows="5" name="message" value="" id="message" class="required"></textarea>
</p>
</div>
<div style="margin-bottom:25px;">
<img src="captcha.php" alt="captcha image">
<p>
<label for="captcha">(antispam code, 3 black symbols)</label><br />
<input type="text" name="captcha" maxlength="3" id="captcha" class="required">
</p>
</div>
<input type="submit" value="Send Message" name="submit" />
</form>
[/php]
[edit ] 2011-12-20 8:22pm CST - updated the second block of code with the final code that the OP is using - based on off site chat.
There's a better way to write the code. I'm putting an example of this below. Ask questions and I'll update the code with comments explaining. I revamped the if statement you had for the captcha so that it didn't need a double if. Using || (or) in the if statement causes PHP to stop after testing the first condition (if the first condition evaluates to true). Therefore, if the variable is not set it never moves on to the comparison of POST with SESSION.
Also, I defaulted your hasError variable to false, and am testing for the boolean value. This is better because it makes sense. Think about the programmers who will come after you. If it makes sense, it'll be easier to work with. You might be that programmer :)
[edited to add session_start();]
<?php
session_start();
// default value
$hasError = false;
//If the form is submitted
if(isset($_POST['submit'])) {
//Check to make sure that the name field is not empty
if(trim($_POST['name']) == '') {
$hasError = true;
} else {
$name = trim($_POST['name']);
}
//Check to make sure that the subject field is not empty
if(trim($_POST['subject']) == '') {
$hasError = true;
} else {
$subject = trim($_POST['subject']);
}
//Check to make sure sure that a valid email address is submitted
if(trim($_POST['email']) == '') {
$hasError = true;
} else if (!eregi("^[A-Z0-9._%-]+#[A-Z0-9._%-]+\.[A-Z]{2,4}$", trim($_POST['email']))) {
$hasError = true;
} else {
$email = trim($_POST['email']);
}
//Check to make sure comments were entered
if( trim($_POST['message']) == '') {
$hasError = true;
} else {
if(function_exists('stripslashes')) {
$message = stripslashes(trim($_POST['message']));
} else {
$message = trim($_POST['message']);
}
}
if( ! isset( $_POST["captcha"] ) || $_SESSION["captcha"] != $_POST["captcha"] ) {
$hasError = true;
echo 'CAPTHCA is not valid; ignore submission<br>';
echo $_POST['captcha' . ' != ' . $_SESSION['captcha'] . '<br>';
}
//If there is no error, send the email
if( $hasError == false ) {
$emailTo = 'email#email.com'; //Put your own email address here
$emailTo = 'email#email.com'; //Put your own email address here
$body = "Name: $name \n\nEmail: $email \n\nSubject: $subject \n\nMessage:\n $message";
// !!!!!!!!!!!!!!!! REMOVE \r\n from $emailTo or your form will be hacked !!!!!!!!!!!!!!!!!!!!!!
$headers = 'From: website form <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $email;
mail($emailTo, $subject, $body, $headers);
$emailSent = true;
} else {
}
}
[edit - full code, edited and (hopefully) improved]
<?php
session_start();
function clean_for_email( $inbound )
{
return str_replace( array( "\n", "\r" ), "", $inbound );
}
// I really like the name of this function. :D
function outputInput( $name, $required )
{
$attribs[] = "name=\"{$name}\"";
$attribs[] = "id=\"{$name}\"";
$attribs[] = $required?'class="required"':'';
$attribs[] = 'type="text"';
if ( count( $_POST ) && array_key_exists( $name, $_POST ) )
{
$attribs[] = 'value="' . htmlspecialchars( $_POST[$name] ) . '"';
}
echo '<input ' . implode( ' ', $attribs ) . '>';
}
//------------------------------------------------------------------------
function outputTextarea( $name, $required, $rows = 5 )
{
$attribs[] = "name=\"{$name}\"";
$attribs[] = "id=\"{$name}\"";
$attribs[] = $required?'class="required"':'';
$attribs[] = 'rows="5"';
$value = '';
if ( count( $_POST ) && array_key_exists( $name, $_POST ) )
{
$value = htmlspecialchars( $_POST[$name] );
}
echo '<textarea ' . implode( ' ', $attribs ) . '>' . $value . '</textarea>';
}
// default value
$hasError = false;
$emailSent = false;
//If the form is submitted
if( count( $_POST ) && isset($_POST['submit'] ) ) {
//Check to make sure that the name field is not empty
if(trim($_POST['name']) == '') {
$hasError = true;
} else {
$name = trim($_POST['name']);
}
//Check to make sure that the subject field is not empty
if(trim($_POST['subject']) == '') {
$hasError = true;
} else {
$subject = trim($_POST['subject']);
}
//Check to make sure sure that a valid email address is submitted
if(trim($_POST['email']) == '') {
$hasError = true;
} else if ( ! preg_match( '/^.+#.+$/i', trim( $_POST['email'] ) ) ) {
$hasError = true;
} else {
$email = trim($_POST['email']);
}
//Check to make sure comments were entered
if( trim($_POST['message']) == '') {
$hasError = true;
} else {
if(function_exists('stripslashes')) {
$message = stripslashes(trim($_POST['message']));
} else {
$message = trim($_POST['message']);
}
}
if ( ! array_key_exists( 'captcha', $_POST ) || $_SESSION['captcha'] != $_POST["captcha"] ) {
$hasError = true;
}
if( ! $hasError )
{
$captchaValid = true;
//If there is no error, send the email
if( $hasError == false ) {
$emailTo = 'xxx'; //Put your own email address here
$body = "Name: $name \n\nEmail: $email \n\nSubject: $subject \n\nMessage:\n $message";
$headers = 'From: website form <'.clean_for_email( $emailTo ).'>' . "\r\n" . 'Reply-To: ' . clean_for_email( $email );
mail($emailTo, $subject, $body, $headers);
$emailSent = true;
} else {
}
}
}
?>
<? if( $hasError ) : ?>
<p class="error">Please check if you've filled all the fields with valid information Thank you.</p>
<? endif; ?>
<? if( $emailSent == true) : ?>
<p><strong>Email Successfully Sent!</strong></p>
<p>Thank you <strong><?php echo $name;?></strong> for contacting us. Your email was successfully sent and we will be in touch with you soon.</p>
<? endif; ?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" id="contactform">
<div>
<p>
<label for="name">Name</label><br />
<? outputInput( 'name', true ); ?>
</p>
</div>
<div>
<p>
<label for="email">Email</label><br />
<? outputInput( 'email', true ); ?>
</p>
</div>
<div>
<p>
<label for="subject">Subject</label><br />
<? outputInput( 'subject', true ); ?>
</p>
</div>
<div style="margin-bottom:25px;">
<p>
<label for="message">Message</label><br />
<? outputTextarea( 'message', true ); ?>
</p>
</div>
<div style="margin-bottom:25px;">
<img src="captcha.php" alt="captcha image">
<p>
<label for="captcha">(antispam code, 3 black symbols)</label><br />
<? outputInput( 'captcha', true ); ?>
</p>
</div>
<input type="submit" value="Send Message" name="submit" />
</form>
if(isset($_POST["captcha"]))
You're missing a bracket.
Edited to show entire code.... Brackets added for captcha conditionals which were missing. As is, your code did not check if the captcha was set via post. It was only checking the session variable against the post variable. If both were blank, the form would mail. You may still have issues with captcha.php or the session variable.
<?php
//If the form is submitted
if(isset($_POST['submit'])) {
//Check to make sure that the name field is not empty
if(trim($_POST['name']) == '') {
$hasError = true;
} else {
$name = trim($_POST['name']);
}
//Check to make sure that the subject field is not empty
if(trim($_POST['subject']) == '') {
$hasError = true;
} else {
$subject = trim($_POST['subject']);
}
//Check to make sure sure that a valid email address is submitted
if(trim($_POST['email']) == '') {
$hasError = true;
} else if (!eregi("^[A-Z0-9._%-]+#[A-Z0-9._%-]+\.[A-Z]{2,4}$", trim($_POST['email'])))
{
$hasError = true;
} else {
$email = trim($_POST['email']);
}
//Check to make sure comments were entered
if(trim($_POST['message']) == '') {
$hasError = true;
} else {
if(function_exists('stripslashes')) {
$message = stripslashes(trim($_POST['message']));
} else {
$message = trim($_POST['message']);
}
}
/*captcha 2*/
if(isset($_POST["captcha"])) {
if($_SESSION["captcha"]==$_POST["captcha"])
{
//CAPTHCA is valid; proceed the message: save to database, send by e-mail ...
//If there is no error, send the email
if(!isset($hasError)) {
$emailTo = 'enquiries#sjbprojects.com'; //Put your own email address here
$emailTo = 'sjbullen#gmail.com'; //Put your own email address here
$body = "Name: $name \n\nEmail: $email \n\nSubject: $subject \n\nMessage:\n $message";
$headers = 'From: SJB Projects website form <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $email;
mail($emailTo, $subject, $body, $headers);
$emailSent = true;
}
else
{
echo 'CAPTHCA is not valid; ignore submission';
}
}
} else {
///message here if CAPTCHA is not set (via post)
}
}
?>
<?php if(isset($hasError)) { //If errors are found ?>
<p class="error">Please check if you've filled all the fields with valid information. Thank you.</p>
<?php } ?>
<?php if(isset($emailSent) && $emailSent == true) { //If email is sent ?>
<p><strong>Email Successfully Sent!</strong></p>
<p>Thank you <strong><?php echo $name;?></strong> for contacting SJB Projects. Your email was successfully sent and we will be in touch with you soon.</p>
<?php } ?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" id="contactform">
<div>
<p>
<label for="name">Name</label><br />
<input type="text" name="name" value="" id="name" class="required">
</p>
</div>
<div>
<p>
<label for="email">Email</label><br />
<input type="text" name="email" value="" id="email" class="required">
</p>
</div>
<div>
<p>
<label for="subject">Subject</label><br />
<input type="text" name="subject" value="" id="subject" class="required">
</p>
</div>
<div style="margin-bottom:25px;">
<p>
<label for="message">Message</label><br />
<textarea rows="5" name="message" value="" id="message" class="required"></textarea>
</p>
</div>
<div style="margin-bottom:25px;">
<img src="captcha.php" alt="captcha image">
<p>
<label for="captcha">(antispam code, 3 black symbols)</label><br />
<input type="text" name="captcha" maxlength="3" id="captcha" class="required">
</p>
</div>
<input type="submit" value="Send Message" name="submit" />
</form>

Categories