3 # Send off an email message
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'});
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'});
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'});
29 &error($text{'send_efrom'}) if (!$found);
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'});
36 &error($text{'send_efrom'}) if (!$found);
38 elsif ($access{'fmode'} == 3) {
39 $in{'from'} .= "\@$access{'from'}";
41 if ($in{'from'} =~ /^(\S+)\@(\S+)$/ && $access{'fromname'}) {
42 $in{'from'} = "$access{'fromname'} <$in{'from'}>";
44 @sub = split(/\0/, $in{'sub'});
45 $subs = join("", map { "&sub=$_" } @sub);
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 ] ];
56 push(@{$mail->{'headers'}},
57 [ 'Cc', &encode_mimewords_address($in{'cc'}, %enc) ]);
60 push(@{$mail->{'headers'}},
61 [ 'Bcc', &encode_mimewords_address($in{'bcc'}, %enc) ]);
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'})
71 @errs = &spell_check_text($plainbody);
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;
79 "javascript:back()", $text{'reply_return'},
80 "index.cgi?user=$in{'user'}&folder=$in{'folder'}",
81 $text{'mail_return'});
85 local $mt = $in{'html_edit'} ? "text/html" : "text/plain";
87 $mt .= "; charset=$in{'charset'}";
89 if ($in{'body'} =~ /[\177-\377]/) {
90 # Contains 8-bit characters .. need to make quoted-printable
92 @attach = ( { 'headers' => [ [ 'Content-Type', $mt ],
93 [ 'Content-Transfer-Encoding',
94 'quoted-printable' ] ],
95 'data' => quoted_encode($in{'body'}) } );
98 # Plain 7-bit ascii text
99 @attach = ( { 'headers' => [ [ 'Content-Type', $mt ],
100 [ 'Content-Transfer-Encoding',
102 'data' => $in{'body'} } );
104 $bodyattach = $attach[0];
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]/) {
113 { 'headers' => [ [ 'Content-Type', $mt ],
114 [ 'Content-Transfer-Encoding',
115 'quoted-printable' ] ],
116 'data' => quoted_encode($plainbody) });
120 { 'headers' => [ [ 'Content-Type', $mt ],
121 [ 'Content-Transfer-Encoding',
123 'data' => $plainbody });
126 # Set content type to multipart/alternative, to tell mail
127 # clients about the optional body
128 local $bound = "altsbound".time();
130 'headers' => [ [ 'Content-Type',
131 'multipart/alternative; '.
132 'boundary="'.$bound.'"' ],
133 [ 'Content-Transfer-Encoding',
135 'data' => join("", &unparse_mail(\@alts, "\n", $bound))
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=\"".
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',
154 $atotal += length($in{"attach${i}"});
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"};
166 local @st = stat($in{"file$i"});
167 &test_max_attach($st[7]);
169 &switch_to_user($in{'user'});
170 $data = &read_file_contents($in{"file$i"});
171 $data || &error(&text('send_efile', $in{"file$i"}, $!));
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',
182 $atotal += length($data);
184 @fwd = split(/\0/, $in{'forward'});
186 # Add forwarded attachments
187 @mail = &mailbox_list_mails($in{'idx'}, $in{'idx'}, $folder);
188 $fwdmail = $mail[$in{'idx'}];
189 &parse_mail($fwdmail);
192 # We are looking at a mail within a mail ..
193 local $amail = &extract_mail($fwdmail->{'attach'}->[$s]->{'data'});
199 &test_max_attach(length($fwdmail->{'attach'}->[$f]->{'data'}));
200 push(@attach, $fwdmail->{'attach'}->[$f]);
201 $atotal += length($fwdmail->{'attach'}->[$f]->{'data'});
204 @mailfwd = split(/\0/, $in{'mailforward'});
206 # Add forwarded emails
207 @mail = &mailbox_list_mails($mailfwd[0], $mailfwd[@mailfwd-1], $folder);
208 foreach $f (@mailfwd) {
209 $fwdmail = $mail[$f];
211 foreach $h (@{$fwdmail->{'headers'}}) {
212 $headertext .= $h->[0].": ".$h->[1]."\n";
214 push(@attach, { 'data' => $headertext."\n".$fwdmail->{'body'},
215 'headers' => [ [ 'Content-type', 'message/rfc822' ],
216 [ 'Content-Description',
217 $fwdmail->{'header'}->{'subject'} ] ]
221 $mail->{'attach'} = \@attach;
222 if ($access{'attach'} >= 0 && $atotal > $access{'attach'}*1024) {
223 &error(&text('send_eattach', $access{'attach'}));
226 # Check for text-only email
227 $textonly = $config{'no_mime'} && !$quoted_printable &&
228 @{$mail->{'attach'}} == 1 &&
229 $mail->{'attach'}->[0] eq $bodyattach &&
233 &send_mail($mail, undef, $textonly, $config{'no_crlf'});
234 &webmin_log("send", undef, undef, { 'from' => $in{'from'}, 'to' => $in{'to'} });
236 # Tell the user that email as sent
237 &mail_page_header($text{'send_title'}, undef, undef,
238 &folder_link($in{'user'}, $folder));
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";
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'});
252 &mail_page_footer("list_mail.cgi?folder=$in{'folder'}&user=$in{'user'}",
253 $text{'mail_return'},
254 "", $text{'index_return'});
257 # write_attachment(&attach)
262 foreach $h (@{$a->{'headers'}}) {
263 $rv .= $h->[0].": ".$h->[1]."\r\n";
265 if (lc($h->[0]) eq 'content-transfer-encoding');
268 if (lc($enc) eq 'base64') {
269 local $encoded = &encode_base64($a->{'data'});
271 $encoded =~ s/\n/\r\n/g;
275 $a->{'data'} =~ s/\r//g;
276 $a->{'data'} =~ s/\n/\r\n/g;
278 if ($a->{'data'} !~ /\n$/) {
287 $attachsize += $_[0];
288 if ($access{'attach'} >= 0 && $attachsize > $access{'attach'}) {
289 &error(&text('send_eattachsize', $access{'attach'}));