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:
If we send with lots of people in the to:, cc: or bcc: fields many email servers will assume the mail is spam and reject it
MS Excel with MS Word and MS Outlook are supposed to allow you to send form letters as email. The option is still in Word. You pick Mail Merge and select an Excel file with all your data, create a form letter and Word will generate a separate mail for each person listed in your Excel file through Outlook. Unfortunately the ability of Outlook to be able to do this was abused by viruses and that feature was disabled off.
Even if we sent from Outlook or pretty much any other software that we could use for form email we ran into problems with Encodings. Half the people we invite are Japanese and must receive email in Japanese. You'd think maybe you could use Unicode (UTF-8) but not everyone is running a modern e-mail client.
Some people are on old cell phones. Some are even stuck using Netscape 4.7 at their office. Send the wrong encoding and they just get a message full of garbage or mojibake as it's called in Japanese.
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.
The most common way I've seen sending email in perl is to pipe directly to sendmail. Unfortunately that means your perl has to be running on a server with sendmail setup.
Even if you sent up sendmail many servers will reject your mail if it's not from the same server as the account you're sending from. In other words if the from address is ourparty@yahoo.com the server sending the mail better be on the yahoo.com domain otherwise it will get rejected by many servers as spam.
To deal with the previous problem you need to send through smtp using an external server (like Yahoo for example). Most mail servers require authentication but most examples I could find online for sending smtp email through perl didn't show how to talk to a smtp server that requires authentication.
Once you get the mail sending you next have to deal with the encoding issues. Subject lines that are not in English as well as Japanese contents need special encodings. No where does this seem to be documented well, at least not in English.
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; myaccountInfo = { 'smtp' => 'smtp.yahoo.com', 'username' => 'myaccount@yahoo.com', 'password' => 'mypassword', 'fromaddress' => 'myaccount@yahoo.com', }; mymailData = { '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', }; myresult = send_mail(accountInfo, mailData); print"result = result\n"; sub send_mail { my(accountInfo, mailData) = @_; mysubject = mailData->{'subject'}; mybody = mailData->{'body'}; mytype = mailData->{'type'}; mymsg = MIME::Lite->new( From => accountInfo->{'fromaddress'}, To => mailData->{'toaddress'}, Subject => mailData->{'subject'}, Type => mailData->{'type'}, Encoding => mailData->{'encoding'}, Data => mailData->{'body'}, ); mymsgstr = msg->as_string(); myresult = send SMTP::Auth => msgstr, accountInfo->{'smtp'}, accountInfo->{'username'}, accountInfo->{'password'}; returnresult; } sub read_file { myfilename = _[0]; mydata = ""; myresult = 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
"鈴木恵子<keiko_suzuki@foobar.com>" then you're going to need to encode those as well. I just settled for leaving
those as the email address only to save frustration.