?
Solved

Use Perl Mail::Sender to programtically add options

Posted on 2009-07-13
5
Medium Priority
?
661 Views
Last Modified: 2012-05-07
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

0
Comment
Question by:shersker
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 39

Expert Comment

by:Adam314
ID: 24842961

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

Open in new window

0
 
LVL 39

Expert Comment

by:Adam314
ID: 24842964
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
 

Author Comment

by:shersker
ID: 24843579
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
 
LVL 39

Accepted Solution

by:
Adam314 earned 2000 total points
ID: 24843643
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
 

Author Closing Comment

by:shersker
ID: 31602945
Excellent! That will work perfectly!! Thank you again!!
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

649 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question