global has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

I'm using MIME::Lite to send out an email. I have an ini file that I am reading some values in from. My main problem is that I want to express the From value of the email client as:

"Test User" <test@test.com> using my $msg=MIME::Lite->new(<br> 'From' =>'"Test User" <test@test.com>', etc

works but I have an ini file which I am accessing through

Local::Ini my $ini=new Local::Ini(q|test.ini|); my %defaults=$ini->defaults(); and then need to have my $msg=MIME::Lite->new(<br> 'From' =>'$defaults{Mail}{From}||"$ENV{USER}"',

but of course the single quotes stop the expansion. I have had a look at the FAQ on expanding variables in strings and expanding function calls in a string but I wasn't really sure what to do.

Replies are listed 'Best First'.
Re: Variables and Strings
by dragonchild (Archbishop) on Jul 23, 2003 at 14:26 UTC
    'From' =>'$defaults{Mail}{From}||"$ENV{USER}"', ### becomes From => $defaults{Mail}{From} || $ENV{USER},
    If that doesn't work, reply to this post and I'll give you some different ideas.

    ------
    We are the carpenters and bricklayers of the Information Age.

    Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

    Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.

      The problem with just having

      $defaults{Mail}{From} || $ENV{USER}

      is that when it is converted to:

      "Test User" <test@test.com>

      unless it is enclosed with ' ' MIME::Lite tries and sends and email to Test User which doesn't work. :-)

      The "Test User" <test@test.com> really needed to come through to MIME::Lite as '"Test User" <test@test.com>'

      Thanks all for the suggestions.

Re: Variables and Strings
by global (Novice) on Jul 23, 2003 at 14:25 UTC
    Sorry all, I have fixed it. Getting too tired to notice :-(

    ended up going with:

    my $emailname=$defaults{Mail}{FromName}||""; my $emailaddr=$defaults{Mail}{From}||"$ENV{USER}"; my $emailcombined='"' . $emailname . '" <' . $emailaddr .'>'; my $msg=MIME::Lite->new( 'From' =>$emailcombined, etc

    I would still be interested in other ways to do this.

    Thanks.

Re: Variables and Strings
by edoc (Chaplain) on Jul 23, 2003 at 14:25 UTC

    maybe I've misunderstood, but does this do what you want?

    my $msg=MIME::Lite->new( 'From' =>"$defaults{Mail}{From}||\"$ENV{USER}\"",

    cheers,

    J

Re: Variables and Strings
by zby (Vicar) on Jul 23, 2003 at 14:30 UTC
    Just try without them. You don't need those "||" literary in your From line? You want to use it as a boolean operator? Am I right?

    By the way please fix the code tags.