;
close(FILE);
local $count = ($data =~ tr/[\000-\176]/[\000-\176]/);
if (!length($data) || 100*$count / length($data) > 95) {
# File is text
local $a;
local $sf = &short_name($f);
$a .= "Content-Type: text/plain; name=\"$sf\"\n";
$a .= "Content-Transfer-Encoding: 7bit\n";
$a .= "\n";
$a .= $data;
push(@attach, $a);
}
}
}
# Include uploaded attached files
foreach $u ('attach0', 'attach1') {
if ($in{$u} ne '') {
local $a;
local $name = &short_name($in{"${u}_filename"});
local $type = $in{"${u}_content_type"};
$type = &guess_mime_type($name) if (!$type);
$a .= "Content-type: $type; name=\"$name\"\n";
$a .= "Content-Transfer-Encoding: base64\n";
$a .= "\n\n";
$a .= &encode_base64($in{$u});
push(@attach, $a);
}
}
# Build the MIME email
$bound = "bound".time();
$mail = $headers;
$mail .= "Content-Type: multipart/mixed; boundary=\"$bound\"\n";
$mail .= "MIME-Version: 1.0\n";
$mail .= "\n";
$mail .= "This is a multi-part message in MIME format.\n";
foreach $a (@attach) {
$mail .= "\n--".$bound."\n";
$mail .= $a;
}
$mail .= "\n--".$bound."--\n";
if (!$in{'mailserver_def'}) {
$ok = &send_via_smtp($in{'mailserver'});
$sent = 3 if ($ok);
}
if (!$sent) {
# Try to send the email by calling sendmail -t
%sconfig = &foreign_config("sendmail");
$sendmail = $sconfig{'sendmail_path'} ? $sconfig{'sendmail_path'}
: &has_command("sendmail");
if (-x $sendmail && open(MAIL, "| $sendmail -t")) {
print MAIL $mail;
if (close(MAIL)) {
$sent = 2;
}
}
}
if (!$sent) {
# Try to connect to a local SMTP server
$ok = &send_via_smtp("localhost");
$sent = 1 if ($ok);
}
if ($sent) {
# Tell the user that it was sent OK
&ui_print_header(undef, $text{'feedback_title'}, "", undef, 0, 1);
if ($sent == 3) {
print &text('feedback_via', join(",", @tolist),
"$in{'mailserver'}"),"\n";
}
elsif ($sent == 2) {
print &text('feedback_prog', join(",", @tolist),
"$sendmail"),"\n";
}
else {
print &text('feedback_via', join(",", @tolist),
"localhost"),"\n";
}
print "\n";
&ui_print_footer("/", $text{'index'});
# Save settings in config
$gconfig{'feedback_name'} = $in{'name'};
$gconfig{'feedback_email'} = $in{'email'};
$gconfig{'feedback_mailserver'} =
$in{'mailserver_def'} ? undef : $in{'mailserver'};
&write_file("$config_directory/config", \%gconfig);
}
else {
# Give up! Tell the user ..
&error($text{'feedback_esend'});
}
sub send_via_smtp
{
local $error;
&open_socket($_[0], 25, MAIL, \$error);
return 0 if ($error);
&smtp_command(MAIL) || return 0;
&smtp_command(MAIL, "helo ".&get_system_hostname()."\r\n") || return 0;
&smtp_command(MAIL, "mail from: <$email>\r\n") || return 0;
foreach $t (@tolist) {
&smtp_command(MAIL, "rcpt to: <$t>\r\n") || return 0;
}
&smtp_command(MAIL, "data\r\n");
$mail =~ s/\r//g;
$mail =~ s/\n/\r\n/g;
print MAIL $mail;
&smtp_command(MAIL, ".\r\n");
&smtp_command(MAIL, "quit\r\n");
close(MAIL);
return 1;
}
# smtp_command(handle, command)
sub smtp_command
{
local ($m, $c) = @_;
print $m $c;
local $r = <$m>;
return $r =~ /^[23]\d+/;
}
sub short_name
{
$_[0] =~ /([^\\\/]+)$/;
return $1;
}