編輯如何從Mail :: Message中獲取電子郵件內容?
現在我的代碼是這樣的:
#!/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);
但是當我試圖執行它我得到這個:
[[email protected]:~/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.
[[email protected]:~/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);
而且我使用存儲電子郵件的文件:
====================================================
====================================================
====================================================
什麼,我做錯了什麼?謝謝。
這是很好的進展。我看到你已經使用'my'來聲明一些變量。 'use strict'要求你使用'my'來表示你使用的所有變量。 – mob 2009-10-22 18:37:17
非常感謝!現在我學會了自己,我可以繼續我的項目給我的家人。謝謝! – 2009-10-22 18:52:08
:(爲什麼下來投票 – 2009-10-22 18:55:29