-
Notifications
You must be signed in to change notification settings - Fork 0
/
formsubmit.php
128 lines (126 loc) · 4.48 KB
/
formsubmit.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
<?php
// subject of the email to be sent.
// set the default subject here
// if there is a <input type="hidden" name="subject" value="My Custom Subject"> this will be used instead
$default_subject = "Zetaops iletisim";
// the to, cc, and bcc email addresses.
// seperate them by commas if you want to use multiple
$to="[email protected]"; //change this to your email
//$cc="[email protected]";
$bcc="";
// the default redirect.
// if you set <input type="hidden" name="redirect" value="foo.htm"> it will override this
$redirect = "tesekkur.php";
// required form fields, if any of these are missing then the
// form will not submit and an error message will be shown.
// set <input type="hidden" name="required" value="field1,field2,field3"> to override these
$required = array(
"name",
"email",
);
$email_title = "İletişim Formu Mesajı";
// these are the fields to ignore in the email that comes through
$ignorefields = array(
"submit",
"redirect",
"subject",
"email_to",
"required",
"submit_y",
"submit_x",
);
// names of the <input type=file> fields:
$uploadfiles = array(
"file_upload1",
"file_upload2",
"file_upload3",
);
function f_safe_string($s){
return preg_replace( '((?:\n|\r|\t|%0A|%0D|%08|%09)+)i' , '', $s);
}
// the default from email address that this form gets sent from.
// if the user specifies a "name" or "email" field then this will be used instead.
if($_REQUEST['name'] && $_REQUEST['email']){
$from = f_safe_string($_REQUEST['name']) . " <".f_safe_string($_REQUEST['email']).">";
}else{
$from="Contact Website <info@".$_SERVER['HTTP_HOST'].">";
}
// this part checks for required fields:
$missing = array();
if(isset($_REQUEST['required']) && strlen($_REQUEST['required'])){
$required = array();
foreach(explode(",",$_REQUEST['required']) as $field){
if(trim($field)){
$required [] = trim($field);
}
}
}
foreach($required as $req){
if(!$_REQUEST[$req]){
$missing [] = $req;
}
}
// this is the error message that gets displayed
// when they dont fill in all their required fields, wrap your design around this.
if(count($missing)){
header("HTTP/1.0 400 Bad Request");
die();
}
if($_REQUEST['subject'])$subject = f_safe_string($_REQUEST['subject']);
else $subject = $default_subject;
$boundary='--' . md5( uniqid("formsubmit_boundary") );
$charset="iso-8859-1";
$ctencoding="8bit";
$sep= chr(13) . chr(10);
$disposition="inline";
$header.="From: $from\n";
if($cc)$header.="CC: $cc\n";
if($bcc)$header.="BCC: $bcc\n";
$header.="Mime-Version: 1.0\nContent-Type: multipart/mixed;\n boundary=\"$boundary\"\n";
$header.="Content-Transfer-Encoding: $ctencoding\nX-Mailer: Php/libMailv1.3\r\n\r\n";
// build the message
$message = "This is a multi-part message in MIME format.\n--$boundary\n";
$message .= "Content-Type: text/html; charset=$charset\n";
$message .= "Content-Transfer-Encoding: $ctencoding\n\n";
$message .= "<h2>$email_title:</h2>";
$message .= '<table width="100%">';
foreach($_POST as $pkey => $value){
if((!in_array(strtolower($pkey),$ignorefields)) && (!in_array(strtolower($pkey),$uploadfiles))){
$key = '';
$val = '';
if(is_array($value)){
$key = ucwords(str_replace("_"," ",$pkey));
foreach($value as $k=>$v){
$val .= $k ." = " . $v . "<br>\n";
}
}else{
$key = ucwords(str_replace("_"," ",$pkey));
$val = $value . "\n";
}
$message .= '<tr style="border-bottom:1px solid #CCCCCC;"><td width="180"><b>' . $key . ':</b></td><td>' . $val . '</td></tr>';
}
}
$message .= '</table>';
$message .= "\n\n";
foreach($uploadfiles as $filetoupload){
if(is_file($_FILES[$filetoupload]['tmp_name'])){
if($_FILES[$filetoupload]['error'] == UPLOAD_ERR_OK) {
$filename = $_FILES[$filetoupload]['name'];
$filetype = $_FILES[$filetoupload]['type'];
$message .="--$boundary\nContent-type: $filetype ;\n name=\"$filename\"\n";
$message .="Content-Transfer-Encoding: base64\nContent-Disposition: $disposition;\n filename=\"$filename\"\n";
$linesz= filesize( $_FILES[$filetoupload]['tmp_name'])+1;
$fp= fopen( $_FILES[$filetoupload]['tmp_name'], 'r' );
$content2 = chunk_split(base64_encode(fread( $fp, $linesz)));
fclose($fp);
$message .= $sep.$content2;
}
}
}// end foreach file to upload
mail($to,$subject,$message,$header);
if(isset($_REQUEST['redirect'])){
$redirect = $_REQUEST['redirect'];
}
header("HTTP/1.1 201 Created");
exit;
?>