Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Single variable Multiple Values

by PilotinControl (Scribe)
on Jun 29, 2013 at 17:50 UTC ( #1041491=perlquestion: print w/ replies, xml ) Need Help??
PilotinControl has asked for the wisdom of the Perl Monks concerning the following question:

Good Afternoon my fellow monks here is my question: I have a variable that is passed from one subroutine to another. In the second subroutine this variable could represent one or more values. What I am trying to accomplish is sending an email based on an Abbreviation for example: $emailverify in the previous sub could equal Bob, Tim, George etc. In a database Bob = some@email.com, Tim = test@cork.com etc. I am having trouble making $emailverify = Bob in turn making Bob = $to and sending the file to Bob or Tim. Thanks in advance!

sub email { my $emailverify = shift; print ("(1) MICROSOFT ACCESS"); my $input = <STDIN>; $input = <STDIN> until defined $input; chop ($input); if ( $input == 1 ) { # SENDS MS ACCESS FILE my $from = 'email@system.com; my $your_file = 'file.mdb'; my $my_file = 'file.mdb'; my $mime_type = 'multipart/mixed'; my $mail_server = 'mail.test.com'; my $to = $emailverify; my $msgone = "Dear $emailverify Your Files are Attached"; # CREATE MESSAGE my $msg = MIME::Lite->new( From => $from, To => $to, Type => $mime_type, Subject => 'Files Attached', ); # CREATE ATTACHMENT $msg->attach( Type => 'TEXT', Data => "$msgone" ); $msg->attach ( Type => 'application/msaccess', Path => $my_file, Filename => $your_file, Disposition => 'attachment' ) or die "Error adding $your_file: $!\n"; MIME::Lite->send('smtp', $mail_server, Timeout=>60); $msg->send or die "Couldn't Send $!";

Comment on Single variable Multiple Values
Download Code
Re: Single variable Multiple Values
by Happy-the-monk (Monsignor) on Jun 29, 2013 at 18:10 UTC

    I am having trouble making $emailverify = Bob in turn making Bob = $to and sending the file to Bob or Tim.

    Where to start?

    Perl has got all kinds of variables. You are using scalars in your code most of the time. That's the type of variable that generally fits a single value in it. With a little help from perlintro you will find arrays for variable lists and hashes for variable lists representing relationships of keys values. And of course references and objects that may virtually be or do anything or everything.

    However, you use a scalar, a single thing variable for an email address, and I think all you need now is that.
    You can concatenate a string in a scalar like this:

    $email = $email:1 . "," . $email_2;

    A number of email programmes treat such a string the way you want them, as a list of email addresses separated by the comma (",").

    Your mileage may vary ;-)

    Cheers, Sören

    (hooked on the Perl Programming language)

Re: Single variable Multiple Values
by Perlbotics (Abbot) on Jun 29, 2013 at 18:11 UTC
    If I understand your problem correctly, you could apply this quick patch:
    ... my $mime_type = 'multipart/mixed'; my $mail_server = 'mail.test.com'; #--old: my $to = $emailverify; my $to = alias_to_emailaddress( $emailverify ); ...
    And then add something to resolve the name/alias to an email-address:
    sub alias_to_emailaddress { my ($alias) = @_; #-- e.g. "Bob"; #-- query your DB or start with a hash-lookup for testing... my %alias2email = ( 'BOB' => 'some@email.com', 'TIM' => 'test@cork.com', ); #-- maybe normalise your alias first? my $email = $alias2email{ uc $alias }; #---- now add some error handling... #-- perlhaps you can use a default? # $email //= 'bugreport2self@myhost.com'; #-- throw an exception (catch with 'eval' later) # die "No email-address found for '$alias'!" unless $email; return $email; }
    (untested)

      That worked! Thanks!

Re: Single variable Multiple Values
by poj (Curate) on Jun 29, 2013 at 19:17 UTC
    "..this variable could represent one or more values"

    Are you sending the same email to more than one person ?.

    For example; you want $to to be 'myname@email1.com,yourname@email2.com,anyname@email4.com' but the variable you are being passed is like 'me,you,anyname@email4.com'

    poj
Re: Single variable Multiple Values
by Anonymous Monk on Jun 30, 2013 at 05:16 UTC
    Wishful thinking here :)  $input = <STDIN> until defined $input;

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1041491]
Approved by Happy-the-monk
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (15)
As of 2014-07-29 19:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (226 votes), past polls