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
Solved

Use Perl Mail::Sender to programtically add options

Posted on 2009-07-13
5
645 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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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…

809 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