如何从Mail :: Message获取电子邮件内容?

时间:2009-10-22 16:28:57

标签: perl email

被修改

现在我的代码是这样的:

#!/usr/bin/perl

# import packages
use Net::POP3;
use Getopt::Long;
use Mail::Message;
use strict;
use warnings;

# read command line options
# display usage message in case of error
GetOptions ('h|host=s' => \$host,
            'u|user=s' => \$user,
            'p|pass=s' => \$pass) or die("Input error. Try calling me with: -h <host> -u <username> -p <password>");

# file operations
open($email_file, ">>", "Mail.txt");

# initiate connection
# default timeout = 120 sec
$conn = Net::POP3->new($host) or die("ERROR: Unable to connect.\n");

# login
$numMsg = $conn->login($user, $pass) or die("ERROR: Unable to login.\n");

# get message numbers
# iterate over list and print first 20 lines of each
if ($numMsg > 0) {
    $msgList = $conn->list();
    foreach $msg (keys(%$msgList)) {
        $rawdata = $conn->get($msg);
        my $msg_obj = Mail::Message->read($rawdata);
        my $body = $msg_obj->body;
        print $email_file @body;
        print $email_file "\n====================================================\n";
    }
} else {
    print "Mailbox is empty.\n";   
}

# close connection
$conn->quit();
close($email_file);

但是当我尝试执行它时,我得到了这个:

[ubuntu@eeepc:~/Desktop/mail] ./get.pl -h pop.vix.terra.com.br -u nathanpc -p secret
Global symbol "$host" requires explicit package name at ./get.pl line 12.
Global symbol "$user" requires explicit package name at ./get.pl line 13.
Global symbol "$pass" requires explicit package name at ./get.pl line 14.
Global symbol "$email_file" requires explicit package name at ./get.pl line 17.
Global symbol "$conn" requires explicit package name at ./get.pl line 21.
Global symbol "$host" requires explicit package name at ./get.pl line 21.
Global symbol "$numMsg" requires explicit package name at ./get.pl line 24.
Global symbol "$conn" requires explicit package name at ./get.pl line 24.
Global symbol "$user" requires explicit package name at ./get.pl line 24.
Global symbol "$pass" requires explicit package name at ./get.pl line 24.
Global symbol "$numMsg" requires explicit package name at ./get.pl line 28.
Global symbol "$msgList" requires explicit package name at ./get.pl line 29.
Global symbol "$conn" requires explicit package name at ./get.pl line 29.
Global symbol "$msg" requires explicit package name at ./get.pl line 30.
Global symbol "$msgList" requires explicit package name at ./get.pl line 30.
Global symbol "$rawdata" requires explicit package name at ./get.pl line 31.
Global symbol "$conn" requires explicit package name at ./get.pl line 31.
Global symbol "$msg" requires explicit package name at ./get.pl line 31.
Global symbol "$rawdata" requires explicit package name at ./get.pl line 32.
Global symbol "$email_file" requires explicit package name at ./get.pl line 34.
Global symbol "@body" requires explicit package name at ./get.pl line 34.
Global symbol "$email_file" requires explicit package name at ./get.pl line 35.
Global symbol "$conn" requires explicit package name at ./get.pl line 42.
Global symbol "$email_file" requires explicit package name at ./get.pl line 43.
Execution of ./get.pl aborted due to compilation errors.
[ubuntu@eeepc:~/Desktop/mail]

原始

您好,

我正在学习Perl并同时做一个自制项目,我将把它用于我家人的活动,但是当我使用Mail :: Message来获取eMails的主体时我什么都没得到。看我的代码:

#!/usr/bin/perl

# import packages
use Net::POP3;
use Getopt::Long;
use Mail::Message;

# read command line options
# display usage message in case of error
GetOptions ('h|host=s' => \$host,
            'u|user=s' => \$user,
            'p|pass=s' => \$pass) or die("Input error. Try calling me with: -h <host> -u <username> -p <password>");

# file operations
open(file, ">>", "Mail.txt");

# initiate connection
# default timeout = 120 sec
$conn = Net::POP3->new($host) or die("ERROR: Unable to connect.\n");

# login
$numMsg = $conn->login($user, $pass) or die("ERROR: Unable to login.\n");

# get message numbers
# iterate over list and print first 20 lines of each
if ($numMsg > 0) {
    $msgList = $conn->list();
    foreach $msg (keys(%$msgList)) {
        my $msg_obj = Mail::Message::->read($rawdata);
        my $body = $msg_obj->body;
        print file @body;
        print file "\n====================================================\n";
    }
} else {
    print "Mailbox is empty.\n";   
}

# close connection
$conn->quit();
close(file);

我用来存储电子邮件的文件:

====================================================

====================================================

====================================================

我做错了什么?感谢。

3 个答案:

答案 0 :(得分:2)

$rawdata来自哪里?你有没有忘记说

之类的话
    $rawdata = $conn->get($msg);

脚本顶部的

use strictuse warnings可能帮助您抓住了这一点。

答案 1 :(得分:2)

$raw_data中没有任何内容,因为您从未分配过它。

@body(数组变量)中没有任何内容。你把东西放在$body(一个标量变量)中。

这些是use strict为您找到的各种问题。 :)

答案 2 :(得分:0)

表单中的所有消息:

Global symbol "$host" requires explicit package name at ./get.pl line 12.

来自您使用use strict启用严格变量的事实,这要求您在使用变量之前声明变量。

尝试添加use diagnostics以获取有关错误消息的扩展说明。

您还可以运行splain来解释错误消息。运行脚本并将stderr捕获到文件get.pl 2> errors中,然后运行splain errors

对于你的错误,splain说:

全局符号“$ host”需要在./get.pl第12行显示包名称(#1)     (F)你说过“使用严格的变量”,它表示所有变量     必须是词法范围(使用“我的”),事先声明使用     “我们的”,或明确限定说明哪个包的全局变量     在(使用“::”)。

结帐perldoc strictperldoc warningsperldoc perllexwarn,最后perldoc -f my

脚本的第一位(在所有使用语句之后)修复:

# read command line options
# display usage message in case of error

# predeclaring all variables with `my`
my $host = 'localhost';  # you can set default values for Getopt::Long this way. 
my $user;
my $pass;

GetOptions ('h|host=s' => \$host,
            'u|user=s' => \$user,
            'p|pass=s' => \$pass,
) or die("Input error. Try calling me with: -h <host> -u <username> -p <password>");
相关问题