email-met-meerdere-attachments
Gesponsorde koppelingen
PHP script bestanden
Code (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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
<?php
//Toegestane MIME-typen
$allowed=array('image/x-png','image/jpeg','image/bmp','image/pjpeg','text/plain','text/html','image/png','image/gif','application/x-js','application/x-shockwave-flash');
//Maximale aantal bijlagen. Wordt overgenomen door JavaScript file input en ook server sided geverifieerd
$max_atts=3;
function validate($param)
{
if(ereg("^[^@]{1,64}@[^@]{1,255}$",$param))
{
$addr=explode("@",$param);
$local=explode(".",$addr[0]);
for($i=0;$i<sizeof($local);$i++)
{
if(!ereg("^(([A-Za-z0-9!#$%&'*+/=?^_`{|}~-][A-Za-z0-9!#$%&'*+/=?^_`{|}~\.-]{0,63})|(\"[^(\\|\")]{0,62}\"))$",$local[$i]))
return 0;
}
if(!ereg("^\[?[0-9\.]+\]?$",$addr[1]))
{
$domain=explode(".",$addr[1]);
if(sizeof($domain)<2)
return 0;
for($i=0;$i<sizeof($domain);$i++)
{
if(!ereg("^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|([A-Za-z0-9]+))$",$domain[$i]))
return 0;
}
}
return 1;
}
else
return 0;
}
function verify_field()
{
$err="";
if(validate($_POST['send_mail'])==0)
{
$err.="<li>Geef een correct geformuleerd e-mailadres</li>\n";
$error=1;
}
$recipients=explode(",",$_POST['rec_name']);
for($i=0;$i<sizeof($recipients);$i++)
{
if(validate($recipients[$i])==0)
{
$err.="<li>Geef 1 of meerdere correct geformuleerde ontvangers op, gescheiden door een komma</li>\n";
$error=1;
}
}
if($err!="")
{
echo($err);
return false;
}
else
return true;
}
function geterror($id)
{
switch($id)
{
case '1':
return 'Het bestand is te groot';
break;
case '2':
return 'Het bestand is groter dan gespecificeerd in HTML form MAX_FILE_SIZE';
break;
case '3':
return 'Het bestand is gedeeltelijk geupload';
break;
case '4':
return 'Er is geen upload aangekomen';
break;
}
}
function getstring($charno)
{
switch($charno)
{
case '0':
return 'text/plain';
break;
case '1':
return 'text/html';
break;
default:
return 'text/plain';
break;
}
}
function getcharset($charno)
{
switch($charno)
{
case '0':
return 'us-ascii';
break;
case '1':
return 'utf-8';
break;
default:
return 'us-ascii';
break;
}
}
if(!empty($_POST))
{
if(verify_field()==true)
{
$header="From: ".addslashes($_POST['send_name'])." <".addslashes($_POST['send_mail']).">\r\n";
$header.="To: ".$_POST['rec_name']."\r\n";
if(strlen($_POST['subject'])>0)
$header.="Subject: ".$_POST['subject']."\r\n";
$header.="Return-Path: <".$_POST['send_mail'].">\r\n";
$header.="Mime-Version: 1.0\r\n";
if(!empty($_FILES))
{
$err_files="";
$header.="Content-Type: multipart/mixed; boundary=\"grensje\"\r\n\r\n";
$body="--grensje\r\nContent-Type: ".getstring($_POST['format'])."; charset=\"".getcharset($_POST['charset'])."\"\r\n\r\n";
$body.=$_POST['msg']."\r\n\r\n";
if(($num_files=count($_FILES))<=$max_atts)
{
while(list($key,$val)=each($_FILES))
{
if(empty($_FILES[$key]['name']))
{
$err_files.="<li>U hebt uploadveld ".$key." leeggelaten</li>\n";
$illegal=1;
}
else if($_FILES[$key]['error']!=0)
{
$err_files.="<li>Er is een fout opgetreden bij het uploaden van ".$_FILES[$key]['name']."<br>".geterror($_FILES[$key]['error'])."</li>\n";
$illegal=1;
}
else
{
if(!in_array($_FILES[$key]['type'],$allowed))
{
$err_files.="<li>Illegaal bestandtype in ".$_FILES[$key]['name'].": ".$_FILES[$key]['type']."</li>\n";
$illegal=1;
}
else
{
$body.="--grensje\r\nContent-Type: ".$_FILES[$key]['type']."\r\n";
$body.="Content-Disposition: attachment; filename=\"".$_FILES[$key]['name']."\"\r\n";
$body.="Content-Transfer-Encoding: base64\r\n\r\n";
$body.=base64_encode(file_get_contents($_FILES[$key]['tmp_name']))."\r\n\r\n";
}
}
}
}
else
{
$err_files.="<li>Meer bestanden geupload dan toegestaan (".$num_files." tegenover ".$max_atts." toegestane bestanden)</li>\n";
$illegal=1;
}
$body.="--grensje--";
}
else
$header.="Content-Type: ".getstring($_POST['format'])."; charset=\"".getstring($_POST['charset'])."\"\r\n\r\n";
if(isset($illegal))
echo($err_files);
else
{
if(!mail($_POST['rec_name'],$_POST['subject'],$body,$header))
echo("<li>Er is een fout opgetreden bij het verzenden van het bericht</li>\n");
else
echo("<li>Bericht verzonden</li>\n");
}
}
}
?>
<html>
<head>
<script language="JavaScript">
var counter=0;
var dis=0;
var max=<?php echo($max_atts); ?>;
var fileArr=new Array();
function addFile()
{
if(dis<max)
{
document.getElementById('lijst').innerHTML+="<span id='"+counter+"'><input type='file' name='input"+counter+"'><input type='button' value='x' onclick=\"javascript:delFile("+counter+")\"><br></span>";
counter++;
dis++;
}
else
alert("U mag maximaal "+max+" bestanden uploaden");
}
function delFile(id)
{
document.getElementById(id).innerHTML="";
fileArr[id]=null;
dis--;
}
</script>
</head>
<body>
<form method="post" action="<?php echo($_SERVER['PHP_SELF']); ?>" enctype="multipart/form-data">
<table>
<tr>
<td>Uw naam </td>
<td>:</td>
<td><input name="send_name" type="text" id="send_name" /></td>
</tr>
<tr>
<td>Uw e-mailadres </td>
<td>:</td>
<td><input name="send_mail" type="text" id="send_mail" /></td>
</tr>
<tr>
<td>Naar</td>
<td>:</td>
<td><input name="rec_name" type="text" id="rec_name" /></td>
</tr>
<tr>
<td>Onderwerp</td>
<td>:</td>
<td><input name="subject" type="text" id="subject" /></td>
</tr>
<tr>
<th colspan="3">Bericht</th>
</tr>
<tr>
<td colspan="3"><div align="center">
<textarea name="msg" cols="35" rows="5" id="msg"></textarea>
</div></td>
</tr>
<tr>
<td colspan="3" align="center"><a id="hyper" href="javascript:addFile()">Bijlageveld toevoegen</a></td>
</tr>
<tr>
<td colspan="3" align="center"><div id='lijst'></div></td>
</tr>
<tr>
<th colspan="3">Formaat</th>
</tr>
<tr>
<td colspan="3"><input name="format" type="radio" value="0" checked="checked" />
Gewone tekst </td>
</tr>
<tr>
<td colspan="3"><input name="format" type="radio" value="1" />
HTML</td>
</tr>
<tr>
<th colspan="3">Tekenset</th>
</tr>
<tr>
<td colspan="3"><input name="charset" type="radio" value="0" checked="checked" />
us-ascii</td>
</tr>
<tr>
<td colspan="3"><input name="charset" type="radio" value="1" />
utf-8</td>
</tr>
<tr>
<td colspan="3"><div align="center">
<input type="reset" name="Reset" value="Opnieuw" />
<input type="submit" name="Submit2" value="Versturen" />
</div></td>
</tr>
</table>
</form>
</body>
</html>
//Toegestane MIME-typen
$allowed=array('image/x-png','image/jpeg','image/bmp','image/pjpeg','text/plain','text/html','image/png','image/gif','application/x-js','application/x-shockwave-flash');
//Maximale aantal bijlagen. Wordt overgenomen door JavaScript file input en ook server sided geverifieerd
$max_atts=3;
function validate($param)
{
if(ereg("^[^@]{1,64}@[^@]{1,255}$",$param))
{
$addr=explode("@",$param);
$local=explode(".",$addr[0]);
for($i=0;$i<sizeof($local);$i++)
{
if(!ereg("^(([A-Za-z0-9!#$%&'*+/=?^_`{|}~-][A-Za-z0-9!#$%&'*+/=?^_`{|}~\.-]{0,63})|(\"[^(\\|\")]{0,62}\"))$",$local[$i]))
return 0;
}
if(!ereg("^\[?[0-9\.]+\]?$",$addr[1]))
{
$domain=explode(".",$addr[1]);
if(sizeof($domain)<2)
return 0;
for($i=0;$i<sizeof($domain);$i++)
{
if(!ereg("^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|([A-Za-z0-9]+))$",$domain[$i]))
return 0;
}
}
return 1;
}
else
return 0;
}
function verify_field()
{
$err="";
if(validate($_POST['send_mail'])==0)
{
$err.="<li>Geef een correct geformuleerd e-mailadres</li>\n";
$error=1;
}
$recipients=explode(",",$_POST['rec_name']);
for($i=0;$i<sizeof($recipients);$i++)
{
if(validate($recipients[$i])==0)
{
$err.="<li>Geef 1 of meerdere correct geformuleerde ontvangers op, gescheiden door een komma</li>\n";
$error=1;
}
}
if($err!="")
{
echo($err);
return false;
}
else
return true;
}
function geterror($id)
{
switch($id)
{
case '1':
return 'Het bestand is te groot';
break;
case '2':
return 'Het bestand is groter dan gespecificeerd in HTML form MAX_FILE_SIZE';
break;
case '3':
return 'Het bestand is gedeeltelijk geupload';
break;
case '4':
return 'Er is geen upload aangekomen';
break;
}
}
function getstring($charno)
{
switch($charno)
{
case '0':
return 'text/plain';
break;
case '1':
return 'text/html';
break;
default:
return 'text/plain';
break;
}
}
function getcharset($charno)
{
switch($charno)
{
case '0':
return 'us-ascii';
break;
case '1':
return 'utf-8';
break;
default:
return 'us-ascii';
break;
}
}
if(!empty($_POST))
{
if(verify_field()==true)
{
$header="From: ".addslashes($_POST['send_name'])." <".addslashes($_POST['send_mail']).">\r\n";
$header.="To: ".$_POST['rec_name']."\r\n";
if(strlen($_POST['subject'])>0)
$header.="Subject: ".$_POST['subject']."\r\n";
$header.="Return-Path: <".$_POST['send_mail'].">\r\n";
$header.="Mime-Version: 1.0\r\n";
if(!empty($_FILES))
{
$err_files="";
$header.="Content-Type: multipart/mixed; boundary=\"grensje\"\r\n\r\n";
$body="--grensje\r\nContent-Type: ".getstring($_POST['format'])."; charset=\"".getcharset($_POST['charset'])."\"\r\n\r\n";
$body.=$_POST['msg']."\r\n\r\n";
if(($num_files=count($_FILES))<=$max_atts)
{
while(list($key,$val)=each($_FILES))
{
if(empty($_FILES[$key]['name']))
{
$err_files.="<li>U hebt uploadveld ".$key." leeggelaten</li>\n";
$illegal=1;
}
else if($_FILES[$key]['error']!=0)
{
$err_files.="<li>Er is een fout opgetreden bij het uploaden van ".$_FILES[$key]['name']."<br>".geterror($_FILES[$key]['error'])."</li>\n";
$illegal=1;
}
else
{
if(!in_array($_FILES[$key]['type'],$allowed))
{
$err_files.="<li>Illegaal bestandtype in ".$_FILES[$key]['name'].": ".$_FILES[$key]['type']."</li>\n";
$illegal=1;
}
else
{
$body.="--grensje\r\nContent-Type: ".$_FILES[$key]['type']."\r\n";
$body.="Content-Disposition: attachment; filename=\"".$_FILES[$key]['name']."\"\r\n";
$body.="Content-Transfer-Encoding: base64\r\n\r\n";
$body.=base64_encode(file_get_contents($_FILES[$key]['tmp_name']))."\r\n\r\n";
}
}
}
}
else
{
$err_files.="<li>Meer bestanden geupload dan toegestaan (".$num_files." tegenover ".$max_atts." toegestane bestanden)</li>\n";
$illegal=1;
}
$body.="--grensje--";
}
else
$header.="Content-Type: ".getstring($_POST['format'])."; charset=\"".getstring($_POST['charset'])."\"\r\n\r\n";
if(isset($illegal))
echo($err_files);
else
{
if(!mail($_POST['rec_name'],$_POST['subject'],$body,$header))
echo("<li>Er is een fout opgetreden bij het verzenden van het bericht</li>\n");
else
echo("<li>Bericht verzonden</li>\n");
}
}
}
?>
<html>
<head>
<script language="JavaScript">
var counter=0;
var dis=0;
var max=<?php echo($max_atts); ?>;
var fileArr=new Array();
function addFile()
{
if(dis<max)
{
document.getElementById('lijst').innerHTML+="<span id='"+counter+"'><input type='file' name='input"+counter+"'><input type='button' value='x' onclick=\"javascript:delFile("+counter+")\"><br></span>";
counter++;
dis++;
}
else
alert("U mag maximaal "+max+" bestanden uploaden");
}
function delFile(id)
{
document.getElementById(id).innerHTML="";
fileArr[id]=null;
dis--;
}
</script>
</head>
<body>
<form method="post" action="<?php echo($_SERVER['PHP_SELF']); ?>" enctype="multipart/form-data">
<table>
<tr>
<td>Uw naam </td>
<td>:</td>
<td><input name="send_name" type="text" id="send_name" /></td>
</tr>
<tr>
<td>Uw e-mailadres </td>
<td>:</td>
<td><input name="send_mail" type="text" id="send_mail" /></td>
</tr>
<tr>
<td>Naar</td>
<td>:</td>
<td><input name="rec_name" type="text" id="rec_name" /></td>
</tr>
<tr>
<td>Onderwerp</td>
<td>:</td>
<td><input name="subject" type="text" id="subject" /></td>
</tr>
<tr>
<th colspan="3">Bericht</th>
</tr>
<tr>
<td colspan="3"><div align="center">
<textarea name="msg" cols="35" rows="5" id="msg"></textarea>
</div></td>
</tr>
<tr>
<td colspan="3" align="center"><a id="hyper" href="javascript:addFile()">Bijlageveld toevoegen</a></td>
</tr>
<tr>
<td colspan="3" align="center"><div id='lijst'></div></td>
</tr>
<tr>
<th colspan="3">Formaat</th>
</tr>
<tr>
<td colspan="3"><input name="format" type="radio" value="0" checked="checked" />
Gewone tekst </td>
</tr>
<tr>
<td colspan="3"><input name="format" type="radio" value="1" />
HTML</td>
</tr>
<tr>
<th colspan="3">Tekenset</th>
</tr>
<tr>
<td colspan="3"><input name="charset" type="radio" value="0" checked="checked" />
us-ascii</td>
</tr>
<tr>
<td colspan="3"><input name="charset" type="radio" value="1" />
utf-8</td>
</tr>
<tr>
<td colspan="3"><div align="center">
<input type="reset" name="Reset" value="Opnieuw" />
<input type="submit" name="Submit2" value="Versturen" />
</div></td>
</tr>
</table>
</form>
</body>
</html>