Using MIME::Lite to send email and hash

A

a

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 =>'(e-mail address removed)',
From =>'(e-mail address removed)',
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>[email protected]</from>
<to>[email protected]</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{$_});
}
 
S

sln

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 =>'(e-mail address removed)',
From =>'(e-mail address removed)',
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>[email protected]</from>
<to>[email protected]</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 => '(e-mail address removed)',
to => '(e-mail address removed)',
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;
}
}
 
J

Jens Thoms Toerring

a said:
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 =>'(e-mail address removed)',
From =>'(e-mail address removed)',
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>[email protected]</from>
<to>[email protected]</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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,744
Latest member
CortneyMcK

Latest Threads

Top