Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

This is a brief summary of code that I've found to send UTF-8 Unicode e-mails that can be sent through SMTP servers that only support 7-bit encoding.

Creating UTF-8 Formatted E-mail Using MIME::Entity

Many SMTP servers will not permit 8-bit characters. UTF-8 encoding requires 8-bit support because the high bit of the first byte is used to indicate successive bytes forming part of the same character.

Assume you have some text containing unicode. For example:

my $data = "Goodbye in Japanese is \x{3055}\x{3088}\x{306A}\x{3089}.";

When creating an e-mail use the "quoted-printable" form of encoding for the data which will correctly encode the UTF-8 characters into 7-bit form. E-mail headers, however, must be encoded in the "MIME-Header" form if they contain any UTF-8 characters (which would often be in the case of foreign language subject headings).

The 7-bit version of the e-mail is obtained using the stringify function.

use Encode; use MIME::Entity; my $entity = MIME::Entity->build( Type => "text/plain", Charset => "UTF-8", Encoding => "quoted-printable", Data => Encode::encode( "UTF-8", $data ), From => Encode::encode( "MIME-Header", $sender ), To => Encode::encode( "MIME-Header", $recipient ), Subject => Encode::encode( "MIME-Header", $subject ), );

Sending the Encoded Message Using Net::SMTP

Next, it is a relatively trivial matter of sending the appropriately coded message to a SMTP server (using Net::SMTP):

use Net::SMTP; my $smtp = Net::SMTP->new( "localhost", Hello => 'localhost.localdomain', Timeout => 15, Debug => 1, ); $smtp->mail( $sender ); if ( $smtp->recipient( $recipient ) ) { $smtp->data(); my $msg = $entity->stringify; while ( $msg =~ m/([^\r\n]*)(\r\n|\n\r|\r|\n)?/g ) { my $line = ( defined($1) ? $1 : "" ) . "\r\n"; $smtp->datasend( $line ); } if ( $smtp->dataend() ) { print( STDERR, "Message sent!\n" ); } else { print( STDERR, "Failure sending data.\n" ); } } else { print( STDERR, "Rejected recipient.\n" ); } $smtp->quit();

See Also

Update: corrected formatting.


In reply to Sending a UTF-8 (Unicode) E-mail by monarch

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!
  • 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, 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, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others studying the Monastery: (11)
    As of 2014-07-11 13:09 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      When choosing user names for websites, I prefer to use:








      Results (224 votes), past polls