Can’t call method “bodyhandle” on an undefined value

I’ve recently been seeing and increase in spam emails for my fourthwoods.com accounts. I started playing with a perl script to process the spam messages to feed to SpamAssassin. When I finish it I’ll post an article on here describing what I did and why. Hopefully it will be helpful to somebody.

Anyway, this was the first I used the MIME::Parser and related perl classes so I muddled my way through following some examples and reading the perl documentation. I did run across an error that wasn’t immediately obvious what was causing it. According to the examples, I was doing everything correctly. However, I ran into the following:

Can't call method "bodyhandle" on an undefined value at foo.pl line XX.

Ok, so I’m parsing a multipart message following along with this example and every time I try to access the bodyhandle I get the error. WTF?? My code isn’t that much different from the example. If I iterate through all the parts the data is there, so why no bodyhandle?

Digging through the documentation for bodyhandle “An entity will have either a body or parts: not both.” Aha! Ok, so when will there be parts and when will there be a bodyhandle?

Well, it depends on the mime-type and whether or not the parser is set up to extract nested messages. In my case I’m concerned with mime-type “message/rfc822” which according to the table, if the parser is configured to extract nested messages the nested messages are parsed and put into an array of MIME::Entity objects returned by the parts method. If the parser is not to extract nested messages, they are placed into a MIME::Body object returned by bodyhandle.

For the MIME::Parser, according to the documentation for the extract_nested_messages option it defaults to true which causes my issue. To prevent extraction of nested messages and instead get a valid bodyhandle, extract_nested_messages must be set to false or 0.


In the example that I was following they do not ever set this option to false however, they also do not seem to be particularly interested in my specific case. I did see that sal-wrapper.pl does in fact set this option so I know I’m not nuts!

Related Posts

Leave a Reply