首页 > 解决方案 > 使用 Perl 问题将电子邮件转发到 Epson 打印机“电子邮件打印”

问题描述

我目前正在使用 Cpanel 设置电子邮件转发,该电子邮件转发通过 CPanel 电子邮件转发“Pipe to Program”中的功能进行。在这个 perl 脚本中,我抓取标题并将它们替换为 Epson 电子邮件打印地址,因为 Epson 打印机不喜欢直接转发。但是,我遇到的问题是一次发送给多个用户会导致错误,并且它不喜欢超过 1 个收件人。

我的代码如下:

#!/usr/bin/perl
use strict;

# Real email address for the printer
my $email = 'example@domain.com';

my $sm;
open($sm, "|/usr/sbin/sendmail -t");

my $in_header = 1;

while (my $line = <STDIN>) {
        chomp $line;

        # Empty line while in headers means end of headers
        if ($in_header && $line eq '') {
                $in_header = 0;
        }

        # Replace To: field if we're in headers
        if ($in_header && $line =~ m/^To: /) {
                $line = "To: $email";
        }

        # Pass through to sendmail
        print $sm "$line\n";
}

close($sm);

我觉得我的问题的根源来自我的代码中的这一行:

# Replace To: field if we're in headers
if ($in_header && $line =~ m/^To: /) {
    $line = "To: $email";
}

我必须承认,我在网上找到了这段代码片段,但我对 Perl 完全不熟悉,以便找到一个可行的解决方案,以便能够毫无问题地转发到多封电子邮件。任何关于我可以从哪里开始的迹象,即使一个完整的解决方案不明确,也会非常有帮助。

资源:

https://www.cravingtech.com/how-to-setup-epson-email-print-using-your-own-domain-name.html

标签: perlemailprintingepson

解决方案


#!/usr/bin/perl
use strict;

my $email = 'example@domain.com';

my $sm;
open($sm, "|/usr/sbin/sendmail -t");

my $in_header = 1;
my $in_to = 0;

while (my $line = <STDIN>) {
    chomp $line;
    # Empty line while in headers means end of headers
    if ($in_header && $line eq '') {
            $in_header = 0;
    }
    # Email Header
    if ($in_header){
        if($line =~ m/^To: /) {
            $in_to = 1;
            $line = "To: $email";
        } elsif($in_to) {
            if($line =~ /:/){
                $in_to = 0;
            }else{
                next;
            }
        }   
    }   
    print $sm "$line\n";
}
close($sm);

经过数小时的反复试验,这最终成为了我的解决方案。

只是在这里发布这个,以防有人遇到我的小众问题哈哈。

谢谢。


推荐阅读