我正在尝试为我的本地乐队时事通讯设置此脚本.
目前,有人发送了一封要求添加的电子邮件,我们手动将其添加到我设置的新闻邮件中.(这要归功于我在这里找到的帮助!)
现在的目的是让我的脚本登录到我为服务器上的列表设置的电子邮件帐户,获取信息以自动添加电子邮件.
我知道有很多应用程序可以做到这一点,但我想学习自己.
我已经有"添加到列表"工作时,从BUT下方的标题(从)返回一个电子邮件地址,有时标题(来自)是一个名称而不是电子邮件地址(eg "persons name" is returned from persons name<email@address> but, not the <email@address>.)
现在,我对下面的方法并没有一丝不苟,但是,它有点着名......
我阅读了这些模块上的所有文档,但是我找不到任何东西可以随时获取电子邮件.
有人可以帮我吗?由于我在努力学习Perl,因此非常感谢详细的例子.
#!/usr/bin/perl -w
##########
use CGI;
use Net::IMAP::Simple;
use Email::Simple;
use IO::Socket::SSL; #optional i think if no ssl is needed
use strict;
use CGI::Carp qw(fatalsToBrowser warningsToBrowser);
######################################################
# fill in your details here
my $username = '#########';
my $password = '#############';
my $mailhost = '##############';
#######################################################
print CGI::header();
# Connect
my $imap = Net::IMAP::Simple->new($mailhost, port=> 143, use_ssl => 0, ) || die "Unable to connect to IMAP: $Net::IMAP::Simple::errstr\n";
# Log in
if ( !$imap->login( $username, $password ) ) {
print STDERR "Login failed: " . $imap->errstr . "\n";
exit(64);
}
# Look in the the INBOX
my $nm = $imap->select('INBOX');
# How many messages are there?
my ($unseen, $recent, $num_messages) = $imap->status();
print "unseen: $unseen, <br />recent: $recent, <br />total: $num_messages<br />\n\n";
## Iterate through unseen messages
for ( my $i = 1 ; $i <= $nm ; $i++ ) {
if ( $imap->seen($i) ) {
my $es = Email::Simple->new( join '', @{ $imap->top($i) } );
printf( "[%03d] %s\n\t%s\n", $i, $es->header('From'), $es->header('Subject'));
print "<br />";
next;
}## in the long version these are pushed into different arrays for experimenting purposes
else {
my $es = Email::Simple->new( join '', @{ $imap->top($i) } );
printf( "[%03d] %s\n\t%s\n", $i, $es->header('From'), $es->header('Subject'));
print "<br />";
}
}
# Disconnect
$imap->quit;
exit;
Run Code Online (Sandbox Code Playgroud)
use Email::Address;
my @addresses = Email::Address->parse('persons name <email@address>');
print $addresses[0]->address;
Run Code Online (Sandbox Code Playgroud)
该parse方法返回一个数组,所以上面的方法适合我.