Tuesday, October 6, 2009
Tuesday, May 6, 2008
Create entry by mail on movable type 4
mail to blog's entry,it passed at XPsp2 :
perl -w pop3_to_blog_utf8.pl
-------------------------------------------------------------------
#!/usr/local/bin/perl
# This file is encoding by utf8,do not use other encoding!
#
# These codes are originally comes from
http://forums.sixapart.com/index.php?showtopic=63059
# but has been completely rewritten.
# ----------------------------------------------------------
# I rewrite it because it doesn't support wide character.
# Gao Feng 2007.use it freely.
# ----------------------------------------------------------
# There is an another MT::Import::Mbox ,I am not try it.
# Same as above,a program named mtmail-0.5,a mail->mt worked as mailfilter
# come from http://www.zonageek.com/software/files/mt/mtmail-0.5/mtmail.html
# is a more complex case,but it has been outdated.
use strict;
use File::Temp qw ( :mktemp );
use File::Basename;
# use Net::Blogger;
# use XML::Simple;
# use LWP;
# use SOAP;
use Net::MovableType;
# Created by Gao Feng 2007
# the Mail::Box is a large module,
# you'd better use it on your machine.(Not exsited on hosting machine)
use Carp;
use Data::Dump;
use Mail::Box;
use Mail::Box::Manager;
use Mail::Box::POP3;
use Mail::Address;
# use IO::Scalar;
use Encode qw( encode decode );
use Encode::Guess;
sub loopmail{
my $mailproccessor=shift||return;
my $mailconfig=shift;
open(LOGFILE, ">>pop3_to_blog_log.log");
# Net::POP3 Like Net::SMTP,it do nothing but receive messsage
# You must parse the body your self,
# Mail::Box is better.
# But you still have to decoding the header and the body.
# for those who mother language is not English.
my $mgr = Mail::Box::Manager->new;
# my $url = 'pop3://name:password@pop.perl.com';
# print $mailconfig->{'type'},"\n",$mailconfig->{'username'},"\n",$mailconfig->{'password'},"\n",$mailconfig->{'server_name'},"\n";
my $pop = $mgr->open(
type => Encode::encode("iso-8859-1", $mailconfig->{'type'}),
username => Encode::encode("iso-8859-1", $mailconfig->{'username'}),
password => Encode::encode("iso-8859-1", $mailconfig->{'password'}),
server_name => Encode::encode("iso-8859-1", $mailconfig->{'server_name'})
);
foreach my $message ($pop->messages) {
my $Subject = ( $message->get('Subject') || '<no subject>' ) ;
$Subject = Encode::decode( "MIME-Header",$Subject ) ;
$Subject = Encode::encode('utf8', $Subject);
# Perl can't detect a string is utf8 encoding
# use hak instead ,suppose only one address existed.
my $From = $message->get('From');
my $Sender = $message->get('To');
$From =~s/^.*<(.*)>$/$1/;
$Sender =~s/^.*<(.*)>$/$1/;
# pay attention ->body() print empty line
# may be caused by utf8 encoding
# If you want these codes working everywhere,convert the string to utf8
my $BodyLines = $message->decoded();
my $Body = join('',@{ $BodyLines->{'MMBL_array'} } );
my $id = &$mailproccessor($mailconfig,$Sender,$From,$Subject,$Body);
# --------------------------------------------------------------
# See the documents of Encode::PerlIO;
# open(OUT, ">:utf8","mail.txt") or die $!;
# if the string is not encoding ,you can use below
# The code below convert string to utf8 file automotively,
# but Encode::from_to method doen't work!
# open(OUT, ">:encoding(euc-cn)","mail.txt") or die $!;
# open(OUT, ">:encoding(utf8)","mail.txt") or die $!;
# print OUT $Subject;
# close OUT;
# ---------------------------------------------------------------
if (!$id){
print "post fail! \n";
next;
}
# Timestamp for the log.
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime();
$year = sprintf("%02d", $year % 100); # Make 2 digit year.
$mon += 1; #month is 0 based i.e. 0 = Jan, 11 = Dec
printf(LOGFILE "%02d-%02d-%02d %02d:%02d:%02d ->Posting message" ,
$mday,$mon,$year,$hour,$min,$sec);
print(LOGFILE "\n");
print LOGFILE "$Subject \n";
print LOGFILE "Message posted as $id .\n";
print "Message posted.\n", $Subject;
$message->delete;
print LOGFILE "Message deleted as $id. \n";
}
# Close POP box..
$pop->close();
print LOGFILE "Finished processing mail. \n";
close(LOGFILE);
}
