problem with hashes of attachments and Mail::Sender::Easy

D

Dan

Ahoy all,

The following is a simplified extract of some code i'm knocking
together...

$attachmenthash->{'smily.gif'} = {'smily.gif' => { 'ctype' =>
'image/gif', 'file' => '/foo/bar/smiley.gif' }};
$attachmenthash->{'printtest.xls'} = {'printtest.xls' => { 'ctype' =>
'application/octet-stream', 'file' => '/foo/bar/printtest.xls' }};

email({
'from' => $from,
'to' => $to,
'cc' => $cc,
'bcc' => $bcc,
'subject' => $subject,
'priority' => 3, # 1-5 high to low
'smtp' => 'smtp.yadayadayada.com',
'port' => 25,
'auth' => 'LOGIN',
'authid' => 'yadayada',
'authpwd' => 'yada',
'_text' => $email,
'_attachments' => %attachmenthash,
}) or die "email() failed: $@";


.... the code should create an email with the two attachments. In the
real world, the number of attachments would depend on variables passed
by the user. The email sends fine, but no attachments are included.

Please help, i've not done much with hashes of hashes before, and this
is driving me nuts.

Any advice appreciated.

Dan
 
T

Tad McClellan

Dan said:
$attachmenthash->{'smily.gif'} = {'smily.gif' => { 'ctype' =>
'image/gif', 'file' => '/foo/bar/smiley.gif' }};
$attachmenthash->{'printtest.xls'} = {'printtest.xls' => { 'ctype' =>
'application/octet-stream', 'file' => '/foo/bar/printtest.xls' }};
'_attachments' => %attachmenthash,


You should enable "use strict" when developing Perl code.

It would have found the bug that you have inserted there.

Please help, i've not done much with hashes of hashes before, and this
is driving me nuts.


You have _two_ problems.

$attachmenthash and %attachmenthash are totally different things,
despite their similar names.

$attachmenthash already contains the hash-ref that you need, so:

_attachments => $attachmenthash,


(the => operator will quote your keys for you, so you don't need quotes.)

Secondly, you are not building the hash structure that the module's
docs describe. Yours is one level too deep (ie. it contains *two*
keys equal to the filename).


# one at a time
$attachmenthash->{smily.gif} = { ctype => 'image/gif',
file => '/foo/bar/smiley.gif' };
$attachmenthash->{printtest.xls} = { ctype => 'application/octet-stream',
file => '/foo/bar/printtest.xls'};

# all at once
$attachmenthash = {
smily.gif => { ctype => 'image/gif',
file => '/foo/bar/smiley.gif'
},
printtest.xls => { ctype => 'application/octet-stream',
file => '/foo/bar/printtest.xls'
},
};
 
J

J. Gleixner

Dan said:
Ahoy all,

The following is a simplified extract of some code i'm knocking
together...

$attachmenthash->{'smily.gif'} = {'smily.gif' => { 'ctype' =>
'image/gif', 'file' => '/foo/bar/smiley.gif' }};
$attachmenthash->{'printtest.xls'} = {'printtest.xls' => { 'ctype' =>
'application/octet-stream', 'file' => '/foo/bar/printtest.xls' }};

email({
'from' => $from,
'to' => $to,
'cc' => $cc,
'bcc' => $bcc,
'subject' => $subject,
'priority' => 3, # 1-5 high to low
'smtp' => 'smtp.yadayadayada.com',
'port' => 25,
'auth' => 'LOGIN',
'authid' => 'yadayada',
'authpwd' => 'yada',
'_text' => $email,
'_attachments' => %attachmenthash,
}) or die "email() failed: $@";


... the code should create an email with the two attachments. In the
real world, the number of attachments would depend on variables passed
by the user. The email sends fine, but no attachments are included.

Please help, i've not done much with hashes of hashes before, and this
is driving me nuts.

In the real world you should add:

use strict;
use warnings;

to your script. That should point you to your error.
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top