170 likes | 183 Views
A sample mail view program in Perl that allows users to view and sort their mails, and read mail contents with attachments.
E N D
Homework 2-Mail Reader Perl Programming
A sample mail in the mailbox (1) From liuyh@nabsd.cs.nctu.edu.tw Fri Apr 13 18:05:44 2007 Return-Path: <liuyh@nabsd.cs.nctu.edu.tw> X-Original-To: liuyh@nabsd.cs.nctu.edu.tw Delivered-To: liuyh@nabsd.cs.nctu.edu.tw Received: by nabsd.cs.nctu.edu.tw (Postfix, from userid 1003) id C014B3B4CF9; Fri, 13 Apr 2007 18:05:44 +0800 (CST) Date: Fri, 13 Apr 2007 18:05:44 +0800 From: =?big5?B?vEKlzrW+?= Liu Yung Hsiang <liuyh@nabsd.cs.nctu.edu.tw> To: =?big5?B?vEKlzrW+?= Liu Yung Hsiang <liuyh@nabsd.cs.nctu.edu.tw> Subject: =?big5?B?p6ikpKTlsNo=?= Message-ID: <20070413100544.GA61511@nabsd.cs.nctu.edu.tw> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="qMm9M+Fa2AknHoGS“ Content-Disposition: inline Content-Transfer-Encoding: 8bit
A sample mail in the mailbox (2) User-Agent: Mutt/1.5.14 (2007-02-12) Status: RO Content-Length: 338 Lines: 16 --qMm9M+Fa2AknHoGS Content-Type: text/plain; charset=big5 Content-Disposition: inline Content-Transfer-Encoding: 8bit 誰說中文不能夾... 叫你夾你就夾 --qMm9M+Fa2AknHoGS Content-Type: text/plain; charset=big5 Content-Disposition: attachment; filename=chinese Content-Transfer-Encoding: 8bit 裡面也要寫中文讓他夾啊 --qMm9M+Fa2AknHoGS--
A sample mail in mutt … From: 劉用翔 Liu Yung Hsiang liuyh@nabsd.cs.nctu.edu.tw To: 劉用翔 Liu Yung Hsiang <liuyh@nabsd.cs.nctu.edu.tw> Subject: 夾中文啊 … [-- Attachment #1 --] [-- Type: text/plain, Encoding: 8bit, Size: 0.1K --] 誰說中文不能夾... 叫你夾你就夾 [-- Attachment #2: chinese --] [-- Type: text/plain, Encoding: 8bit, Size: 0.1K --] 裡面也要寫中文讓他夾啊
Some sample programs CPAN samples
Read mail list from a mailbox ($MAIL) #!/usr/bin/perl use Mail::Box::Mbox; use MIME::Head; my $folder = Mail::Box::Mbox->new(lock_type => 'NONE'); my $head = MIME::Head->new; foreach $msg ($folder->messages('ACTIVE')) { $head->replace('Subject', $msg->{MM_head}->get('Subject'), 'From', $msg->{MM_head}->get('From')); $head->decode; $subject = $head->get('Subject'); $from = $head->get('From'); chomp($subject, $from); }
Read contents of a mail (1) #!/usr/bin/perl +use Email::MIME::Attachment::Stripper; foreach $msg ($folder->messages('ACTIVE')) { $mail = Email::MIME::Attachment::Stripper->new($msg->{MM_head}. $msg->{MM_body}); $message = $mail->message # process $message } • Data Structure message body parts mycrlf ct header Array of messages discrete composite attributes
Read contents of a mail (2) • Access message(hash reference) contents • Body is a scalar reference • Parts is a array reference • Ct is a hash reference • Discrete is a scalar date • From reference to value • Assign reference to a scalar data • Access value by add appropriate prefix character • Ex: $body = $message->{body}; # $$body $parts = $message->{parts}; # @$parts $ct = $message->{ct}; # %$ct $discrete = $message->{ct}->{discrete}; # $discrete
Read attachments of a mail #!/usr/bin/perl use Mail::Box::Mbox; use MIME::Head; use Email::MIME::Attachment::Stripper; my $folder = Mail::Box::Mbox->new(lock_type => 'NONE'); my $head = MIME::Head->new; foreach $msg ($folder->messages('ACTIVE')) { $mail = Email::MIME::Attachment::Stripper->new($msg->{MM_head}. $msg->{MM_body}); @attachments = $mail->attachments; foreach $attach (@attachments) { print $attach->{content_type}, $attach->{filename}, $attach->{payload}; } }
Requirements-Part 1: Mail List (1) • 1.0. Can specify a mailbox file name in the command line • You might save your mails in another mailbox file, such as ~/mbox. • The program should open your mailbox by default. ($MAIL) • 1.1. Enter the program, show all mails in a list • Each mail has its own order(number), subject, and source. • Use perl format to format your mail list output. • Need to take care of the out-of-page condition. (man perlform) • Ex: 編號 標題 寄件者 ==== ================== ============== 1 這是第一封信 First<mymail@gmail 2 This is the second mail Second<oishi@gmail
Requirements-Part 1: Mail List (2) • 1.2. User Interface • You can define your own commands. • 1.3. Support commands-Users can • Sorting all mails in the list by subjects, sources, or time(by default). • Select a mail (by order) to read. > sort subject > sort from > sort time > read 1 > read 2
Requirements-Part 2: Display a Mail (1) • 2.1. Output a page of mail contents once • Wait for user to request the next page.(Maybe just press enter) • Use perl format to format your mail content output. • Also need to take care of the out-of-page condition. • Wait in the last page.(For saving attachments) • 2.2. Show the source, subject and attachments(if it appear) • At the top of every page, that is, the tops of all pages are the same. • 2.3. Attachments • If attachments can be shown as text, output them in the mail content. • User can save some attachments as any file name they like.
Requirements-Part 2: Display a Mail (2) • Output Example: • Save Attachments Example: From: First<mymail@gmail.com> Subject: 這是第一封信 Attachment 1: att1.txt <text/plain> Attachment 2: att2.avi <video/x-msvideo> ====================================== Mail Content … … … (More) 或 (End) The last line (End)> save 1 first.txt > save 2 enigma.avi
Requirements-Part 3: Searching (1) • In mail list • You can specify some keywords to find out the mails which match these conditions. • The condition can be assigned to either subject or source. • The whole conditions can be combined with ‘and’(match all), or ‘or’(match one). • Accept the input as the perl regular expression. • Then, the mail list contains only matched mails. • Ex: > search subject Exam subject condition > search source TAs source condition > search and combined with ‘and’ > search or combined with ‘or’
Bonus • 1. Add a command for users to delete the specific mail • 2. Add searching function while reading a mail • Ex: • 3. Use Curses::UI CPAN to design a more > delete 2 From: First<mymail@gmail.com> Subject: 這是第一封信 ====================================== Mail Content … (More) 或 (End) The last line /search_this search command