Отредактированный
Теперь мой код выглядит так:
#!/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, чтобы получить только тело письма, я ничего не получаю , Смотрите мой код:
#!/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);
И файл, который я использую для хранения писем:
====================================================
====================================================
====================================================
Что я делаю не так? Спасибо.