develooper Front page | perl.pep | Postings from April 2010

how to encode correctly email

From:
fakessh
Date:
April 17, 2010 14:41
Subject:
how to encode correctly email
Message ID:
3b9fbd8d5210d12c4a71a7710197b661@127.0.0.1
hello list
hello guru of perl
hello all users of this list

I work a little on the webmail of perlwebmail @ jaos
who suffers from a bad encoding of messages in the interface

@jaos impleùente encoding like that

package PerlWebmail::Message;
use base qw(MIME::Lite);

my $crlf = qr/\x0a\x0d|\x0d\x0a|\x0a|\x0d/; #I know this is not correct 
                                      #Unix uses \x0a for line endings.
Windows uses \x0d\x0a. MacOS
                                      #(IIRC) uses \x0d. Which OS uses
\x0a\x0d?


my $quote_prefix = '>';

sub new
{
  my $class = shift;

  unless (grep { /^Encoding$/ } @_) {  #to raise /^Encoding$/i.
    push @_, ('Encoding','8bit');
  }

  my $self = $class->SUPER::new(@_);

  $self->replace('X-Mailer','PerlWebmail (http://software.jaos.org)');

  return $self;
}

sub from
{
  my ($self, $from) = @_;
  if ($from) {
    $self->add('To',$from);
  }
  $self->get('From');
}

sub to
{
  my ($self, $to) = @_;
  if ($to) {
    $self->add('To',$to);
  }
  $self->get('To');
}

sub cc
{
  my ($self, $cc) = @_;
  if ($cc) {
    $self->add('Cc',$cc);
  }
  $self->get('Cc');
}

sub bcc
{
  my ($self, $bcc) = @_;
  if ($cc) {
    $self->add('Bcc',$bcc);
  }
  $self->get('Bcc');
}

sub subject
{
  my ($self, $subject) = @_;
  if ($subject) {
    $self->replace('Subject',$subject);
  }
  $self->get('Subject');
}

sub body
{
  my ($self, $body) = @_;
  if ($body) {
    $self->build( Type => 'TEXT', Encoding => '8bit', Data => $body);
  }
  $self->body_as_string;
}

sub asString
{
  shift->as_string;
}

#my $crlf = qr/\x0a\x0d|\x0d\x0a|\x0a|\x0d/;
#sub _strip_sig { reverse +(split /$crlf\s*--$crlf/o, reverse(pop), 2)[1]
}

sub quote
{
  my $body = shift;
  $body =~ s/($crlf)/$1$quote_prefix /g;
  "\n\n$quote_prefix $body";
}

1;


I think a better realize being an encoding like this
#!/usr/bin/perl

use Encode qw(all);
my $ENC_ASCII = 'ASCII';
# When all other defaults are exhausted, use UTF-8
my $result = undef;
      eval{
              $result = Encode::encode_utf8($text);
                      };
                      if($@){
                      }
              if($result){
   return $result;
      }
# Something is seriously wrong if we get to here
 return encode($ENC_ASCII, $text, undef);


ou 

# IO layer: $handle now decodes all strings upon reading
open my $handle, '<:encoding(UTF-8)', $file;

ou 
# same
binmode $handle, ':encoding(UTF-8)';


how do you recommend to encode messages as much as input output

thanks for advice
thanks for all your feddbacks


ps : http://minilien.fr/a0lhww



nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About