perlemailprintingepson

Forwarding Email to Epson Printer "Email Print" with Perl Issue


I am currently using Cpanel to setup an email forward that goes through a feature in CPanel email forwarding "Pipe to Program". In this perl script, I grab the headers and replace them with the Epson email print address, as Epson printers do not like direct forwarding. However, I am having the issue where sending to multiple users at once causes errors and it does not like more than 1 recipient.

My code is below:

#!/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);

I have a feeling the root of my issues comes from this line in my code:

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

I have to admit something, I found this code snippet online and I am completely unfamiliar with Perl in order to find a viable solution to be able to forward to multiple emails without issue. Any indication on where I could start, even if a full solution isn't clear, would be very helpful.

Resources:

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


Solution

  • #!/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);
    

    This ended up being my solution after many hours of trial and error.

    Just posting this here in case anyone runs into my very niche issue lol.

    Thanks.