|
Sending Japanese EMail from Perl For the last few years I've helped organize a relatively large party here in Tokyo every Summer. This last summer 230 people came. We need to send e-mail to all the people we invite and you'd be surprised how hard it actually is to send email to that many people. Some of the issues are:
The first 2 problems meant we couldn't just use Outlook and the last problem also seemed an issue no mater what software we tried. The solution I tried was to write my own perl script to send the mail but there were oodles of problems.
So, after 3 years of using this script and running into issues and fixing those issues I think it finally works. It uses smtp which avoids the first problem. It authenticates smtp (username/password) so it fixes the second problem. And, finally, after much trial and error it appears to use a format that works all the time (jis / iso-2022-jp) I doubt many people need something like this but if you do hopefully google brought you here and I managed to save you a few headaches #!/usr/bin/perl
# # This program sends an email in Japanese through an SMTP server # that requires authentication. Hopefully it does it in # a way that no recipient will have problems receiving # correctly. (ie, no mojibake) # # The subject is hard coded below # The body is read from the file "msg-ja.txt" # # Both the subject and the file msg-ja.txt are expected to be in shift-jis format # which is the default format for Windows text in Japanese mode. In other words # if you are running Japanese Windows XP or English XP with your # "Language for non-unicode programs" set to "Japanese" then notepad by default # will save text in shift-jis and you can use that Japanese here # use warnings; use strict; use MIME::Lite; use Email::Send; use Encode; use Encode::JP; use JCode; my $accountInfo = { 'smtp' => 'smtp.yahoo.com', 'username' => 'myaccount@yahoo.com', 'password' => 'mypassword', 'fromaddress' => 'myaccount@yahoo.com', }; my $mailData = { 'subject' => Jcode->new("これは日本語もメールです", 'sjis')->mime_encode, 'body' => encode('iso-2022-jp', decode('shiftjis', read_file("msg-ja.txt"))), 'type' => "text/plain; charset=\"iso-2022-jp\"", 'encoding' => "7bit", 'toaddress' =>'match@greggman.com', }; my $result = send_mail($accountInfo, $mailData); print "result = $result\n"; sub send_mail { my ($accountInfo, $mailData) = @_; my $subject = $mailData->{'subject'}; my $body = $mailData->{'body'}; my $type = $mailData->{'type'}; my $msg = MIME::Lite->new( From => $accountInfo->{'fromaddress'}, To => $mailData->{'toaddress'}, Subject => $mailData->{'subject'}, Type => $mailData->{'type'}, Encoding => $mailData->{'encoding'}, Data => $mailData->{'body'}, ); my $msgstr = $msg->as_string(); my $result = send SMTP::Auth => $msgstr, $accountInfo->{'smtp'}, $accountInfo->{'username'}, $accountInfo->{'password'}; return $result; } sub read_file { my $filename = $_[0]; my $data = ""; my $result = open(CONTENT, $filename); if (! $result) { die ("*** ERROR: can't open $filename: $!"); } else { local($/) = undef; $data = <CONTENT>; close(CONTENT); } return ($data); } On thing to be aware of. As it says the above code is expected to be saved in shift-jis format. Unfortnately unless you know what you are doing if you just cut and paste the source above there's a possibility it won't get pasted in shift-jis. If you are running Window XP with your "Language for non-Unicode programs" set to Japanese then you can copy and paste into Notepad and then when you save choose the "ANSI" Encoding. That appears to work. Note: from and to lines will also need special encoding if your are putting more than just the address in. For example if instead of "keiko_suzuki@foobar.com" you are using something like All images not copyright me copyright their respective companies. Everything else copyright me. |
Comments:
|