# ActivePerl 5.16によるスケジュールファイルのHTML化とPHSへの送信 # Written by Minato Nakazawa # 5th April 2014 # CPANでの依存モジュールは # MIME::Base64, Authen::SASL, Net::SMTPS # コマンドラインで,例えば,cpan install MIME::Base64のようにすればよい。 use Encode; $title_string = 'スケジュール'; decode('euc-jp', $title_string); ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time); $time = sprintf("%02d:%02d:%02d",$hour,$min,$sec); $year += 1900; $mon += 1; @youbi = ('日','月','火','水','木','金','土'); $jdate = "$year年$mon月$mday日 ($youbi[$wday]) at $time"; decode('euc-jp', $jdate); # 以下はこのPerlスクリプトからの参照 $datafile = './sc.txt'; # スケジュールデータ(タブ区切りテキストファイル) # データ形式は,//Header//から//Schedule//までがメール本文と詳細htmlに # 書き込まれるメモ,それ以下がタブ区切りで # 日付 曜日 開始時刻 終了時刻 場所 内容 # という体裁。内容は||で区切り,見出しと詳細を含める。以下は例(行頭の#は不要)。 # 2014/4/7 月 14:50 16:20 神戸 教育||環境・食品・産業衛生学(1) # $htmlfile = './myplan.html'; # 詳細予定表html([P:][X:]を処理しない) $csvfile = './schedule.csv'; # 予定表csv(GoogleCalender/Jorteに取込) $openhtmlfile = './schedule.html'; # 公開予定表([P:]=PRIVATE,[X:]=SECRET)html $mode = 0; # 公開html用ヘッダ作成 $ohline = '' . "\n"; $ohline .= '' . "\n"; $ohline .= '' . "\n"; $ohline .= "\n\n"; # $ohline .= "\n"; # cssを使うとき $ohline .= "予定表(Schedule)\n"; $ohline .= "\n

Top

\n"; $ohline .= "

予定表(Schedule)

\n

最終更新:$jdate

"; decode('euc-jp', $ohline); # 詳細html用ヘッダ作成 $hline = '' . "\n"; $hline .= ''; $hline .= "\nMy plan\n"; $hline .= ''; $hline .= "\n\n
";
decode('euc-jp', $hline);
$mhline = "<>\n";

$bline = "\n" . '' . "\n";
$obline = "\n" . '
' . "\n"; $csvline = 'dtstart,dtend,time_start,time_end,title,timeslot,holiday,event_timezone,calender_rule,rrule,on_holiday_rule,content,location,importance,completion,char_color,icon_id,mark,mark_text,reminders' . "\n"; $gcsvline = 'Subject, Start Date, Start Time, End Date, End Time, All Day Event, Reminder On/Off, Reminder Date, Reminder Time, Meeting Organizer, Description, Location, Private'. "\n"; open($in, "<$datafile"); while ($line = <$in>) { decode('euc-jp', $line); chomp($line); if ($line =~ /\/\/Header\/\//) { $mode = 1; } elsif ($line =~ /\/\/Schedule\/\//) { $mode = 2; } elsif ($mode == 1) { $hline .= "$line\n"; $mhline .= "$line\n"; } elsif ($mode == 2) { $line =~ s/\t/<\/th>\n"; $obline .= "\n"; $mode = 3; } elsif ($mode == 3) { ($datex,$dayx,$startx,$endx,$placex,$contentx) = split(/\t/,$line); if (length($startx)<1) { $timeslotx="1"; $timezonex="UTC"; $alarm = ""; } else { $timeslotx="0"; $timezonex="Asia/Tokyo"; $alarm = "10"; } ($titlex,$contentz) = split(/\|\|/,$contentx); if (length($titlex)<1) { $titlex = "その他"; } $csvline .= "$datex,$datex,$startx,$endx,$titlex,$timeslotx,0,$timezonex,2,,0,$contentz,$placex,1,0,0,,,,$alarm\n"; $contentg = $contentz; $contentg =~ s/\[P:\].*/\[PRIVATE\]/g; $contentg =~ s/\[X:\].*/\[SECRET\]/g; $gcsvline .= "$titlex, $datex, $startx, $datex, $endx, , , , , , $contentg, $placex\n"; $line =~ s/\t/<\/td>\n"; $obline .= "\n"; } } close($in); $bline .= "
/g; $bline .= "
$line
$line
/g; $oline = $line; $oline =~ s/\[P:\].*/\[PRIVATE\]/g; $oline =~ s/\[X:\].*/\[SECRET\]/g; $bline .= "
$line
$oline
\n"; $obline .= "\n"; $hline .= "
"; $hline .= "$bline\n"; $ohline .= "$obline\n"; encode('euc-jp', $hline); open(OUT, ">$htmlfile"); print OUT $hline; close(OUT); encode('euc-jp', $ohline); open(OUT, ">$openhtmlfile"); print OUT $ohline; close(OUT); my $zcsvline = decode 'euc-jp', $csvline, 1; my $ycsvline = encode 'utf8', $zcsvline, 1; my $ggcsvline = decode 'euc-jp', $gcsvline, 1; my $ocsvline = encode 'utf8', $ggcsvline, 1; open(OUT,">$csvfile"); print OUT $ycsvline; # Jorte用 # print OUT $ocsvline; # Google Calender用 close(OUT); use MIME::Base64; use MIME::Entity; use Net::SMTPS; use Authen::SASL; my $smtpserver = 'smtp.gmail.com'; my $smtpport = '587'; my $From = '送信元アドレス'; my $To = '宛先アドレス'; my $user = 'gmailのアカウント'; my $passwd = 'gmailのパスワード'; # Create object my $ssl = 'starttls'; my $smtp = Net::SMTPS->new( $smtpserver, Port=>$smtpport, doSSL=>$ssl, # ここ大事 SSL_version=>'TLSv1', # ここも大事 SSL_verify_mode=>'SSL_VERIFY_PEER', # 最近のgmailでは必須 Debug=>0 # $smtpserverとの応答を確認したいときは1に ); die "ERROR: Connection failed." if not $smtp; my $sasl = Authen::SASL->new( 'mechanisms' => 'CRAM-MD5', 'callback' => { 'user' => $user, 'pass' => $passwd } ); # SMTP-AUTH認証 $smtp->auth($sasl) or die("Could not authenticate with gmail.\n"); # 以下メール送信。 $smtp->mail($From); $smtp->to($To); # Subject以下は日本語を含む可能性があるのでMIME使用 $smtp->data(); # Built Data (Create data by MIME::Entity) $subject_org = "最新$title_string($jdate)"; my $zsubject_org = decode 'euc-jp', $subject_org, 1; $body = "$mhline\n$subject_orgを添付。\n"; my $zbody = decode 'euc-jp', $body, 1; my $subject_org = encode 'jis', $zsubject_org, 1; $subject = '=?ISO-2022-JP?B?' . encode_base64($subject_org, '') . '?='; my $body = encode 'jis', $zbody, 1; my $mime = MIME::Entity->build( From=>$From, To=>$To, Subject=>$subject, Type=>'text/plain;charset="iso-2022-jp"', Data=>[$body], Encoding=>"7bit" ); # body # Attached file (html) $mime->attach( Path=>$htmlfile, Type=>'text/html', Encoding=>'base64', Filename=>'schedule.html' ); # Attached file (csv) $mime->attach( Path=>$csvfile, Type=>'text/comma-separated-values', Encoding=>'base64', Filename=>'schedule_data.csv' ); $smtp->datasend($mime->stringify); # transfer strings # Data termination and send mail $smtp->dataend(); #Quit SMTP connection $smtp->quit; print "Completed.\n"; exit;