Using MIME::Lite to send email and hash

Discussion in 'Perl Misc' started by a, Sep 4, 2009.

  1. a

    a Guest

    Hi,

    I am trying to use MIME::Lite to automate some testing cases.
    This module requires the following to initialize the email.
    my $msg = MIME::Lite->new(
    To =>'',
    From =>'',
    Subject =>'Too Too Too Simple Email',
    Data =>"TESTING"
    );
    I put all these information in an xml file as the following, and load the
    file in a hash.
    my $ref = XMLin("sample_email.xml");
    my %h = %{$ref->{email}->[1]};
    <config>
    <email>
    </email>
    <email>
    <from></from>
    <to></to>
    <subject>This is a simple email</subject>
    <data>This is a very simple email</data>
    </email>
    </config>

    Is there any way to put the hash in the MIME::Lite->new( <argument contained
    in hash> ) to initialize it?

    I have tried the following but not success
    my $ref = XMLin("sample_email.xml");
    my %h = %{$ref->{email}->[1]};
    my $msg = MIME::Lite->new();
    foreach (keys %h)
    {
    $msg->set($_=>$h{$_});
    or
    $msg->add($_=>$h{$_});
    or
    $msg->attr($_=>$h{$_});
    }
     
    a, Sep 4, 2009
    #1
    1. Advertising

  2. a

    Guest

    On Fri, 4 Sep 2009 02:54:31 -0700, "a" <> wrote:

    >Hi,
    >
    >I am trying to use MIME::Lite to automate some testing cases.
    >This module requires the following to initialize the email.
    >my $msg = MIME::Lite->new(
    > To =>'',
    > From =>'',
    > Subject =>'Too Too Too Simple Email',
    > Data =>"TESTING"
    > );
    >I put all these information in an xml file as the following, and load the
    >file in a hash.
    >my $ref = XMLin("sample_email.xml");
    >my %h = %{$ref->{email}->[1]};
    > <config>
    > <email>
    > </email>
    > <email>
    > <from></from>
    > <to></to>
    > <subject>This is a simple email</subject>
    > <data>This is a very simple email</data>
    > </email>
    > </config>
    >
    >Is there any way to put the hash in the MIME::Lite->new( <argument contained
    >in hash> ) to initialize it?
    >
    >I have tried the following but not success
    >my $ref = XMLin("sample_email.xml");
    >my %h = %{$ref->{email}->[1]};
    > my $msg = MIME::Lite->new();
    > foreach (keys %h)
    > {
    > $msg->set($_=>$h{$_});
    > or
    > $msg->add($_=>$h{$_});
    > or
    > $msg->attr($_=>$h{$_});
    > }
    >
    >
    >


    Just a guess:

    my $ref = XMLin("sample_email.xml");
    my $msg = MIME::Lite->new( %{$ref->{email}->[1]} );

    Because, the below works.
    -sln

    =====================
    use strict;
    use warnings;

    my %h = (
    from => '',
    to => '',
    subject => 'This is a simple email',
    data => 'This is a very simple email'
    );

    MIME_Lite_new (%h);

    exit;

    sub MIME_Lite_new {
    while (my ($parm,$value) = splice @_,0,2) {
    printf "%-8s => '%s'\n", $parm,$value;
    }
    }
     
    , Sep 4, 2009
    #2
    1. Advertising

  3. a <> wrote:
    > Hi,


    > I am trying to use MIME::Lite to automate some testing cases.
    > This module requires the following to initialize the email.
    > my $msg = MIME::Lite->new(
    > To =>'',
    > From =>'',
    > Subject =>'Too Too Too Simple Email',
    > Data =>"TESTING"
    > );
    > I put all these information in an xml file as the following, and load the
    > file in a hash.
    > my $ref = XMLin("sample_email.xml");
    > my %h = %{$ref->{email}->[1]};
    > <config>
    > <email>
    > </email>
    > <email>
    > <from></from>
    > <to></to>
    > <subject>This is a simple email</subject>
    > <data>This is a very simple email</data>
    > </email>
    > </config>


    > Is there any way to put the hash in the MIME::Lite->new( <argument contained
    > in hash> ) to initialize it?


    > I have tried the following but not success
    > my $ref = XMLin("sample_email.xml");
    > my %h = %{$ref->{email}->[1]};
    > my $msg = MIME::Lite->new();
    > foreach (keys %h)
    > {
    > $msg->set($_=>$h{$_});
    > or
    > $msg->add($_=>$h{$_});
    > or
    > $msg->attr($_=>$h{$_});
    > }


    Why not simply use

    my $msg = MIME::Lite->new( %h );

    or, if you really need the MIME::Lite object before you got the
    hash with the required data

    my $msg = MIME::Lite->new( );
    $msg->build( %h );

    The add() method is for appending to the end of the email header,
    attr() is for MIME attributes, and I didn't find a set() method
    in the documentation. If you want to set the data you have to
    use the data() method as far as I can see. So you can't blindly
    use one of those methods for all the stuff you have to pass to
    the MIME::Lite object.
    Regards, Jens
    --
    \ Jens Thoms Toerring ___
    \__________________________ http://toerring.de
     
    Jens Thoms Toerring, Sep 4, 2009
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Aaron Brady

    SQL, lite lite lite

    Aaron Brady, Dec 29, 2008, in forum: Python
    Replies:
    15
    Views:
    783
    Aaron Brady
    Jan 1, 2009
  2. James
    Replies:
    1
    Views:
    265
    Paul Lalli
    Aug 4, 2004
  3. Replies:
    8
    Views:
    437
  4. ecureuil
    Replies:
    0
    Views:
    357
    ecureuil
    May 28, 2006
  5. Replies:
    2
    Views:
    434
    Tintin
    Jan 5, 2007
Loading...

Share This Page