#!/usr/bin/perl ############################################################################ # jmailsend.pl : ローカルマシン上の`sendmail'プログラムを使用して日本語の # テストメールを送信するスクリプト - MIME-Header-ISO_2022_JPなし版 # Ver.0.9.8 # Copyright T.Nonogaki @Stray Penguin (www.asahi-net.or.jp/~aa4t-nngk/) # USAGE: # jmailsend.pl [-e (jis|sjis|euc|utf8)] [-c N] [-t RECIPIENT] [-h] # # 当スクリプトはroot権限で実行すること! # # 引数はいずれも省略可能で、指定しない場合はスクリプト冒頭で定義されている # デフォルトが適用される。 # -e オプションは送信するメール本文の文字コードで、jis, sjis, euc または # utf8 が指定可能。 # * jisの場合: 7bit形式として送信。 # * eucの場合: base64エンコードして base64形式として送信。 # * sjis/utf8: MIMEエンコードして quoted-printable形式として送信。 # Subjectヘッダに関しては、utf8の時のみ "MIME-B-encoded UTF-8" でエン # コード、それ以外は常に "MIME-B-encoded ISO-2022-JP" でエンコードする。 # -c オプションは、何通送るかを数値で指定。 # -t オプションは宛先メールアドレス。 # -h は簡単なヘルプを表示。 ############################################################################ use strict qw(vars refs); use warnings; no warnings qw(once); use Getopt::Std qw(getopts); use Encode qw(encode decode from_to); use MIME::Base64 qw(encode_base64); use MIME::QuotedPrint qw(encode_qp); our $version = '0.9.8'; our @me = ($0 =~ m!([^/]+)$!); our $sendmail_bin = "/usr/sbin/sendmail"; our %opts; ## 設定項目 ## # 送信メールの文字コードのデフォルト our $charset = 'jis'; # 送り主として使用するメールアドレス our $sender = 'root@localhost'; # 宛先メールアドレスのデフォルト our $rcpt = 'penguin@hoge.cxm'; # 送信メール数のデフォルト our $count = 1; ## 設定終了 ## our $content_text = <<"EOM"; これはテスト用メッセージです。 ここからは英文。一部はスペースで段下げして表示されます。 print FILEHANDLE LIST print LIST print Prints a string or a list of strings. Returns true if success- ful. FILEHANDLE may be a scalar variable name, in which case the variable contains the name of or a reference to the file- handle, thus introducing one level of indirection. (NOTE: If FILEHANDLE is a variable and the next token is a term, it may be misinterpreted as an operator unless you interpose a \"+\" or put parentheses around the arguments.) If FILEHANDLE is omit- ted, prints by default to standard output (or to the last selected output channel--see \"select\"). If LIST is also omit- ted, prints \$_ to the currently selected output channel. To set the default output channel to something other than STDOUT use the select operation. The current value of \$, (if any) is printed between each LIST item. The current value of \"\$\\\" (if any) is printed after the entire LIST has been printed. Because print takes a LIST, anything in the LIST is evaluated in list context, and any subroutine that you call will have one or more of its expressions evaluated in list context. Also be careful not to follow the print keyword with a left parenthesis unless you want the corresponding right parenthesis to termi- nate the arguments to the print--interpose a \"+\" or put paren- theses around all the arguments. EOM sub VERSION_MESSAGE { print $version,"\n"; exit 0; } sub HELP_MESSAGE { print <<'EOM'; USAGE: jmailsend.pl [-e (jis|sjis|euc|utf8)] [-c N] [-t RECIPIENT] [-h] EOM exit 0; } sub handle_jp { my $str = shift; my $jopt = shift; if ($charset eq 'jis') { from_to($str,'utf8','iso-2022-jp'); } elsif ($charset eq 'sjis') { from_to($str,'utf8','shiftjis'); } elsif ($charset eq 'euc') { from_to($str,'utf8','euc-jp'); } if (defined($jopt)) { if ($jopt eq 'mime') { if ($charset eq 'utf8') { $str = encode('MIME-B',decode('utf8',$str)); } elsif ($charset eq 'euc') { $str = '=?ISO-2022-JP?B?' . encode_base64(encode('iso-2022-jp',decode('euc-jp',$str)),'') . '?='; } else { $str = '=?ISO-2022-JP?B?' . encode_base64(encode('iso-2022-jp',decode('cp932',$str)),'') . '?='; } } elsif ($jopt eq 'base64') { $str = encode_base64($str); } elsif ($jopt eq 'qp') { $str = encode_qp($str); } } return $str; } sub send_mail { my $rep_str = "テスト "; $rep_str .= shift; open(SM, "|$sendmail_bin -t -f$sender") || return 1; print SM "From: $sender\n"; print SM "To: $rcpt\n"; if ($charset eq 'jis') { print SM "Content-Type: text/plain; charset=ISO-2022-JP\n"; print SM "Content-Transfer-Encoding: 7bit\n"; } else { if ($charset eq 'euc') { print SM "Content-Type: text/plain; charset=EUC-JP\n"; print SM "Content-Transfer-Encoding: base64\n"; goto sm_end; } elsif ($charset eq 'utf8') { print SM "Content-Type: text/plain; charset=UTF-8\n"; } else { print SM "Content-Type: text/plain; charset=Shift_JIS\n"; } print SM "Content-Transfer-Encoding: quoted-printable\n"; } sm_end: print SM "Subject: ", handle_jp($rep_str,'mime'); print SM "\n"; print SM "$content_text"; print SM ".\n"; close(SM); return 0; } #### getopts('he:c:t:',\%opts); HELP_MESSAGE() if defined($opts{'h'}); if (defined($opts{'e'})) { if ($opts{'e'} =~ /^s(?:hift-?)?jis$/i) { $charset = 'sjis'; } elsif ($opts{'e'} =~ /^euc(?:-?jp)?$/i) { $charset = 'euc'; } elsif ($opts{'e'} =~ /^utf(?:-?8)?$/i) { $charset = 'utf8'; } else { print "unknown charset `$opts{'e'}\'.", " Do `$me[0] -h\'.\n"; exit 1; } } $count = $opts{'c'} if (defined($opts{'c'}) && $opts{'c'}); $rcpt = $opts{'t'} if (defined($opts{'t'}) && $opts{'t'}); exit 1 unless (-x $sendmail_bin); if ($charset eq 'jis') { $content_text = handle_jp($content_text); } else { if ($charset eq 'euc') { $content_text = handle_jp($content_text,'base64'); } else { $content_text = handle_jp($content_text,'qp'); } } my $i = 1; for ($i=1; $i<=$count; $i++) { exit 1 if send_mail($i); } exit 0;