Voor iemand ben ik bezig om een goed werkend form to mail te maken.
het werkt op zich prima behalve dat de text in de email niet aligned is.
helaas kan ik niet achterhalen waar de fout ligt dus wilde ik een beroep op jullie expertise doen.
Hier is de cgi code die ik gebruik.
prive gegevens zijn gemaskeerd met XXXXX
Alvast mijn dank voor enige reacties
het werkt op zich prima behalve dat de text in de email niet aligned is.
helaas kan ik niet achterhalen waar de fout ligt dus wilde ik een beroep op jullie expertise doen.
Hier is de cgi code die ik gebruik.
prive gegevens zijn gemaskeerd met XXXXX
code:
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
| #!/usr/bin/perl $path_to_sendmail = "/usr/sbin/sendmail"; $email_field = "01Email"; $use_autoresponse = "No"; $autoresponse_message = ""; $showconfirmation = "No"; $usecustomthankyou = "Yes"; $htmlheader = ""; $htmlfooter = ""; print "Content-type: text/html\n\n"; %data=&get_data(); @fields = sort(keys(%data)); @required = ('00Naam','01Email','02Adres','03Postcode','04Woonplaats','05Land'); if($fields[0] eq ""){ print "$htmlheader <body bgcolor=\"#FEFCF5\" text=\"#000000\" link=\"#0000FF\" alink=\"#FF0000\" vlink=\"#800080\"> <b>Fout: Onvoldoende informatie</b><br><br> <font size=+1>U heeft geen gegevens ingevoerd. Ga terug naar bestellen, vul alles in, en druk op verzenden.</font>"; print "<hr><a href=\"javascript:history.back();\">Back...</a>"; print "</font>$htmlfooter"; exit(); } foreach $field (@required){ if($field eq $email_field){ $email_test = &check_email($data{$field}); if($email_test){ $error_fields .= $email_test; } } foreach $req (@required){ if($req eq $field){ if($data{$field} eq ""){ if($field eq $email_field && $email_test ne ""){ next; } $field =~ s/^\d\d//g; $field =~ s/_/ /g; $error_fields .= "$field must be filled out.<br>"; } } } } #if($data{'Message'} eq "" || $data{'Message'} =~ /^\s+$/){ # $error_fields .= "U heeft geen bericht ingevoerd!<br>\n"; # } if($error_fields ne ""){ print "$htmlheader <body bgcolor=\"#FEFCF5\" text=\"#000000\" link=\"#0000FF\" alink=\"#FF0000\" vlink=\"#800080\"> <b>Fout: Onvoldoende Informatie</b><br><br> <font size=+1>Sorry, maar er is een probleem met wat u heeft ingevuld.</font><br> <br> Verbeter uw fout en probeer opnieuw:<br> <br> $error_fields <br><br> Ga terug naar bestellen, vul alles in, en druk op verzenden..</font>"; print "<hr><a href=\"javascript:history.back();\">Back...</a>"; print "</font>$htmlfooter"; exit(); } foreach $field (@fields){ $len1 = sprintf("%3s", length($field)); $len1 =~ s/\D/0/g; $lengths[$#lengths + 1] = $len1; } @lengths = sort(@lengths); $longest = $lengths[$#lengths]; $toemail = "XXXXXXXX\@XXXXXXX.nl"; $fromemail = "XXXXXXXX\@XXXXXXXXX.nl"; if($data{$email_field}){ $replytoemail = $data{$email_field}; } else { $replytoemail = $fromemail; } if($data{'FormSubject'} ne ""){ $subject = $data{'FormSubject'}; } else { $subject = "Bericht van uw website"; } $subject =~ s/\s+/ /sg; $subject =~ s/\0/ /sg; $subject .= "\n\n"; $message = "=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\n"; $message .= " Deze bericht is verstuurd via uw website, de gebruiker die deze\n"; $message .= " bericht heeft verstuurd heeft IP adres:\n"; $message .= " $ENV{'REMOTE_ADDR'} $ENV{'REMOTE_HOST'}\n"; $message .= " Tijdstip: " . localtime() . "\n"; $message .= " Als dit bericht spam is, neem dan contact op met hun provider.\n"; $message .= "=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-="; $message .= "\n\n"; if($showconfirmation eq "Yes"){ $confirmation = "<table border=1 cellpadding=2 cellspacing=0> <tr><td colspan=2><b>Het volgende informtatie heeft u verzonden:</b></td></tr>\n"; } foreach $field (@fields){ if($field eq "FA" || $field eq "owneremail" || $field eq "BodyTag" || $field eq "Submit" || $field eq "required" || $field eq "Message"){ next; } $show = $field; $show =~ s/^\d\d//g; $show =~ s/_/ /g; $longest = $longest * 1; $len = "% ".$longest."s"; $show = sprintf("$len", $show); $message .= "$show: $data{$field}\n"; if($showconfirmation eq "Yes"){ $confirmation .= "<tr><td><b>$show</b></td><td>$data{$field}</td></tr>\n"; } } if($showconfirmation eq "Yes"){ $confirmation .= "<tr><td valign=left><b>Message</b></td><td>$data{'Message'}</td></tr>"; $confirmation .= "</table>\n"; } $message .= "\nBericht:\n\n$data{'Message'}\n"; foreach $msgline (split(/\n/, $message)){ $msgline =~ s/^cc:/ X-abused-cc: /isg; $msgline =~ s/^bcc:/ X-abused-bcc: /isg; $msgline =~ s/^to:/ X-abused-to: /isg; $msgline =~ s/^from:/ X-abused-from: /isg; $msgline =~ s/^reply-to:/ X-abused-reply-to: /isg; $msgline =~ s/^subject:/ X-abused-subject: /isg; $msgline =~ s/^Received:/ X-abused-received: /isg; $newmsg .= "$msgline\n"; } $message = $newmsg; # Do the Autoresponse, if any if($replytoemail ne $fromemail && $use_autoresponse eq "Yes" && $autoresponse_message ne ""){ &send_mail($replytoemail,$replytoemail,$fromemail,$fromemail,$fromemail, $fromemail,"Bedankt voor uw email",$autoresponse_message, 'mail.your_domain.com', 'www.your_domain.com', 'unix', '',$smtp_auth, $pop_un,$pop_pw); } # Send the mail to you &send_mail($toemail,"Webmaster",$fromemail,"$replytoemail ",$replytoemail, $replytoemail,$subject,$message, 'mail.your_domain.com', 'www.your_domain.com', 'unix', '',$smtp_auth, $pop_un,$pop_pw); $data{'BodyTag'} =~ s/</\</g; $data{'BodyTag'} =~ s/>/\>/g; $data{'BodyTag'} =~ s/"/\"/g; if($usecustomthankyou eq "No"){ print "$htmlheader $data{'BodyTag'} <font size=+1>Dank je. Je bericht is verzonden.<br><br> $confirmation<br> "; print "<a href=\"http://www.XXXXXXXXX.nl/XXXXXXXXX.html\">http://www.XXXXXXXXX.nl/XXXXXXXXX.html</a>"; print "</font>$htmlfooter"; exit(); } else { $htmlheader =~ s/<\/head>/<meta HTTP-EQUIV=Refresh Content=\"0;URL=http:\/\/www.XXXXXXXXX.nl\/XXXXXXXXX.html\">\n<\/head>/is; if($htmlheader !~ /<\/head>/i){ $htmlheader .= "<head>\n<META HTTP-EQUIV=Refresh Content=\"0;URL=http://www.gXXXXXXXXX.nl/XXXXXXXXX.html\">\n</head>"; } print "$htmlheader $data{'BodyTag'} <font size=+1>Dank je. Je bericht is verzonden.<br> $confirmation<br> "; print "<a href=\"http://www.XXXXXXXXX.nl/XXXXXXXXX.html\">http://www.XXXXXXXXX.nl/XXXXXXXXX.html</a>"; print " </font>$htmlfooter"; exit(); } sub check_email { my $email = $_[0]; if($email !~ /(.{1,})(\@)(.{1,})(\.)(.{2,})/ || $email =~ /,/ || $email =~ /.*\@.*\@/ || $email =~ /\@\@/ || $email =~ /\s/ || $email =~ /\.$/ || $email =~ /\@\./){ return "Email adres is geen geldige email adres.<br>"; } } #&send_mail("to_email","to_name","from_email","from_name","replyto_email", # "replyto_name","subject","message","mail_server_hostname", # "this_server_hostname",$opsys,$htmlmail,$smtp_auth, # $pop_un,$pop_pw); sub send_mail { local($toemail,$toname,$fromemail,$fromname, $replytoemail,$replytoname,$subject, $message,$mail_server_hostname,$this_server_hostname,$opsys, $htmlmail,$smtp_auth,$pop_un,$pop_pw) = @_; my($boundary) = crypt("blah",time.$$).time.$$; my($testaddr); foreach $testaddr (split(/,/, "toemail,toname,fromemail,fromname,replytoemail,replytoname,subject")) { ${$testaddr} =~ s/\0//sg; ${$testaddr} =~ s/\r//sg; ${$testaddr} =~ s/\n//sg; ${$testaddr} =~ s/\r\n//sg; ${$testaddr} =~ s/\015//sg; ${$testaddr} =~ s/\012//sg; ${$testaddr} =~ s/\015\012//sg; ${$testaddr} =~ s/\s+/ /sg; } if($opsys eq "win"){ my($port,$child,$proto); # Switch to $auth_from_address if there is one. if(($smtp_auth eq "POP" || $smtp_auth eq "SMTP") && $auth_from_address){ $fromemail = $auth_from_address; } # Do pop-before-smtp auth if necessary. if($smtp_auth eq "POP"){ &do_pop_auth($this_server_hostname,$mail_server_hostname,$pop_un,$pop_pw); } $CRLF = "\015\012"; # Make sure we have the correct line endings $message =~ s/\r\n/\n/sg; $message =~ s/\r/\n/sg; $message =~ s/\n/$CRLF/sg; my $port = 25; use Socket; $SIG{'INT'} = 'dokill'; sub dokill { kill 9,$child if $child; } my $proto = 6; # tcp $this = pack_sockaddr_in(0, inet_aton($this_server_hostname)); $that = pack_sockaddr_in($port, inet_aton($mail_server_hostname)); if (socket(S, AF_INET, SOCK_STREAM, $proto)) { &debug("Created socket."); } else { &debug("Could not create socket for mail: $!"); } if (connect(S,$that)) { &debug("Connected to socket."); } else { &debug("Could not connect to socket: $!"); } # Unbuffer the output for socket and stdout select(S); $| = 1; select(STDOUT); $| = 1; &debug("Server responded: " . read_response()) if $no_more !=1; # SMTP auth is done in the same socket connection, so let's do it now. if($smtp_auth eq "SMTP"){ &do_smtp_auth($pop_un,$pop_pw); } # We don't need HELO with SMTP auth because we already did EHLO else { &debug("I sent: HELO $this_server_hostname\\r\\n") if $no_more !=1; print S "HELO $this_server_hostname$CRLF"; &debug("Server responded: " . read_response()) if $no_more !=1; } &debug("I sent: MAIL FROM:<$fromemail>\\r\\n") if $no_more !=1; print S "MAIL FROM:<$fromemail>$CRLF"; &debug("Server responded: " . read_response()) if $no_more !=1; &debug("I sent: RCPT TO:<$toemail>\\r\\n") if $no_more !=1; print S "RCPT TO:<$toemail>$CRLF"; &debug("Server responded: " . read_response()) if $no_more !=1; &debug("I sent: DATA\\r\\n") if $no_more !=1; print S "DATA$CRLF"; &debug("Server responded: " . read_response()) if $no_more !=1; &debug("I sent: To: $toname <$toemail>\\r\\n") if $no_more !=1; print S "To: $toname <$toemail>$CRLF"; &debug("I sent: From: $fromname <$fromemail>\\r\\n") if $no_more !=1; print S "From: $fromname <$fromemail>$CRLF"; if($replytoemail ne ""){ &debug("I sent: Reply-to: $replytoname <$replytoemail>\\r\\n") if $no_more !=1; print S "Reply-to: $replytoname <$replytoemail>$CRLF"; } if(!$x_auth_header){ $x_auth_header = "X-Auth-Type: None"; } print S "$x_auth_header$CRLF"; print S "X-Mailer: $0 http://tesol.net/scripts/$CRLF"; &debug("I sent: Subject: $subject\\r\\n") if $no_more !=1; print S "Subject: $subject$CRLF"; if($htmlmail eq "yes"){ &debug("Putting in HTML message\\r\\n") if $no_more !=1; print S "MIME-Version: 1.0$CRLF"; print S "Content-type: MULTIPART/MIXED; BOUNDARY=\"$boundary\"$CRLF"; print S "--$boundary$CRLF"; print S "Content-type: text/html;$CRLF$CRLF"; print S "$message$CRLF"; print S "$CRLF$CRLF"; print S "--$boundary$CRLF"; print S "Content-type: TEXT/PLAIN; charset=US-ASCII$CRLF$CRLF"; print S "$message--$boundary--$CRLF"; } else { &debug("Putting in plain text message\\r\\n") if $no_more !=1; print S "$CRLF"; print S "$message$CRLF"; } &debug("I sent: .\\r\\n") if $no_more !=1; print S ".$CRLF"; &debug("Server responded: " . read_response()) if $no_more !=1; &debug("I sent: QUIT\\r\\n") if $no_more !=1; print S "QUIT$CRLF"; &debug("Server responded: " . read_response()) if $no_more !=1; &debug("Sending mail done.") if $no_more !=1; } elsif($opsys eq "unix"){ # Allow for qmail-inject ~sigh~ # Most other mailer thingies do use the -t option like # sendmail in order to be compatible, but apparently # qmail-inject does not. Oh well... if($path_to_sendmail !~ /qmail-inject/){ $path_to_sendmail = "$path_to_sendmail -t"; } open(MAIL, "|$path_to_sendmail") || &debug("Could not open sendmail: $!"); print MAIL "To: \"$toname\" <$toemail>\n"; print MAIL "From: \"$fromname\" <$fromemail>\n"; print MAIL "X-Abuse-Identifier: $ENV{'REMOTE_ADDR'}\n"; if($replytoemail ne ""){ print MAIL "Reply-to: \"$replytoname\" <$replytoemail>\n"; } print MAIL "Subject: $subject\n"; if($htmlmail eq "yes"){ print MAIL "MIME-Version: 1.0\n"; print MAIL "Content-type: MULTIPART/MIXED; BOUNDARY=\"$boundary\"\n"; print MAIL "--$boundary\n"; print MAIL "Content-type: text/html;\n\n"; print MAIL "$message\n"; print MAIL "\n\n"; print MAIL "--$boundary\n"; print MAIL "Content-type: TEXT/PLAIN; charset=US-ASCII\n\n"; print MAIL "$message--$boundary--\n"; } else { print MAIL "\n"; print MAIL "$message\n"; } close(MAIL) || &debug("Sending mail got an error: $! $?"); &debug("Sending mail done."); } else { &debug("Cannot send mail. '$opsys' is not a valid operating sytem. Please set \$opsys to either 'unix' or 'win' in the script and try this again."); } } # Usage: do_pop_auth(local_server_hostname,mail_server_hostname,username,pw); # Opens a socket connection to the pop server to do POP-before-SMTP # authentication. sub do_pop_auth { my($this_server_hostname,$mail_server_hostname,$username,$password) = @_; use Socket; $port = 110; $x_auth_header = "X-Auth-Type: POP-BEFORE-SMTP"; my $CRLF = "\r\n"; $SIG{'INT'} = 'dokill'; sub dokill { kill 9,$child if $child; } my $proto = 6; $this = pack_sockaddr_in(0, inet_aton($this_server_hostname)); $that = pack_sockaddr_in($port, inet_aton($mail_server_hostname)); if (socket(S, AF_INET, SOCK_STREAM, $proto)) { &debug("Created S socket for pop auth."); } else { &debug("Could not create socket for pop auth: $!"); } if (connect(S, $that)){ &debug("Connected to S socket for pop auth."); } else { &debug("Could not connect to socket for pop auth: $!"); } select(S); $| = 1; select(STDOUT); $| = 1; &debug("Server responded: " . read_response()) if $no_more !=1; &debug("I sent: USER $username\\r\\n") if $no_more !=1; print S "USER $username$CRLF"; &debug("Server responded: " . read_response()) if $no_more !=1; &debug("I sent: PASS <password>\\r\\n") if $no_more !=1; print S "PASS $password$CRLF"; &debug("Server responded: " . read_response()) if $no_more !=1; if($_ !~ /OK/){ &debug("Ooops. Username/PW incorrect."); } else { &debug("I sent: QUIT\\r\\n") if $no_more !=1; print S "QUIT$CRLF"; &debug("Server responded: " . read_response()) if $no_more !=1; } close(S) || &debug("Could not close S socket: $!\n"); &debug("POP before SMTP attempt complete."); } # Usage: do_smtp_auth($username,$password); # Assumes that your socket connection to the SMTP port of the mail server # is already open from sub send_mail above. sub do_smtp_auth { my($username,$password) = @_; $x_auth_header = "X-Auth-Type: SMTP-AUTH-LOGIN"; &debug("I sent: EHLO $this_server_hostname\\r\\n") if $no_more !=1; print S "EHLO $this_server_hostname\r\n"; &debug("Server responded: " . read_response()) if $no_more !=1; &debug("I sent: AUTH LOGIN\\r\\n") if $no_more !=1; print S "AUTH LOGIN\015\012"; &debug("Server responded: " . read_response()) if $no_more !=1; # AUTH LOGIN requires username/pw encoded in Base64. However, we don't # know if the user will have the Base64 module available, so we'll do it # manually. $b64_u = join '', map( pack('u',$_)=~ /^.(\S*)/, ($username=~/(.{1,45})/gs)); $b64_u =~ tr|` -_|AA-Za-z0-9+/|; &debug("I sent: '$b64_u'\\r\\n") if $no_more !=1; print S "$b64_u$CRLF"; &debug("Server responded: " . read_response()) if $no_more !=1; $b64_p= join '', map( pack('u',$_)=~ /^.(\S*)/, ($password=~/(.{1,45})/gs)); $b64_p=~ tr|` -_|AA-Za-z0-9+/|; &debug("I sent: '$b64_p'\\r\\n") if $no_more !=1; print S "$b64_p$CRLF"; &debug("Server responded: " . read_response()) if $no_more !=1; if($_ !~ /^235/){ &debug("ERROR: SMTP authentication failed. Mail may not be sent."); } &debug("SMTP auth attempt complete."); } # Read multi-line responses from our socket connection. Only if # using a Windows server for mail. sub read_response { my ($socket_says); # If we've hit a 500 error, we're not going any further, so go ahead # and quit the connection. if($no_more == 1){ return; } do { $_ = <S>; chomp($_); $socket_says .= $_ . "\n"; if(/^5/){ &debug("\nERROR: Something went wrong." . "Anything beyond this won't work. $server_says "); print S "QUIT$CRLF"; $no_more = 1; } } while (/^[\d]+-/); $socket_says; } sub get_data { use CGI qw/:standard/; my $query = new CGI; foreach $key ($query->param()){ $data{$key} = $query->param($key); } %data; # return associative array of name=value } sub debug { if($debugging == 1){ print "DEBUG: $_[0]<br>\n"; } } |
Alvast mijn dank voor enige reacties