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>&#129345; <b>Introducing On&nbsp;Radar&nbsp;&#8209;&nbsp;where quir +k meets&nbsp;impact!</b>&nbsp;&#128640;</p> </body> </html>

In reply to Test code (was: Re: Character encoding in emails) by Bod
in thread Character encoding in emails by Bod

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.