php mail script

droctoganapus86

New Member
Messages
49
Reaction score
0
Points
0
hello,

I have a contact/email form made powered with php, and i'd like to have some extra things added. But i dont know anything about php, so who would like to help me? :biggrin:

the extra's are email attachment and maybe checkboxes so you can select one or more. also, if you can find something to prevent "flooding" (pressing the button 100 times) that would be nice.
I have this script :

Code:
<?php
session_start();
$mail=' '; //je email
include('validEmail.php');
if($_POST['send']){
	if(!validEmail($_POST['mail'])){
		$_SESSION['message']='Please fill in a correct Email-adress.';
	}
	elseif(trim($_POST['name'])==""){
		$_SESSION['message']='Please fill in your name.';
	}
	elseif(trim($_POST['message'])==""){
		$_SESSION['message']='Please type your message.';
	}
	else{
		$_SESSION['message']='Thanks, your message has been send.';
	}
	mail($mail,'Bericht van: '.trim($_POST['name']),$_POST['message'],'From: '.trim($_POST['name']).' <'.$_POST['mail'].'>');
}
?>
Code:
<form action="#" method="post">
	<label for="name">Name :</label><br />
	<input class="info" type="text" value="<?php echo($_POST['name'])?$_POST['name']:'';?>" name="name" id="name" />
	<label for="mail">E-mail :</label><br />
	<input class="info" type="text" value="<?php echo($_POST['mail'])?$_POST['mail']:'';?>" name="mail" id="mail" />
	<label for="message">Message :</label><br />
	<textarea class="info" name="message" id="message" cols="" rows="5" ><?php echo($_POST['message'])?$_POST['message']:'';?></textarea>
	<div><input class="send" type="submit" value="Send" name="send" /></div>
</form>
 
Last edited:

threedee

New Member
Messages
28
Reaction score
2
Points
0
Just wondering,

include('validEmail.php');

what is inside your validEmail.php? Or did I miss seeing that part here?
 

lichao891069

New Member
Messages
4
Reaction score
0
Points
0
hello,

I have a contact/email form made powered with php, and i'd like to have some extra things added. But i dont know anything about php, so who would like to help me? :biggrin:

the extra's are email attachment and maybe checkboxes so you can select one or more. also, if you can find something to prevent "flooding" (pressing the button 100 times) that would be nice.
I have this script :

Code:
<?php
session_start();
$mail=' '; //je email
include('validEmail.php');
if($_POST['send']){
    if(!validEmail($_POST['mail'])){
        $_SESSION['message']='Please fill in a correct Email-adress.';
    }
    elseif(trim($_POST['name'])==""){
        $_SESSION['message']='Please fill in your name.';
    }
    elseif(trim($_POST['message'])==""){
        $_SESSION['message']='Please type your message.';
    }
    else{
        $_SESSION['message']='Thanks, your message has been send.';
    }
    mail($mail,'Bericht van: '.trim($_POST['name']),$_POST['message'],'From: '.trim($_POST['name']).' <'.$_POST['mail'].'>');
}
?>
Code:
<form action="#" method="post">
    <label for="name">Name :</label><br />
    <input class="info" type="text" value="<?php echo($_POST['name'])?$_POST['name']:'';?>" name="name" id="name" />
    <label for="mail">E-mail :</label><br />
    <input class="info" type="text" value="<?php echo($_POST['mail'])?$_POST['mail']:'';?>" name="mail" id="mail" />
    <label for="message">Message :</label><br />
    <textarea class="info" name="message" id="message" cols="" rows="5" ><?php echo($_POST['message'])?$_POST['message']:'';?></textarea>
    <div><input class="send" type="submit" value="Send" name="send" /></div>
</form>
absolutely
 

droctoganapus86

New Member
Messages
49
Reaction score
0
Points
0
that's a script to validate email adresses. it checks some common mistakes, and then looks it up in the dns. or something, thats what they told me :p
 

rajat44

Banned
Messages
24
Reaction score
0
Points
0
its very easy! :)

<?php
// Contact subject
$subject ="Enter subject here";
// Details
$message="";

// Mail of sender
$mail_from=$_POST['mail'];
// From
$header="from: $name <$mail_from>";

// Enter your email address
$to ='someone@somewhere.com';

$send_contact=mail($to,$subject,$message,$header);

// Check, if message sent to your email
// display message "We've recived your information"
if($send_contact){
echo "We've recived your mail";
}
else {
echo "ERROR";
}
?>

you can get those variables from a form. for example, the email from part of the code.
in your html form in ur post, the email text fields 'name' is mail.

so, here- $mail_from=$_POST['mail'];

hope it helped.
 

rajat44

Banned
Messages
24
Reaction score
0
Points
0
<form method="POST" name="email_form_with_php"
action="php-form-action.php" enctype="multipart/form-data">

<label for='name'>Name: </label>
<input type="text" name="name" >

<label for='email'>Email: </label>
<input type="text" name="email" >

<label for='message'>Message:</label>
<textarea name="message"></textarea>

<label for='uploaded_file'>Select A File To Upload:</label>
<input type="file" name="uploaded_file">

<input type="submit" value="Submit" name='submit'>
</form>


the form will be something like this^


Now to get the uploaded file in the script:

//Get the uploaded file information
$name_of_uploaded_file =
basename($_FILES['uploaded_file']['name']);

//get the file extension of the file
$type_of_uploaded_file =
substr($name_of_uploaded_file,
strrpos($name_of_uploaded_file, '.') + 1);

$size_of_uploaded_file =
$_FILES["uploaded_file"]["size"]/1024;//size in KBs

The code above is getting the different attributes of the uploaded file from the $_FILES array.



Now, to validate file size:

//Settings
$max_allowed_file_size = 100; // size in KB
$allowed_extensions = array("jpg", "jpeg", "gif", "bmp");

//Validations
if($size_of_uploaded_file > $max_allowed_file_size )
{
$errors .= "\n Size of file should be less than $max_allowed_file_size";
}

//------ Validate the file extension -----
$allowed_ext = false;
for($i=0; $i<sizeof($allowed_extensions); $i++)
{
if(strcasecmp($allowed_extensions[$i],$type_of_uploaded_file) == 0)
{
$allowed_ext = true;
}
}

if(!$allowed_ext)
{
$errors .= "\n The uploaded file is not supported file type. ".
" Only the following file types are supported: ".implode(',',$allowed_extensions);
}


copying the file to an uploads folder:

//copy the temp. uploaded file to uploads folder
$path_of_uploaded_file = $upload_folder . $name_of_uploaded_file;
$tmp_path = $_FILES["uploaded_file"]["tmp_name"];

if(is_uploaded_file($tmp_path))
{
if(!copy($tmp_path,$path_of_uploaded_file))
{
$errors .= '\n error while copying the uploaded file';
}
}



and now, the mail-

$message = new Mail_mime();
$message->setTXTBody($text);
$message->addAttachment($path_of_uploaded_file);
$body = $message->get();
$extraheaders = array("From"=>$from, "Subject"=>$subject,"Reply-To"=>$visitor_email);
$headers = $message->headers($extraheaders);
$mail = Mail::factory("mail");
$mail->send($to, $headers, $body);
 
Top