Reputation: 17032
I'm having a problem with utf-8 support on a perl script I'm writing. The script is meant to send html email messages. The html messages are saved in UTF-8 format inside a PostgreSQL database. Everything seems to be working but still I get corruption sometimes when I receive an email from the script - "�".
In the beginning of the script I have:
#!/usr/bin/perl -w
use utf8;
use Encode;
use MIME::Base64;
use MIME::Lite;
my $connection = DBI->connect('dbi:Pg:dbname='.$db_name.';host='.$db_host.'', $db_user,$db_pass, { AutoCommit=>1, PrintError => 1, pg_enable_utf8 => 1 });
my $fetchHtml = $connection->prepare('SELECT * FROM emails ORDER BY n_id DESC LIMIT 1');
$fetchHtml->execute();
my $message = $fetchHtml->fetchrow_hashref();
my $sendMsg = MIME::Lite->build(
Encoding => 'quoted-printable',
Type => 'multipart',
To => '<[email protected]>',
From => '<[email protected]>',
Subject => encode("MIME-B", $message->{'title'}),
Data => decode_entities($message->{'html'})
);
$sendMsg->attr("Content-Type" => "text/html; charset=utf-8;");
$sendMsg->send_by_smtp('127.0.0.1', Timeout =>30, Debug => 0, SkipBad => 1);
I'm wondering what I'm doing wrong and why do I keep on getting the cool "�" sign ? :)
Another thing is that I get this exception when I execute the script:
Uncaught exception from user code:
Wide character in subroutine entry at /usr/lib/perl5/site_perl/5.10.0/MIME/Lite.pm line 2259.
at /usr/lib/perl5/site_perl/5.10.0/MIME/Lite.pm line 2259
MIME::Lite::print_simple_body('MIME::Lite=HASH(0xa51b9c8)', 'MIME::Lite::SMTP=GLOB(0xa5b8888)', 1) called at /usr/lib/perl5/site_perl/5.10.0/MIME/Lite.pm line 2191
MIME::Lite::print_body('MIME::Lite=HASH(0xa51b9c8)', 'MIME::Lite::SMTP=GLOB(0xa5b8888)', 1) called at /usr/lib/perl5/site_perl/5.10.0/MIME/Lite.pm line 2126
MIME::Lite::print_for_smtp('MIME::Lite=HASH(0xa51b9c8)', 'MIME::Lite::SMTP=GLOB(0xa5b8888)') called at /usr/lib/perl5/site_perl/5.10.0/MIME/Lite.pm line 2897
MIME::Lite::send_by_smtp('MIME::Lite=HASH(0xa51b9c8)', 'bla.example.com', 'Timeout', 30, 'Debug', 0, 'SkipBad', 1) called at ./advanced-daemon.pl line 354
main::send_mail('Subject Title' <webmaster@testing>', '[email protected]', 'HASH(0xa518630)') called at ./advanced-daemon.pl line 225
main::sendEmailsToSubscribers('DBI::db=HASH(0xa517f40)', 24, 'HASH(0xa518630)') called at ./advanced-daemon.pl line 136
I can't understand what exactly is the problem but I think it's related to the utf8.. Any help would be pretty much appreciated.. :)
Upvotes: 2
Views: 2308
Reputation: 1322
In my code I use the multipart
type and here is the version which seems to work correctly.
my $mail_htm = 'Text of the email with utf8 characters like these: ľščťžýáíúäô';
my $msg = MIME::Lite->new(
From =>'[email protected]',
'Reply-To'=>'[email protected]',
To =>'[email protected]',
Subject => 'Simple ascii non-utf8 subject',
Type =>'multipart/mixed'
);
my $msg_body = MIME::Lite->new(
Type =>'multipart/alternative'
);
$msg_body->attach(
Type =>'text/html; charset=UTF-8',
Encoding=>'quoted-printable',
Data =>$mail_htm
);
$msg->attach($msg_body);
I didn't play much with utf8 encoding for the subject as I didn't need it that much and several solutions I found around didn't work.
Upvotes: 0
Reputation: 126
First you need use utf8
only if you have unicode character in the quellcode. Then decode_entities($message->{'html'})
ist also wrong. Use only $message->{'html'}
.
The database must be utf8 by default. Then add Encoding => '8bit'
. That works nice for me.
Your MIME::Lite is false: see on http://www.perlmonks.org/?node_id=105262 for a nice example
Upvotes: 1