MIME::Parser - Can't save binary attachment - perl

I am using Perl to read messages, look for and save attachments. Attachments will always be binary pdf documents and there will never be more than one attachment. I need to read the subject, check for and save an attachment (if exists) an copy message to a folder for temporary storage.
The reading, printing, copying functions all work. I've tried a lot of different scenarios with MIME::Parser (I have MIME::Tools installed) but either get a blank file or file with 1 or 2 characters. I'd also like to know how to determine / set the file extension rather than just blindly rename to .pdf.
#!/usr/bin/perl
use Net::IMAP::Simple::SSL;
use Email::Simple;
use MIME::Parser;
print "Content-type: text/html\n\n";
$server = new Net::IMAP::Simple::SSL('xxx');
$server->login('xxx','xxx');
my $folder='inbox';
my ($unseen, $recent, $total) = $server->status($folder);
my $newm = $server->select('INBOX');
my $tmp=($total-9); #limit for testing
my $outputdir = "./temp";
my $parser = new MIME::Parser;
$parser->output_dir($outputdir);
for (my $i = $tmp; $i <= $total; $i++) {
if ($server->seen($i)) {
print "Message #$i has been seen before...<br />";
} else {
my $es=Email::Simple->new(join '', #{$server->top($i)});
print $es->header('Subject')." on ";
print $es->header('Date')."<br />";
print "You've just seen message #$i<br />" if $server->see($i)."<br />";
$msg = $server->get($i);
$parser->parse_data($msg);
$server->copy($i,'dump');
}
}
$server->quit();
exit;
Error
parse_data: wrong argument ref type: Net::IMAP::Simple::_message at mailextract.pl line x

Don't know why you're using two different parsers...
my $entity = $parser->parse_data($message);
my $from = $entity->head->get('From');
my $subject = $entity->head->get('Subject');
my $timestamp = $entity->head->get('Date');
for my $part ($entity->parts()) {
if ( $part->mime_type eq 'application/pdf' ) { ### Few different types in use, see what your
### messages get sent with
my $filename = $part->bodyhandle->path;
...
### Do whatever
}
}
Edit: And your error is happening because you're not feeding through the correct thing to be parsed, a Net::IMAP::Simple::_message instead of:
parse_data DATA
Instance method. Parse a MIME message that's already in core. This internally creates an "in memory" filehandle on a Perl scalar value
using PerlIO
You may supply the DATA in any of a number of ways...
A scalar which holds the message. A reference to this scalar will be used internally.
A ref to a scalar which holds the message. This reference will be used internally.
DEPRECATED
A ref to an array of scalars. The array is internally concatenated into a temporary string, and a reference to the new
string is used internally.
It is much more efficient to pass in a scalar reference, so please consider refactoring your code to use that interface instead.
If you absolutely MUST pass an array, you may be better off using
IO::ScalarArray in the calling code to generate a filehandle, and
passing that filehandle to parse()
Try $parser->parse($server->getfh($i));

#!/usr/bin/perl
use Net::IMAP::Simple::SSL;
use MIME::Parser;
print "Content-type: text/html\n\n";
$server = new Net::IMAP::Simple::SSL('xxx');
$server->login('xxx','xxx');
my $newm=0;
$newm = $server->select('INBOX');
if ($newm==0) {
$server->quit();
print "No New Messages.";
exit;
}
my $outputdir = "./temp";
my $parser = new MIME::Parser;
$parser->output_dir($outputdir);
for (my $i = 1; $i <= $newm; $i++) {
my $entity = $parser->parse($server->getfh($i));
my $from = $entity->head->get('From');
my $subject = $entity->head->get('Subject');
my $timestamp = $entity->head->get('Date');
print "#$i $from / $subject / $timestamp<br />";
for my $part ($entity->parts()) {
print " / ".$part->mime_type;
if ( $part->mime_type eq 'application/octet-stream' || $part->mime_type eq 'application/pdf' ) {
my $filename = $part->bodyhandle->path;
print " / $filename";
}
print "<br />";
}
$server->copy($i,'dump');
$server->delete($i);
}
$server->quit();

Related

Text file to hash table

I want to create a hash table for the data in my file.
The file contains a bunch of commands that are written as
===|showcommand|
Every time I see this delimiter I want to create a hash key and store the data below it as an array in the value until it sees the next delimiter.
The next delimiter will do the same thing which is to create a hash key with the delimiter name and store the data on the next lines following it into an array as a value.
my %commands;
my $name;
my $body;
while (<>) {
if (my ($new_name) = /===\|([^|]*)\|/) {
$commands{$name} = $body if defined($name);
$name = $new_name;
$body = '';
} else {
$body .= $_;
}
}
$commands{$name} = $body if defined($name);
Assumes the body of the command starts on the line after the header, and stop on the line before the one with the next header.
You probably have it already working, but adding a small comment still, regarding the question on how to return the hash from a function.
Here's an example:
Input -file (used the following, which, I think contains similar structure as your input -file.
===|showcommand|
cmd1
cmd2
cmd3
cmd4
===|testcommand|
command1
command2
command3
===|anothercommand|
another1
another2
another3
another4
Perl -script:
use strict;
# Calling ReadCommandFile to build hash.
my %commands = ReadCommandFile("./commands.txt");
# ReadCommandFile - reads commands.txt and builds
# a hash.
sub ReadCommandFile()
{
my $file = shift;
my %hash = ();
my $name;
open(FILE, "<$file");
while(<FILE>)
{
if($_ =~ /===\|(.*)\|/)
{
$name = $1;
$hash{$name} = [];
}
else
{
my $line = $_;
$line =~ s/\n$//;
push(#{$hash{$name}}, $line);
}
}
close(FILE);
return %hash;
}
As a result, you should get the following hash (output from Data::Dumper):
$VAR1 = 'anothercommand';
$VAR2 = [
'another1',
'another2',
'another3',
'another4'
];
$VAR3 = 'showcommand';
$VAR4 = [
'cmd1',
'cmd2',
'cmd3',
'cmd4'
];
$VAR5 = 'testcommand';
$VAR6 = [
'command1',
'command2',
'command3'
];
You can then access individual elements like this:
Get the third command from "showcommand":
print "\nCommand #3: " . $commands{'showcommand'}[2];
Output: cmd3
The data from the file is copied to a hash and the commands are added as an array under the respective keywords.
Thanks!

How do I iterate over methods for Perl object

I've created an Object such as
my $hex = Hexagram->new();
and it has various methods:
top
bot
chinese
title
meaning
This object will be created numerous times and each time I need to gather and test information for each of the above methods.
I would like to do something like
foreach my $method ( qw/top bot chinese title meaning/ )
{
&gather_info($hex,$method);
}
and then have something like
sub gather_info {
my ($hex,$method) = #_;
print "What is the $method? ";
my $response = <STDIN>;
chomp $response;
$hex->${method}($reponse);
.... and other actions ....
}
But this doesn't work. Instead, for each method I seem to have to write out the basic code structure again and again which just seems plain wasteful.
I've also tried something where I try to pass a reference to the method call such as in
foreach my $ra ( [\$hex->top, "top"],
[\$hex->bot, "bot"],....)
{
my ($object_method, $name) = #{$ra};
&rgather_info($object_method, $name);
}
where
sub $gather_info {
my ($rhex, $name) = #_;
print "What is the $name?";
my $response = <STDIN>;
chomp $response;
&{$rhex}($response);
.... and other actions ....
}
But this time I get an error about
Not a CODE reference at <program name> line <line number>,....
Any suggestions on how I can do this?
According to perlobj method calls can be made using a string variable.
$object->$method( #args );
So your foreach loop should have worked fine. Or this one, which is much less wordy:
use strict;
use warnings;
my $hex = Hexagram->new();
gather_info( $hex, $_ )
for qw/top bot chinese title meaning/;
sub gather_info {
my ($hex, $method) = #_;
print "What is $method?\n";
my $response = <STDIN>;
chomp $response;
$hex->$method( $response );
}
Make sure you have strict and warnings enabled and try again. Update you post with errors, etc.

save key value in two different variables

I'm trying to figure out a way to split a string by delimiter : and save them as two string. I tried something in line 9 but it is not working. Apparently i want to find existence of #clients in #ping_host, if not exist then send an alert. Any suggestion?
#ping_host = ['1232','1212'];
#clients = ['1232:RARB','1212:CBN'];
client_monitor_state(#ping_host);
sub client_monitor_state(#ping_host){
my $token = $properties{token};
#clients = split(/,/, $token);
foreach $client (#clients){
($client_id,$client_name)=m/(\w+)\s*:(.+)/; # here the client_id should have the first part of match string
if(! grep($client_id,#ping_host)){
print "Client noted is $client_name \n";
# mail the client that is not reachable
my $subject_line = "The client $client_name is not reachable";
smtp_send(server_name => $client_name, subject_name => $subject_line);
}
}
}
You should use warnings; because it would probably have hinted at the solution. You are implicitly using $_ instead of $client, and you need to use =~ instead of =
use warnings;
use strict;
my $client = 'this:that';
my ($client_id, $client_name) = $client =~ m/(\w+)\s*:(.+)/;
print "$client_id,$client_name\n";
__END__
this,that

Perl LWP:Simple Get URL String Varilable

#!/usr/bin/perl
use LWP::Simple;
use warnings;
$content = 0;
$find = "webvis.edgesuite.net";
open (HOSTLIST,"lists.hosts");
#hosts = <HOSTLIST>;
foreach $host(#hosts) {
$results = `nslookup www.$host`;
my $pos = index($results, $find);
if ($pos > -1 )
{
my $url = "http://www.$host";
$content = get ($url);
print $content;
my $pos1 = index($content, $url);
if($pos1 > -1) {
print "Content Match\n";
} else {
print "No Content Match\n";
}
$count++;
chomp ($host);
print "$count www.$host\n";
}
}
close (HOSTLIST);
exit($errorcode);
Using the code above, I always get the following error:
IO::Socket::INET: Bad hostname 'www.test.com
If change the $url to:
$url = 'http://www.test.com';
I get the content retrieval from the page.
So my question is how do I pass in a string variable to the get attribute so it doesn't produce
the bad hostname error?
Thank you in advance
When you read in the hosts from <HOSTLIST>, each line (except possibly the last) will have a newline at the end of it which does not belong in a domain name and thus has to be explicitly removed with the chomp function before trying to do anything important.

How to use a perl module that you have written?

I've just written my first Perl module and am having trouble getting it to work with a script I produced also. Here is the error that the Perl interpreter displays when I attempt to run the script that is using my newly created module.
Error message:
scraper_tools_v1.pm did not return a true value at getYid.pl line 5.
BEGIN failed--compilation aborted at getYid.pl line 5.
scraper_tools_v1.pm is the Perl module which I have written and getYid.pl is the Perl script which attempts to utilize the scraper_tools_v1.pm module.
Here is the code for the scraper_tools_v1.pm file:
#!/usr/bin/perl
package scraper_tools_v1;
use strict;
use warnings;
use WWW::Curl::Easy;
# Note this function expects a single parameter which should be in the form of a URL
sub getWebPage($)
{
# Setting up the Curl parameters
my $curl = WWW::Curl::Easy->new; # create a variable to store the curl object
# A parameter set to 1 tells the library to include the header in the body output.
# This is only relevant for protocols that actually have headers preceding the data (like HTTP).
$curl->setopt(CURLOPT_HEADER, 1);
# Setting the target URL to retrieve with the passed parameter
$curl->setopt(CURLOPT_URL, #_);
# Declaring a variable to store the response from the Curl request
my $response_body = '';
# Creating a file handle for CURL to output to, then redirecting our output to the $response_body variable
open(my $fileb, ">",\$response_body) or die $!;
$curl->setopt(CURLOPT_WRITEDATA, $fileb);
# getting the return code from the header to see if the GET was successful
my $return_code = $curl->perform;
# capturing the response code from the GET request in the HTTP header, i.e... 200, 404, 500, etc...
# 200 is success
my $response_code = $curl->getinfo(CURLINFO_HTTP_CODE);
# if the return code is zero than the request was a success
if ($return_code == 0)
{
# A little debug output to keep you informed
print ("Success ". $response_code.": ".#_."\n");
# return whatever was contained on the web page that we just got using a GET
return $response_body;
}
else
{
print ("Failure ". $response_code.": ".#_."\n");
}
close($fileb); # close the file-handle
}
And here is the getYid.pl script which attempts to use the above module
#!/usr/bin/perl
use strict;
use warnings;
use scraper_tools_v1;
my %cat_links; # Hash that stores categories and their numbers (ID's)
my $web_page = scraper_tools_v1->getWebPage("http://something.com/categoryindex.aspx");
my #lines = split(/\n/, $web_page);
foreach my $line (#lines)
{
chomp($line);
if ($line =~ /<option value=\"{1}(.+)\">(.+)<\/option>/)
{
my $num = $1;
my $desc = $2;
$desc =~ s/\s+&\s+/ & /;
$cat_links{$desc} = $num;
}
}
my #allTargetUrls; # make a new array to store all the links we need to extract listings from
$web_page = ''; # Reset this variable so we can reuse it.
my $totalNumberOfListings = 0;
foreach my $key (keys %cat_links)
{
my $target = "http://something.com/categorydetail.aspx?id=$cat_links{$key}&exact_phrase=0";
$web_page = scraper_tools_v1->getWebPage($target);
#lines = split(/\n/, $web_page);
foreach my $line (#lines)
{
my $pages;
chomp($line);
if ($line =~ /We found (\d) listings for your search\./)
{
my $listingsInCat = $1;
print ("$cat_links{$key}, $listingsInCat");
$totalNumberOfListings += $listingsInCat;
}
if ($line =~ /Page 1 of (\d)/)
{
$pages = $1;
}
for (my $i = 1; $i <= $pages; $i++)
{
#build the target urls
my $pageUrl = "http://something.com/categorydetail.aspx?id=$key&search=&exact_phrase=True&city=&state=&zipcode=&page=$i";
push(#allTargetUrls, $pageUrl);
}
}
print("Total number of listings = ".$totalNumberOfListings);
}
Any help in resolving this issue would greatly be appreciated and please note that I have tested both files independently for interpreter errors and found nothing. Thanks to all for taking a look.
When you write a Perl module, you should always end the file with the line
1;
Perl executes code at the module level when the module is imported. If you don't return a true value (1 is true), then you'll get the error you describe. Essentially, Perl is informing you that the initialisation code in your module didn't succeed.