-1

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

DanDaMan
  • 73
  • 2
  • 9
  • What's the error? – brian d foy Aug 06 '20 at 15:16
  • A message that you sent contained one or more recipient addresses that were incorrectly constructed: gabe.s@print.epsonconnect.com\n : malformed address: may not follow gabe.s@print.epsonconnect.com This address has been ignored. There were no other addresses in your message, and so no attempt at delivery was possible. – DanDaMan Aug 11 '20 at 15:21
  • Update your question with new information. And what happens when you follow the instructions in the error message? – brian d foy Aug 11 '20 at 17:06

1 Answers1

1
#!/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.

DanDaMan
  • 73
  • 2
  • 9