Проблемы с кодировкой!

  • Автор темы rusws02
  • Дата начала
R

rusws02

Гость
#1
Есть скрипт «рекомендовать другу», для отправке сообщения с сайта, но сообщения приходят в KOI8-R кодировке, как сделать, чтобы было windows-1251?

Товарищи выручайте!

#!/usr/bin/perl



$mailprogram = '/usr/sbin/sendmail';
# $mailprogram = '/usr/lib/sendmail';
# $mailprogram = '/usr/bin/sendmail';

# Edit this one to achieve color matching with the rest
# of your site - this would be what goes in your html <BODY> tag.
#
# NOTE : DO NOT use " ' # < or >
# Use the below example to help you with what is allowed

$body = 'bgcolor=000000 text=ffffff link=fac700 alink=ff0000 vlink=ff0000';

# URL of this script - include the filename.

$scripturl = 'http://www.site.ru/cgi-bin/easyrefer.cgi';

# Actual PATH (not the URL) of the log file - include the filename.

$logpath = '/path/to/refer.log';

# Change this to the URL of the site or webpage you want to
# allow referals for

$refersite = 'http://www.site.ru';

# Change this to the name of your site or webpage

$refertitle = 'В помощь веб-мастеру';

# Change this to your e-mail address
# If you're not using PERL 5 on your system ie. version 4
# or earlier, you will need to escape the @ sign like this :
# ( \@ instead of just @ )
#
# Perl 5 users should be able to enter their straight e-mail address

$myemail = 'admin@site.ru';

# How long a message (in characters) do you want to allow
# a visitor to send to their friend?

$maxlength = '40';

#################### STOP HERE! ####################
# #
# You may now upload this file to your server, using #
# the instructions you received in the readme.txt file #
# #
########################################################
# #
# WARNING : Changing anything in this box or below #
# may damage the script. #
# #
########################################################


# Get the form variables

if ($ENV{'REQUEST_METHOD'} eq 'GET') {
$buffer = $ENV{'QUERY_STRING'};
}
else {
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
}

# Break em up into a format the script can read

@pairs = split(/&/, $buffer);
foreach $pair (@pairs) {
($name, $value, $message) = split(/=/, $pair);
$value =~ s/\+/ /g;
$value =~ s/%([0-9A-H]{2})/pack('C',hex($1))/eg;
$FORM{$name} = $value;
}

# Assign shorter variable names
# (Laziness on my part - but I find the longer
# a script gets the more work typing long
# variable names becomes.)

$sender=$FORM{'sender'};
$recip= $FORM{'recip'};
$message= $FORM{'message'};

# Check for empty fields and print error page with a link
# back to the page they were on if there is

if ($FORM{'sender'} eq "") {
print "Content-type: text/html; charset=windows-1251\n\n";
print "<html><head><title>Ошибка - Недостающие Данные</title></head><body $body>\n";
print "<H1>Ошибка - Пустая Область Отправителя</H1>\n";
print "<FORM ACTION=\"$scripturl\" METHOD=\"GET\"><CENTER><TABLE BORDER=1 BGCOLOR=000077><FONT FACE=\"arial,helvetica\" COLOR=\"FAC700\"><TR><B>Расскажи другу про этот сайт!</B></FONT>\n";
print "<TD><FONT FACE=\"arial,helvetica\" COLOR=\"FAC700\"><B>Ваш E-mail</B></FONT></TD><TD><INPUT TYPE=\"text\" name=\"sender\" SIZE=25 VALUE=\"$sender\"></TD></TR><TR>\n";
print "<TD><FONT FACE=\"arial,helvetica\" COLOR=\"FAC700\"><B>E-mail Вашего друга</B></FONT></TD><TD><INPUT TYPE=\"text\" name=\"recip\" SIZE=25 VALUE=\"$recip\"></TD></TR><TR>\n";
print "<TD><FONT FACE=\"arial,helvetica\" COLOR=\"FAC700\"><B>Ваш коментарий</B></FONT></TD><TD><INPUT TYPE=\"text\" name=\"message\" SIZE=25 MAXLENGTH=$maxlength VALUE=\"$message\"></TD></TR></TABLE><INPUT TYPE=\"submit\" VALUE=\"Отправить!\"></CENTER></FORM><P>\n";
print "<A HREF=\"$refersite\">Click Here</A> to return.\n";
print "<P>Powered by <A HREF=\"http://www.getperl.com\">";
print "<B>EasyRefer</B></A> Copyright 1998 by Thomas J. Delorme\n";
print "</body></html>\n";
exit;
}
if ($FORM{'recip'} eq "") {
print "Content-type: text/html; charset=windows-1251\n\n";
print "<html><head><title>Ошибка - Недостающие Данные</title></head><body $body>\n";
print "<H1>Ошибка - Пустая Область Получателя</H1>\n";
print "<FORM ACTION=\"$scripturl\" METHOD=\"GET\"><CENTER><TABLE BORDER=1 BGCOLOR=000077><FONT FACE=\"arial,helvetica\" COLOR=\"FAC700\"><TR><B>Расскажи другу про этот сайт!</B></FONT>\n";
print "<TD><FONT FACE=\"arial,helvetica\" COLOR=\"FAC700\"><B>Ваш E-mail</B></FONT></TD><TD><INPUT TYPE=\"text\" name=\"sender\" SIZE=25 VALUE=\"$sender\"></TD></TR><TR>\n";
print "<TD><FONT FACE=\"arial,helvetica\" COLOR=\"FAC700\"><B>E-mail Вашего друга</B></FONT></TD><TD><INPUT TYPE=\"text\" name=\"recip\" SIZE=25 VALUE=\"$recip\"></TD></TR><TR>\n";
print "<TD><FONT FACE=\"arial,helvetica\" COLOR=\"FAC700\"><B>Ваш коментарий</B></FONT></TD><TD><INPUT TYPE=\"text\" name=\"message\" SIZE=25 MAXLENGTH=$maxlength VALUE=\"$message\"></TD></TR></TABLE><INPUT TYPE=\"submit\" VALUE=\"Отправить!\"></CENTER></FORM><P>\n";
print "<A HREF=\"$refersite\">Click Here</A> to return.\n";
print "<P>Powered by <A HREF=\"http://www.getperl.com\">";
print "<B>EasyRefer</B></A> Copyright 1998 by Thomas J. Delorme\n";
print "</body></html>\n";
exit;
}

if ($FORM{'message'} eq "") {
print "Content-type: text/html; charset=windows-1251\n\n";
print "<html><head><title>Ошибка - Недостающие Данные</title></head><body $body>\n";
print "<H1>Ошибка - Пустая Область Сообщения</H1>\n";
print "<FORM ACTION=\"$scripturl\" METHOD=\"GET\"><CENTER><TABLE BORDER=1 BGCOLOR=000077><FONT FACE=\"arial,helvetica\" COLOR=\"FAC700\"><TR><B>Расскажи другу про этот сайт!</B></FONT>\n";
print "<TD><FONT FACE=\"arial,helvetica\" COLOR=\"FAC700\"><B>Ваш E-mail</B></FONT></TD><TD><INPUT TYPE=\"text\" name=\"sender\" SIZE=25 VALUE=\"$sender\"></TD></TR><TR>\n";
print "<TD><FONT FACE=\"arial,helvetica\" COLOR=\"FAC700\"><B>E-mail Вашего друга</B></FONT></TD><TD><INPUT TYPE=\"text\" name=\"recip\" SIZE=25 VALUE=\"$recip\"></TD></TR><TR>\n";
print "<TD><FONT FACE=\"arial,helvetica\" COLOR=\"FAC700\"><B>Ваш коментарий</B></FONT></TD><TD><INPUT TYPE=\"text\" name=\"message\" SIZE=25 MAXLENGTH=$maxlength VALUE=\"$message\"></TD></TR></TABLE><INPUT TYPE=\"submit\" VALUE=\"Отправить!\"></CENTER></FORM><P>\n";
print "<A HREF=\"$refersite\">Click Here</A> to return.\n";
print "<P>Powered by <A HREF=\"http://www.getperl.com\">";
print "<B>EasyRefer</B></A> Copyright 1998 by Thomas J. Delorme\n";
print "</body></html>\n";
exit;
}

# Only complete the rest if there are no empty form fields

if ($FORM{'message'} ne "" && $FORM{'recip'} ne "" && $FORM{'sender'} ne "") {

# Send e-mail for site recommendation to the Recipient

open (MAIL,"|$mailprogram -t");
print MAIL "To: $recip\n";
print MAIL "From: $sender\n";
print MAIL "Subject: Рекомендую посмотреть этот сайт!\n";
print MAIL "$sender Ваш друг рекомендовал вам посмотреть этот сайт $refertitle!\n\n";
print MAIL "Комментарии :\n$message\n";
print MAIL "$refersite\n\n";
print MAIL "Powered by :\n";
print MAIL "EasyRefer - Copyright 1998 by Thomas J. Delorme\n";
print MAIL "http://www.getperl.com\n";
close(MAIL);

# Send thank-you e-mail to sender for recommending
# your site to a friend.

open (MAIL,"|$mailprogram -t");
print MAIL "To: $sender\n";
print MAIL "From: $myemail\n";
print MAIL "Subject: Спасибо!\n";
print MAIL "Спасибо за рекомендацию сайта $refertitle для $recip!\n\n";
print MAIL "Ваше сообщение:\n$message\n";
print MAIL "$refersite\n\n";
print MAIL "Powered by :\n";
print MAIL "EasyRefer - Copyright 1998 by Thomas J. Delorme\n";
print MAIL "http://www.getperl.com\n";
close(MAIL);

# Send e-mail to you notifying you of the recommendation

open (MAIL,"|$mailprogram -t");
print MAIL "To: $myemail\n";
print MAIL "From: $sender\n";
print MAIL "Subject: Рекомендация сайта\n";
print MAIL "Сообщение отправленно $recip\n\n";
print MAIL "$message\n";
print MAIL "$refersite\n\n";
print MAIL "Powered by :\n";
print MAIL "EasyRefer - Copyright 1998 by Thomas J. Delorme\n";
print MAIL "http://www.getperl.com\n";
close(MAIL);

# Open the log and record the sender and recipient's e-mail addresses

open (LOG, ">>$logpath");
print LOG "$message - $recip referred by $sender\n";
close (LOG);

# Finally, print message sent screen for the sender
# with a link back to the page he was on.

print "Content-type: text/html charset=windows-1251\n\n";
print "<html><head><title>Спасибо - Ваше собщение отправлено</title></head><body $body>\n";
print "<H1>Сообщение отправлено!</H1>\n";
print "<FORM ACTION=\"$scripturl\" METHOD=\"GET\"><CENTER><TABLE BORDER=1 BGCOLOR=000077><FONT FACE=\"arial,helvetica\" COLOR=\"FAC700\"><TR><B>Расскажите еще кому нибудь о сейта $refertitle?</B></FONT>\n";
print "<TD><FONT FACE=\"arial,helvetica\" COLOR=\"FAC700\"><B>Ваш E-mail</B></FONT></TD><TD><INPUT TYPE=\"text\" name=\"sender\" SIZE=25 value=\"$sender\"></TD></TR><TR>\n";
print "<TD><FONT FACE=\"arial,helvetica\" COLOR=\"FAC700\"><B>E-mail Вашего друга</B></FONT></TD><TD><INPUT TYPE=\"text\" name=\"recip\" SIZE=25></TD></TR><TR>\n";
print "<TD><FONT FACE=\"arial,helvetica\" COLOR=\"FAC700\"><B>Ваш коментарий</B></FONT></TD><TD><INPUT TYPE=\"text\" name=\"message\" SIZE=25 MAXLENGTH=$maxlength value=\"$message\"></TD></TR></TABLE><INPUT TYPE=\"submit\" VALUE=\"Отправить!\"></CENTER></FORM><P>\n";
print "<A HREF=\"$refersite\">Click Here</A> to return.\n";
print "<P>Powered by <A HREF=\"http://www.getperl.com\">";
print "<B>EasyRefer</B></A> Copyright 1998 by Thomas J. Delorme\n";
print "</body></html>\n";

}
 
V

Vovochka

Гость
#2
Фактически, у вас все строки кода в кодировке самого скрипта.
Если кодировка скрипта - koi8-r
То преобразовать текст в другую кодировку можно следующим образом:
Код:
use Encode qw(encode decode);
print encode('cp1251', decode('koi8-r', 'Русский текст'));
Вообще смотрите доку к модулю Encode.
Судя по тому, что хтмл и так отдается в cp1251, то скрипт у вас скорее всего в нем же.
А значит и письма по идее должны быть в cp1251.