Solved

Use Perl Mail::Sender to programtically add options

Posted on 2009-07-13
5
642 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
  • 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 500 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 Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
perl script 4 94
How to search multiple patterms in a file with perl? 4 77
Move Function in Perl Script 2 58
create a gui in perl 3 46
I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

760 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now