perl - 如何使用 Perl 的 Net::IMAP::Simple 转发电子邮件

标签 perl email imap

似乎有数百个类似的问题。我想我已经读过所有这些,但似乎没有一个回答这个问题。我已经查看了 FetchMail,但认为我无法将其安装在我的 DreamHost 共享服务器上。我查看了 Mail::Box,但不知道如何登录并收集我要转发的邮件。 我目前正在尝试执行如下所示的操作...显然这已被简化以使其更易于遵循并且可能包含错误。但本质上,我正在获取已读邮件的正文,并尝试将其作为转发电子邮件的正文发布。

邮件已正确阅读和转发,但查看转发的邮件时,所有格式均错误,有时会被截断。

use Net::IMAP::Simple;
use Email::Simple;
use IO::Socket::SSL;
use Email::Address;

# Connect
my $imap = Net::IMAP::Simple->new($Server, port  => 993,  use_ssl => 1,) || die "Unable to connect to IMAP: $Net::IMAP::Simple::errstr\n";

# Log in
if ( !$imap->login( $L, $P ) ) {print  "Login failed for $L: " . $imap->errstr . "\n"; exit(64);}

# Look in the inbox
my $nm = $imap->select('Inbox');

## Iterate through all messages
for ( my $i = 1 ; $i <= $nm ; $i++ ) {
my $es = Email::Simple->new( join '', @{ $imap->get($i) } );
$Body = $es->body;

use Net::SMTP::SSL;
my $smtp;
if (not $smtp = Net::SMTP::SSL->new($Server, Port => 465, Debug => 0)) {warn "Could not connect to $Server!\n"; $method = 'Server Fail'; $error=$@;} else {

if (not $smtp->auth($L2, $P2)) {warn "Authentication Failed! Login: $L sending from $from TO $to\n"; $method = "Authentication Fail Login: $L2 $P2 sending from $from TO $to\n"; $error=$@;} else {

$smtp->mail($from . "\n");
$smtp->to($to . "\n");
$smtp->data();
$smtp->datasend("MIME-Version: 1.0\n");
$smtp->datasend("From: " . $from . "\n");
$smtp->datasend("To: " . $to . "\n");
$smtp->datasend("Subject: " . $subject . "\n");
$smtp->datasend("\n");
$smtp->datasend($Body . "\n");
$smtp->dataend();
# Check it was sent correctly 
$result = $smtp->message();
if ($result !~ /OK/i) {$error = $result; if ($result =~ /quota exceeded/) {$method = 'Quota Blocking'; } else {$method = 'Bad Address';}}

$smtp->quit;

# Copy this message
$imap->create_mailbox( "Inbox.Processed" );
$imap->copy( $i, "Inbox.Processed" ) or die $imap->errstr;
$imap->delete( $i );

最佳答案

这就是你所要做的。只需运行 Net::Imap::Simple 的两个“ session ”,然后将消息从一个 session 复制到另一个 session 即可。

    use Net::IMAP::Simple;
    use IO::Socket::SSL;
    # Conntect to the TO mailbox       
    $imap2 = Net::IMAP::Simple->new('smtp.SomeServer.com', port  => 993,  use_ssl => 1,) || die "Unable to connect to IMAP: $Net::IMAP::Simple::errstr\n";
    $L2 = '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="fbafb49a9f9f899e8888bb969a9297d5989496" rel="noreferrer noopener nofollow">[email protected]</a>';
    $P2 = 'password';
    # Log in
    if ( !$imap2->login( $L2, $P2 ) ) {print  "Login failed for $L2: " . $imap2->errstr . "\n"; exit(64);}
    
    # Connect to the From Mail box
    $imap = Net::IMAP::Simple->new('smtp.SomeOtherServer.com', port  => 993,  use_ssl => 1,) || die "Unable to connect to IMAP: $Net::IMAP::Simple::errstr\n";
     $L = '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="8fc9ddc0c2eeebebfdeafcfccfe2eee6e3a1ece0e2" rel="noreferrer noopener nofollow">[email protected]</a>';
    $P = 'password';   
    # Log in
    if ( !$imap->login( $L, $P ) ) {print  "Login failed for $L: " . $imap->errstr . "\n"; exit(64);}
    
    # Look in the inbox
    my $nm = $imap->select('Inbox');
    
    ## Iterate through all messages
    for ( my $i = 1 ; $i <= $nm ; $i++ ) {
    $message = $imap->get( $i ) or die $imap->errstr;
    @flags = $imap->msg_flags($i);
    
    # Move this message into a processed folder
    $imap->create_mailbox( "Inbox.Processed" );
    $imap->copy( $i, "Inbox.Processed" ) or die $imap->errstr;
    $imap->delete( $i );
    
    # Copy to other mail address ...
    $imap2->put( 'Inbox', $message, @flags ) or die $imap2->errstr;
    
    }
    
    # Disconnect
    $imap->quit;
    $imap2->quit;

关于perl - 如何使用 Perl 的 Net::IMAP::Simple 转发电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71166778/

相关文章:

spring - 将内联图像添加到 spring 电子邮件

c# - 如何获取指定的消息标签?

php - PHP 脚本可以反序列化使用 Perl 创建的可存储文件吗?

php - 附件名称和文件扩展名在电子邮件 *.eml 中不起作用

android - 在android中以编程方式启动电子邮件客户端并将电子邮件地址传递给客户端

azure - 对于 Azure Active Directory 中的注册应用程序,Office 365 Exchange Online 权限不可见

php - 从 php 下载 gmail 附件

linux - fork 后线程安全吗?

perl - 这些对 Perl 打印的调用有何不同?

perl - 从 Moose 类中获取所有属性作为散列的更好方法