in reply to Character encoding in emails
To try and replicate the issue away from the full production scripts, I've written a test with the sending code copied directly from the production code. A few variables have changed names and a couple of other tweaks to make it run but the sending code is as faithful to production as I can get it. The sending mechanism is extremely similar, so it's unlikely to be that. The sending method is toggled in the code below by setting $METHOD true or false.
Method true stores the message in a database table. The data types and DB engine are the same as the production DB. There's a bit of substitution and then it's sent.
Method false bypasses storing and retrieving from the database. It does a similar substitution and sends.
Running the script in a web environment sends email, in both cases, without the emojis being properly reproduced. So, I set up CRON to run the script...again, both methods produce emails with corrupted emojis.
It appears that it's not the DB that's affecting it, and it's not the CRON vs web environment that's affecting it...
I'm finding this really tricky to debug as I cannot think of a way of inspecting what is happening between the data being passed to MIME::Lite and it appearing in my email client.
#!/usr/bin/perl use CGI::Carp qw(fatalsToBrowser); use strict; use warnings; use lib "/home/..../..../public_test/cgi-bin"; # Toggle sending method my $METHOD = 0; use MIME::Lite; use incl::HTML; if ($ENV{'GATEWAY_INTERFACE'}) { print "Content-type: text/plain\n\n" ; } $/ = undef; my $subject = 'Testing email'; my $message = <DATA>; my $fname = 'Andrew'; my $sname = 'Test'; my $tomail = 'ian@l******t.co.uk'; if ($METHOD) { $dbh->do("CREATE TEMPORARY TABLE Email_Test ( `from` VARCHAR(100) NULL DEFAULT NULL, `email` VARCHAR(100) NULL DEFAULT NULL, `subject` VARCHAR(100) NULL DEFAULT NULL, `message` BLOB NULL DEFAULT NULL ) ENGINE = MyISAM"); $dbh->do("INSERT INTO Email_Test SET `from` = 'Bod', email = 'ian\ +@boddison.com', subject = ?, message = ?", undef, $subject, $message); my ($from, $email, $subject, $message) = $dbh->selectrow_array("SE +LECT `from`, email, subject, message FROM Email_Test LIMIT 1"); $subject =~ s/%FNAME%/$fname/g; # There are $subject =~ s/%SNAME%/$sname/g; # more of $message =~ s/%FNAME%/$fname/g; # these in $message =~ s/%SNAME%/$sname/g; # production my $mail = MIME::Lite->new( To => "\"$fname\" <$tomail>", From => "\"$from\" <$email>", Subject => "$subject", Type => 'text/html', Data => $message, ); my $check = $mail->send; print "Failed to send to $fname $sname ($tomail)\n" unless ($check +); } else { my $name = "$fname $sname"; $subject=~s/%FNAME%/$fname/g; $subject=~s/%SNAME%/$sname/g; $message=~s/%FNAME%/$fname/g; $message=~s/%SNAME%/$sname/g; my $mime = MIME::Lite->new( From => "\"Ian Boddison\"<ian\@boddison.com>", To => "\"$name\"<$tomail>", Subject => $subject, # Type => 'multipart/alternative', # ); # $mime->attach( # Type => 'text/plain', # Data => $data{'plaintext'}, # ); # $mime->attach( Type => 'text/html', Data => $message, ); my $chk; eval { $chk = $mime->send() }; if ($@) { print "Error - $@\n"; } } print "\nComplete...\n"; __DATA__ <html> <body> <p>🥁 <b>Introducing On Radar ‑ where quir +k meets impact!</b> 🚀</p> </body> </html>
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Test code (was: Re: Character encoding in emails)
by afoken (Chancellor) on Dec 09, 2023 at 10:41 UTC | |
by Bod (Parson) on Dec 09, 2023 at 14:37 UTC | |
|
Re: Test code (was: Re: Character encoding in emails)
by NERDVANA (Priest) on Dec 09, 2023 at 10:53 UTC | |
by afoken (Chancellor) on Dec 09, 2023 at 11:41 UTC | |
by Bod (Parson) on Dec 09, 2023 at 14:41 UTC |