dbmathis has asked for the wisdom of the Perl Monks concerning the following question:

Hello People,

I am inputting russian / cyrillic into an e-mail form / program I wrote and when it arrives, part of the output is mangled for the same russian / cyrillic input. For example the person inputs their name (Увеличение массы мозга) in russian / cyrillic and when the e-mail arrives the name is displayed properly in the body of the e-mail but not in the attached jpg file or the subject of the e-mail. All three are using the name that was input.

Here is the code:
#================================================================ #- Send image #================================================================ # sub send_image { my $message_body; # Adjust sender, recipient and your SMTP mailhost my $from_address = $from; my $to_address = $email; # Adjust subject and body message if ( $testmode ) { $message_body = "Thank you for performing the transfer test. I +t's good until <b>$msg_body_expdate</b>.<br /><br />Visit <a href=\"h +ttp://www./specialoffer\">http://www./specialoffer</a> now to take ad +vantage of this exciting special one week offer of 10% off<br /><br / +>"; } else { $message_body = "Attached is the chi card you ordered. It's go +od until <b>$msg_body_expdate</b>."; } my $subject = "Chi-Card delivery service"; # Create the multipart container my $msg = MIME::Lite->new ( From => $from_address, To => $to_address, BCC => $bcc, Subject => $subject, Type =>'multipart/mixed' ) or die "Error creating multipart cont +ainer: $!\n"; # Add the text message part $msg->attach ( Type => 'text/html', Data => qq{ $message_body } ) or die "Error adding the text message part: $!\n"; # Add the excel file $msg->attach ( Type => 'image/jpeg', #Encodxing => 'base64', Path => $chicardcustomed, Filename => $filename, Disposition => 'attachment' ) or die "Error adding $chicardcustomed: $!\n"; # Now send $msg->send("sendmail") or die "Error e-mailing: $filename.\n"; } #================================================================ #- Send info #================================================================ # sub send_info { # Adjust sender, recipient and your SMTP mailhost my $from_address = $from; my $to_address = $infoemail; # Adjust subject and body message my $message_body = "<h2>The following visitor requested informatio +n.</h2><b>Name:</b> $string<br><b>E-mail:</b> $email<br>"; my $subject = "$string is requesting information."; # Create the multipart container my $msg = MIME::Lite->new ( From => $from_address, To => $to_address, BCC => $bcc, Subject => $subject, Type =>'multipart/mixed' ) or die "Error creating multipart cont +ainer: $!\n"; # Add the text message part $msg->attach ( Type => 'text/html', Data => qq{ $message_body } ) or die "Error adding the text message part: $!\n"; # Now send $msg->send("sendmail") or die "Error e-mailing: $!\n"; }

The output can be seen here for the image e-mail and the separate info e-mail per the code listed above.

How do I make the jpg name and e-mail subjects display in russian / cyrillic? I have been trying for days.

Best Regards, David

Replies are listed 'Best First'.
Re: Problem with russian / cyrillic in e-mail program.
by Corion (Patriarch) on Apr 03, 2010 at 17:13 UTC

    If you have characters outside the 7-bit ASCII range in headers, you need to base-64 encode the headers, and prefix =?...?B:

    use MIME::Base64; my $charset = 'your charset'; my $subject = 'your subject, containing non-ASCII chars'; my $header = "=?$charset?B?".encode_base64($subject)."?=";

      ... or just let Encode::MIME::Header do the work for you.

      Alexander

      --
      Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

      I am not sure how to implement this into my code. Also do I need to check for non-ascii?

        You can use it as well for ASCII.

        I'm not sure what you mean by "not sure how to implement this into my code" - instead of passing in the subject literally, you construct the encoded subject string and pass that one in. Where are you having problems exactly?

Re: Problem with russian / cyrillic in e-mail program.
by Gangabass (Vicar) on Apr 04, 2010 at 02:46 UTC
    my $msg = MIME::Lite->new( From => "=?UTF-8?B?" . encode_base64( 'Some russian + text', "" ) . "?=" . ' <inf@irkutsk.2gis.ru>', To => $email_address, Subject => "=?UTF-8?B?" . encode_base64( 'Some russian + text', "" ) . "?=", Type => 'multipart/mixed', ); $msg->attach( Type => 'text/html; charset=UTF-8', Data => $message_html, ); $msg->attach( Type => 'application/msword', Path => $base_path . '\\' . $dir_name . '\\' . $fi +le_name . ".doc", Filename => "=?UTF-8?B?" . encode_base64( 'Some russia +n text', "" ) . "?=" . '.doc', ); $msg->attr('content-type.charset' => 'UTF-8'); $msg->send;

      Hi, thanks for the post, I tried it like this and I am still getting the same mangled subject arriving in my inbox.

      #================================================================ #- Send info #================================================================ # sub send_info { # Adjust sender, recipient and your SMTP mailhos my $from_address = $from; my $to_address = $infoemail; # Adjust subject and body message my $message_body = "<h2>The following visitor requested informatio +n.</h2><b>Name:</b> $string<br><b>E-mail:</b> $email<br>"; my $subject = "$string is requesting information."; # Create the multipart container my $msg = MIME::Lite->new ( From => $from_address, To => $to_address, BCC => $bcc, Subject => "=?UTF-8?B?" . encode_base64 +( $subject, "" ) . "?=", Type =>'multipart/mixed' ) or die "Error creating multipart cont +ainer: $!\n"; # Add the text message part $msg->attach ( Type => 'text/html', Data => qq{ $message_body } ) or die "Error adding the text message part: $!\n"; # Now send $msg->attr('content-type.charset' => 'UTF-8'); $msg->send("sendmail") or die "Error e-mailing: $!\n"; }

        Are you sure your subject is UTF-8 encoded?