Handle hostnames with upper-case letters
[webmin.git] / mailboxes / send_mail.cgi
1 #!/usr/local/bin/perl
2 # send_mail.cgi
3 # Send off an email message
4
5 require './mailboxes-lib.pl';
6 &ReadParse(\%getin, "GET");
7 &ReadParseMime(undef, \&read_parse_mime_callback, [ $getin{'id'} ]);
8 &can_user($in{'user'}) || &error($text{'mail_ecannot'});
9 @uinfo = &get_mail_user($in{'user'});
10 @uinfo || &error($text{'view_eugone'});
11
12 # Check inputs
13 @folders = &list_user_folders($in{'user'});
14 $folder = $folders[$in{'folder'}];
15 &error_setup($text{'send_err'});
16 $in{'to'} || &error($text{'send_eto'});
17 if ($access{'fmode'} == 0) {
18         # Any from address allowed
19         $in{'from'} || &error($text{'send_efrom'});
20         }
21 elsif ($access{'fmode'} == 1) {
22         # From address must be in an allowed domain, and match username
23         $validfrom = &get_user_from_address(\@uinfo);
24         foreach $f (split(/\s+/, $access{'from'})) {
25                 $found++ if ("$in{'user'}\@$f" eq $in{'from'} ||
26                              "$in{'ouser'}\@$f" eq $in{'from'} ||
27                              $validfrom eq $in{'from'});
28                 }
29         &error($text{'send_efrom'}) if (!$found);
30         }
31 elsif ($access{'fmode'} == 2) {
32         # From address must be in allowed list
33         foreach $f (split(/\s+/, $access{'from'})) {
34                 $found++ if ($f eq $in{'from'});
35                 }
36         &error($text{'send_efrom'}) if (!$found);
37         }
38 elsif ($access{'fmode'} == 3) {
39         $in{'from'} .= "\@$access{'from'}";
40         }
41 if ($in{'from'} =~ /^(\S+)\@(\S+)$/ && $access{'fromname'}) {
42         $in{'from'} = "$access{'fromname'} <$in{'from'}>";
43         }
44 @sub = split(/\0/, $in{'sub'});
45 $subs = join("", map { "&sub=$_" } @sub);
46
47 # Construct the email
48 $in{'from'} || &error($text{'send_efrom'});
49 $newmid = &generate_message_id($in{'from'});
50 %enc = ( 'Charset' => $in{'charset'} );
51 $mail->{'headers'} = [ [ 'From', &encode_mimewords($in{'from'}, %enc) ],
52                        [ 'Subject', &encode_mimewords($in{'subject'}, %enc) ],
53                        [ 'To', &encode_mimewords_address($in{'to'}, %enc) ],
54                        [ 'Message-Id', $newmid ] ];
55 if ($in{'cc'}) {
56         push(@{$mail->{'headers'}},
57              [ 'Cc', &encode_mimewords_address($in{'cc'}, %enc) ]);
58         }
59 if ($in{'bcc'}) {
60         push(@{$mail->{'headers'}},
61              [ 'Bcc', &encode_mimewords_address($in{'bcc'}, %enc) ]);
62         }
63 &add_mailer_ip_headers($mail->{'headers'});
64 push(@{$mail->{'headers'}}, [ 'X-Priority', $in{'pri'} ]) if ($in{'pri'});
65 $in{'body'} =~ s/\r//g;
66 if ($in{'body'} =~ /\S/) {
67         # Perform spell check on body if requested
68         local $plainbody = $in{'html_edit'} ? &html_to_text($in{'body'})
69                                             : $in{'body'};
70         if ($in{'spell'}) {
71                 @errs = &spell_check_text($plainbody);
72                 if (@errs) {
73                         # Spelling errors found!
74                         &mail_page_header($text{'compose_title'}, undef, undef,
75                                           &folder_link($in{'user'}, $folder));
76                         print "<b>$text{'send_espell'}</b><p>\n";
77                         print map { $_."<p>\n" } @errs;
78                         &mail_page_footer(
79                             "javascript:back()", $text{'reply_return'},
80                             "index.cgi?user=$in{'user'}&folder=$in{'folder'}",
81                             $text{'mail_return'});
82                         exit;
83                         }
84                 }
85         local $mt = $in{'html_edit'} ? "text/html" : "text/plain";
86         if ($in{'charset'}) {
87                 $mt .= "; charset=$in{'charset'}";
88                 }
89         if ($in{'body'} =~ /[\177-\377]/) {
90                 # Contains 8-bit characters .. need to make quoted-printable
91                 $quoted_printable++;
92                 @attach = ( { 'headers' => [ [ 'Content-Type', $mt ],
93                                              [ 'Content-Transfer-Encoding',
94                                                'quoted-printable' ] ],
95                               'data' => quoted_encode($in{'body'}) } );
96                 }
97         else {
98                 # Plain 7-bit ascii text
99                 @attach = ( { 'headers' => [ [ 'Content-Type', $mt ],
100                                              [ 'Content-Transfer-Encoding',
101                                                '7bit' ] ],
102                               'data' => $in{'body'} } );
103                 }
104         $bodyattach = $attach[0];
105
106         if ($in{'html_edit'}) {
107                 # Create an attachment which contains both the HTML and plain
108                 # bodies as alternatives
109                 local @alts = ( $attach[0] );
110                 local $mt = "text/plain; charset=$charset";
111                 if ($plainbody =~ /[\177-\377]/) {
112                         unshift(@alts,
113                           { 'headers' => [ [ 'Content-Type', $mt ],
114                                            [ 'Content-Transfer-Encoding',
115                                              'quoted-printable' ] ],
116                             'data' => quoted_encode($plainbody) });
117                         }
118                 else {
119                         unshift(@alts,
120                           { 'headers' => [ [ 'Content-Type', $mt ],
121                                            [ 'Content-Transfer-Encoding',
122                                              '7bit' ] ],
123                             'data' => $plainbody });
124                         }
125
126                 # Set content type to multipart/alternative, to tell mail
127                 # clients about the optional body
128                 local $bound = "altsbound".time();
129                 $attach[0] = {
130                         'headers' => [ [ 'Content-Type',
131                                          'multipart/alternative; '.
132                                          'boundary="'.$bound.'"' ],
133                                        [ 'Content-Transfer-Encoding',
134                                          '7bit' ] ],
135                         'data' => join("", &unparse_mail(\@alts, "\n", $bound))
136                         };
137                 }
138         }
139 $attachsize = 0;
140 for($i=0; defined($in{"attach$i"}); $i++) {
141         # Add uploaded attachment
142         next if (!$in{"attach$i"});
143         &test_max_attach($attachsize);
144         local $filename = $in{"attach${i}_filename"};
145         $filename =~ s/^.*(\\|\/)//;
146         local $type = $in{"attach${i}_content_type"}."; name=\"".
147                       $filename."\"";
148         local $disp = "inline; filename=\"".$filename."\"";
149         push(@attach, { 'data' => $in{"attach${i}"},
150                         'headers' => [ [ 'Content-type', $type ],
151                                        [ 'Content-Disposition', $disp ],
152                                        [ 'Content-Transfer-Encoding',
153                                          'base64' ] ] });
154         $atotal += length($in{"attach${i}"});
155         }
156 for($i=0; defined($in{"file$i"}); $i++) {
157         # Add uploaded attachment
158         # Add server-side attachment
159         next if (!$in{"file$i"} || !$access{'canattach'});
160         @uinfo = &get_mail_user($in{'user'});
161         @uinfo || &error($text{'view_eugone'});
162         if ($in{"file$i"} !~ /^\//) {
163                 $in{"file$i"} = $uinfo[7]."/".$in{"file$i"};
164                 }
165
166         local @st = stat($in{"file$i"});
167         &test_max_attach($st[7]);
168         local $data;
169         &switch_to_user($in{'user'});
170         $data = &read_file_contents($in{"file$i"});
171         $data || &error(&text('send_efile', $in{"file$i"}, $!));
172         &switch_user_back();
173         $in{"file$i"} =~ s/^.*\///;
174         local $type = &guess_mime_type($in{"file$i"}).
175                       "; name=\"".$in{"file$i"}."\"";
176         local $disp = "inline; filename=\"".$in{"file$i"}."\"";
177         push(@attach, { 'data' => $data,
178                         'headers' => [ [ 'Content-type', $type ],
179                                        [ 'Content-Disposition', $disp ],
180                                        [ 'Content-Transfer-Encoding',
181                                          'base64' ] ] });
182         $atotal += length($data);
183         }
184 @fwd = split(/\0/, $in{'forward'});
185 if (@fwd) {
186         # Add forwarded attachments
187         @mail = &mailbox_list_mails($in{'idx'}, $in{'idx'}, $folder);
188         $fwdmail = $mail[$in{'idx'}];
189         &parse_mail($fwdmail);
190
191         foreach $s (@sub) {
192                 # We are looking at a mail within a mail ..
193                 local $amail = &extract_mail($fwdmail->{'attach'}->[$s]->{'data'});
194                 &parse_mail($amail);
195                 $fwdmail = $amail;
196                 }
197
198         foreach $f (@fwd) {
199                 &test_max_attach(length($fwdmail->{'attach'}->[$f]->{'data'}));
200                 push(@attach, $fwdmail->{'attach'}->[$f]);
201                 $atotal += length($fwdmail->{'attach'}->[$f]->{'data'});
202                 }
203         }
204 @mailfwd = split(/\0/, $in{'mailforward'});
205 if (@mailfwd) {
206         # Add forwarded emails
207         @mail = &mailbox_list_mails($mailfwd[0], $mailfwd[@mailfwd-1], $folder);
208         foreach $f (@mailfwd) {
209                 $fwdmail = $mail[$f];
210                 local $headertext;
211                 foreach $h (@{$fwdmail->{'headers'}}) {
212                         $headertext .= $h->[0].": ".$h->[1]."\n";
213                         }
214                 push(@attach, { 'data' => $headertext."\n".$fwdmail->{'body'},
215                                 'headers' => [ [ 'Content-type', 'message/rfc822' ],
216                                                [ 'Content-Description',
217                                                   $fwdmail->{'header'}->{'subject'} ] ]
218                               });
219                 }
220         }
221 $mail->{'attach'} = \@attach;
222 if ($access{'attach'} >= 0 && $atotal > $access{'attach'}*1024) {
223         &error(&text('send_eattach', $access{'attach'}));
224         }
225
226 # Check for text-only email
227 $textonly = $config{'no_mime'} && !$quoted_printable &&
228             @{$mail->{'attach'}} == 1 &&
229             $mail->{'attach'}->[0] eq $bodyattach &&
230             !$in{'html_edit'};
231
232 # Send it off
233 &send_mail($mail, undef, $textonly, $config{'no_crlf'});
234 &webmin_log("send", undef, undef, { 'from' => $in{'from'}, 'to' => $in{'to'} });
235
236 # Tell the user that email as sent
237 &mail_page_header($text{'send_title'}, undef, undef,
238                   &folder_link($in{'user'}, $folder));
239
240 @tos = ( split(/,/, $in{'to'}), split(/,/, $in{'cc'}), split(/,/, $in{'bcc'}) );
241 $tos = join(" , ", map { "<tt>".&html_escape($_)."</tt>" } @tos);
242 print "<p>",&text($in{'draft'} ? 'send_draft' : 'send_ok', $tos),"<p>\n";
243
244 if ($in{'idx'} ne '') {
245         &mail_page_footer("view_mail.cgi?idx=$in{'idx'}&folder=$in{'folder'}&user=$in{'user'}$subs",
246                 $text{'view_return'},
247                 "list_mail.cgi?folder=$in{'folder'}&user=$in{'user'}",
248                 $text{'mail_return'},
249                 "", $text{'index_return'});
250         }
251 else {
252         &mail_page_footer("list_mail.cgi?folder=$in{'folder'}&user=$in{'user'}",
253                 $text{'mail_return'},
254                 "", $text{'index_return'});
255         }
256
257 # write_attachment(&attach)
258 sub write_attachment
259 {
260 local ($a) = @_;
261 local ($enc, $rv);
262 foreach $h (@{$a->{'headers'}}) {
263         $rv .= $h->[0].": ".$h->[1]."\r\n";
264         $enc = $h->[1]
265             if (lc($h->[0]) eq 'content-transfer-encoding');
266         }
267 $rv .= "\r\n";
268 if (lc($enc) eq 'base64') {
269         local $encoded = &encode_base64($a->{'data'});
270         $encoded =~ s/\r//g;
271         $encoded =~ s/\n/\r\n/g;
272         $rv .= $encoded;
273         }
274 else {
275         $a->{'data'} =~ s/\r//g;
276         $a->{'data'} =~ s/\n/\r\n/g;
277         $rv .= $a->{'data'};
278         if ($a->{'data'} !~ /\n$/) {
279                 $rv .= "\r\n";
280                 }
281         }
282 return $rv;
283 }
284
285 sub test_max_attach
286 {
287 $attachsize += $_[0];
288 if ($access{'attach'} >= 0 && $attachsize > $access{'attach'}) {
289         &error(&text('send_eattachsize', $access{'attach'}));
290         }
291 }
292