Use Perl Mail::Sender to programtically add options

Hello,
I'm a Perl noobie, so this isn't strictly related to mail:sender, but here's my scenario:

I'm writing a perl script the runs periodically (via cron) to scan a directory for *.txt files. Those txt files are generated by an external system and specify an outbound email msg. A sample txt file would be:

From=User@domain.com
To=Recv@anotherDomain.com
Subject=This is my subject
Body=Here's my message.

Typically, a txt file will also have a matching PDF or CSV file (so, mailme.txt will also have mailme.pdf). Also, the txt file might specify a BCC or that there's no attachment at all - just the email itself.

I'm trying to use mail::sender (and am completely open to other modules) to send the message. I need to vary the options depending on the txt file. For example, by default it should ask for a read receipt, unless the From contains the text "noreply@". Also, add a BCC if it's specified in the txt file. And, if "Attachment=N" is in the txt file, don't bother looking/waiting for a matching attachment, just send the email.

My thought was to "build up" the OpenMultipart as a scalar with the options needed, but I'm not having any luck. Clearly, it's because of my novice status in Perl. (I have used a similar method when setting options for Log::Log4Perl).

I'm trying to do this as simple as possible and it's seems hideously redundant to do If statements and separate OpenMultiParts for each possible config. (No Read receipt + BCC, No Read receipt + no BCC, Read Receipt + BCC, Read Receipt + no BCC....adding the attachment should be straightforward either way by just using $sender->Attach if needed).

I'd like to create $BuildConfig with the From, To and Subject. Then, if From doesn't contain "noreply, add the Confirm option. Then, if BCC is specified, add that as well. Then, do the OpenMultiPart, then add the attachment if needed, then send. Or, can you modify the OpenMultipart after the initial basics (From, To, Subject), similar to the way you can add the file attachment with Attach?

Line 9 in the code below just spits out "Odd number of elements in anonymous hash. No From address specified"

Can anybody suggest a way to go on this? Am I completely off-base on this?
Thx!
Steve
$sender = new Mail::Sender{smtp => $config->emailhost, on_errors => 'die'};
 
my $BuildConfig = '
				from =>		$from,
				to => 		$to,
				confirm =>  "reading",
				subject =>	"test txt conf"
				';
				
$sender->OpenMultipart({$BuildConfig});
				
$sender->Body({ msg => $combo_msg});
				
$sender->Attach({
	file => 'out\test.atch'});
			
$sender->Close;

Open in new window

sherskerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Adam314Commented:

my @BuildConfig =
  (from    => $from,
   to      => $to,
   confirm => "reading",
   subject => "test txt conf");
                                
$sender->OpenMultipart({$BuildConfig});

Open in new window

0
Adam314Commented:
Sorry, typo in there... Use this:
my @BuildConfig =
  (from    => $from,
   to      => $to,
   confirm => "reading",
   subject => "test txt conf");
                                
$sender->OpenMultipart({@BuildConfig});

Open in new window

0
sherskerAuthor Commented:
That worked like a champ. Now, would it be possible to use PUSH to add items on to the array? Like below? (I tried and am back to the "odd number of elements" error).

Thx!
Steve
my @BuildConfig =
  (from    => $from,
   to      => $to,
   subject => "test txt conf");
 
If $confirm {push (@BuildConfig, "confirm => 'reading'")}
                                
$sender->OpenMultipart({@BuildConfig});

Open in new window

0
Adam314Commented:
Yes, you can use push.  You need to remove the double-quotes.  The parenthesis after push are optional, and not typically used.  You also have a syntax error in your If code (you need parenthesis, and "If" must be lowercase "if").
my @BuildConfig =
  (from    => $from,
   to      => $to,
   subject => "test txt conf");
 
if($confirm) {push @BuildConfig, confirm => 'reading';}
                                
$sender->OpenMultipart({@BuildConfig});

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
sherskerAuthor Commented:
Excellent! That will work perfectly!! Thank you again!!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Perl

From novice to tech pro — start learning today.