接收邮件附件的脚本。perl。

sh/bash/dash/ksh/zsh等Shell脚本
回复
头像
solcomo
帖子: 2838
注册时间: 2007-04-25 13:12

接收邮件附件的脚本。perl。

#1

帖子 solcomo » 2008-07-12 19:05

...大部分从这抄的
http://topic.csdn.net/u/20070213/16/acb ... 2073054200

需要安装模块

代码: 全选

sudo cpan MIME::Parser

代码: 全选

#!/usr/bin/perl -w

use strict;
use Net::POP3;
use MIME::Parser;
use MIME::Entity;

my $mailserver = "pop.163.com";
my $username   = "aaaaaaaaaaaaaaaaaaaaaaaa";
my $password   = "vvvvvvvvvvvvvvvvvvvvv";

if ($ARGV[0] && ($ARGV[0] =~ /--help|-h/i )) {
printf <<HELP;
cmail: Check mail and download the parts.
Usage:
    cmail [OPTION]
Options:
    NONE	    show all mails and download all parts in current directory.
    -c		    show the total number of email.
    -v		    show all mails.
    -d		    delete all mails.
    -h, --help      show this message.

HELP
    exit 0;
};

my $pop     = Net::POP3->new($mailserver, Timeout => 60) or die "Failed to connect to $mailserver\n";
my $tot_msg = $pop->login($username, $password)          or die "Failed to authenticate $username\n";
my $all     = $pop->list                                 or die "Failed to retreive list of avaible mseages\n";
my $parser  = MIME::Parser->new;

printf("\n   You are $tot_msg messages.\n\n");
printf("————————————————————————————————————————————————\n");

if ($ARGV[0] && ($ARGV[0] eq '-c' )) {
	$pop->quit;
	exit 0;
}

if ($ARGV[0] && $ARGV[0] eq '-d') {
	my $msgnums = $pop->list;
	foreach my $msgnum (keys %$msgnums) {
		$pop->delete($msgnum);
	}
	$pop->quit;
	exit 0;
}

foreach my $num (keys %$all) {
	my $msg     = $pop->get($num);
	my $entity  = $parser->parse_data($msg);

	$entity->head->decode;
	print "Subject: ",$entity->head->get('Subject');
	print "From:    ",$entity->head->get('From');
	print "To:      ",$entity->head->get('To');
	print "Date:    ",$entity->head->get('Date');
	print "MIME:    ",$entity->mime_type,"\n";
	print "Parts:   ",scalar $entity->parts,"\n";
	for my $part ($entity->parts) {
        	print "\t ",$part->mime_type,"\t ",$part->bodyhandle,"\n";
	}

	my $numparts = $entity->parts;
	if (!$numparts) {
		my $body    = $entity->bodyhandle();
		my $content = join("", $body->as_lines);
		$content    = $content;
		print "Text:\n";
		print $content,"\n";
		$body->purge;
	}
	else {
		my $part_ent = $entity-> parts(0);
		if ($part_ent->parts) {
			$part_ent = $part_ent->parts(0);
		}
		my $body     = $part_ent->bodyhandle();
		my $content  = join("", $body->as_lines);
		$content     = $content;
		print "Text:\n";
		print $content,"\n";
		printf "------------------------------------------------\n";
	}

	if ($ARGV[0] && $ARGV[0] eq '-v') {
		$entity->purge();
	}
	else {
		for my $part ($entity->parts) {
			my $body = $part->bodyhandle();
			my $path = '';
			if($body) {
				$path = $body->path();
			}
			else {
				$path = '';
			}
			print $path,"\n"; 
		}
	}
	printf "————————————————————————————————————————————————\n";
}
$pop->quit;
上次由 solcomo 在 2008-07-12 20:02,总共编辑 1 次。
头像
bones7456
帖子: 8495
注册时间: 2006-04-12 20:05
来自: 杭州
联系:

#2

帖子 bones7456 » 2008-07-12 19:07

:shock: :P
关注我的blog: ε==3
头像
HuntXu
帖子: 5776
注册时间: 2007-09-29 3:09

#3

帖子 HuntXu » 2008-07-12 19:08

收藏了先~
HUNT Unfortunately No Talent...
头像
eexpress
帖子: 58428
注册时间: 2005-08-14 21:55
来自: 长沙

#4

帖子 eexpress » 2008-07-12 19:09

复杂了。何必
● 鸣学
头像
solcomo
帖子: 2838
注册时间: 2007-04-25 13:12

#5

帖子 solcomo » 2008-07-12 19:12

eexpress 写了:复杂了。何必
强烈要求ee说出简单的方法 :D
头像
eexpress
帖子: 58428
注册时间: 2005-08-14 21:55
来自: 长沙

#6

帖子 eexpress » 2008-07-12 19:15

我一贯遵循人性化的原则。我那脚本就最简单了。脚本是为了方便使用的,挂fm或者热键才发挥最大效果。
● 鸣学
头像
solcomo
帖子: 2838
注册时间: 2007-04-25 13:12

#7

帖子 solcomo » 2008-07-12 19:28

:shock: 这个是接收的不是发送的...
头像
eexpress
帖子: 58428
注册时间: 2005-08-14 21:55
来自: 长沙

#8

帖子 eexpress » 2008-07-12 19:30

穷到没客户端了?
● 鸣学
头像
BigSnake.NET
帖子: 12522
注册时间: 2006-07-02 11:16
来自: 廣州
联系:

#9

帖子 BigSnake.NET » 2008-07-12 19:31

eexpress 写了:穷到没客户端了?
其实发送用的也是客户端。。。
^_^ ~~~
要理解递归,首先要理解递归。

地球人都知道,理论上,理论跟实际是没有差别的,但实际上,理论跟实际的差别是相当大滴。
头像
solcomo
帖子: 2838
注册时间: 2007-04-25 13:12

#10

帖子 solcomo » 2008-07-12 19:36

eexpress 写了:穷到没客户端了?
是没客户端
以前都是直接上网页 :lol:
头像
Tenyears
帖子: 2245
注册时间: 2005-06-30 15:46
来自: 成都

#11

帖子 Tenyears » 2008-07-12 19:44

2n是什么?
Humanity to others // 己所不欲,勿施与人
Laptop: ThinkPad X220 --- Win7 Home / Ubuntu 12.04
Desktop: Win7/Ubuntu 12.04
Server: Ubuntu12.04
头像
solcomo
帖子: 2838
注册时间: 2007-04-25 13:12

#12

帖子 solcomo » 2008-07-12 20:02

就是2奶了 :lol:
头像
eexpress
帖子: 58428
注册时间: 2005-08-14 21:55
来自: 长沙

#13

帖子 eexpress » 2008-07-12 20:57

BigSnake.NET 写了:
eexpress 写了:穷到没客户端了?
其实发送用的也是客户端。。。
球猫没老婆。理解不到我那脚本的意思。可球猫却有2n。
● 鸣学
回复