Invalid Content-Type 'multipart/; boundary - perl

I am using Perl and Email::MIME to access an email account via IMAP. When I try to download the attachment, I get this error:
Invalid Content-Type 'multipart/;
boundary="===============6113972194662902815=="' at
/path/to/class/Reader.pm line 99.
Here is my code:
my $env = $self->env();
my $imap = $self->imap();
my $logger = $self->logger();
my ($OUT, $out_file);
# parse attachment
my $message_string = $imap->message_string($imap_id);
Email::MIME->new($message_string)->walk_parts(sub {
my ($part) = #_;
if ($part->content_type =~ /octet\-stream/) {
($OUT, $out_file) = tempfile();
binmode $OUT;
print $OUT $part->body;
close $OUT;
$logger->info("downloaded attached report: [$out_file]");
}
});
Here is the end of the dump of the $part:
MixOLE4sTiwwLE4NCiI9IiIyMDE4LTA0LTE5IDA5OjU5OjI5LjQ5NCIiIixhZTJhNDEzMy1hYWRj
LTQ4ZjgtYWY0My1jYjdhMGEzYzQzMzIsIkFwcGxpYW5jZSBwYXJ0cywgaG91c2Vob2xkIixBcHBs
aWFuY2UgU2FsZXMsTUlETE9USElBTixWQSxWYWN1dW0gU3lzdGVtcyw4MDQ1NDg0MTgxLCAsYTEx
OGUxODIyMTAwNzJkYSxEVVJBVElPTiw0NSwyLjQ3ODQsTixOLE4sMCxODQoiPSIiMjAxOC0wNC0x
OSAwOTo1OToyOS45MSIiIiwxZTNlZGQzZi02NGM0LTQ3M2UtODk2Yy00MTI3ZTVhYzIwYWUsRklO
QU5DRXxGSU5BTkNJQUwgSU5TVElUVVRJT05TfEJBTktTLEJhbmtzLEdSRUVOU0JVUkcsUEEsRmly
c3QgRmVkZXJhbCBTYXZpbmdzICYgTG9hbiBBc3NvY2lhdGlvbiBPZiAgR3JlZW4sODQ0MjU1MTk0
MSwgLDgzNDA0NmQ2MWEyYzRkY2UsRFVSQVRJT04sNDUsNS4wLE4sTixOLDAsTg0KIj0iIjIwMTgt
MDQtMTkgMDk6NTk6MjkuOTE1IiIiLGY1YTBmOTc1LTg1MmUtNDAwNC05YTY1LWEzYzgyNDJlYTQy
NywiaW5zdXJhbmNlLCBsZW5kZXJzLCBtb3J0Z2FnZXMiLExvYW5zICYgTW9ydGdhZ2VzLE1hbmFz
cXVhbixOSixDb21tZXJjaWFsIE1vcnRnYWdlIEFzc29jaWF0ZXMgSW5jLiw3MzI0NTEzMzU0LCAs
ZGQ5YTQ4OGVmYjJiY2NlMSxEVVJBVElPTiw0NSwzLjQ0MzUsTixOLE4sMCxODQo=
--===============6113972194662902815==--
",
ct => {
attributes => {
charset => "us-ascii"
},
composite => "plain",
discrete => "text",
subtype => "plain",
type => "text"
},
encode_check => 1,
header => Email::MIME::Header,
mycrlf => "
",
parts => []
}
}
I have tried several different methods of downloading this file and I keep getting that error. Let me know if there are any questoins

Related

In mojolicious, how to protect images from public view

Somebody can help me, please. I have app in mojolicious Lite
I wanna block images from all without session login
when i type http://m.y.i.p:3000/images/imageX.jpg
i wanna show images only with session login.
my html code is
shwo image 1
Same way as any other content. Set up a handler for requests, render (or don't render) the content.
get '/images/*img' => sub {
my $c = shift;
if (!$c->session("is_authenticated")) {
return $c->render( text => "Forbidden", status => 403 );
}
my $file = $c->param("img");
if (!open(my $fh, '<', $IMAGE_DIR/$file)) {
return $c->render( text => "Not found", status => 404 );
}
my $data = do { local $/; <$fh> };
close $fh;
$c->render( data => $data, format => 'jpg' );
};
Your request handler will take priority over the default handler that serves content from public folders, but once you have this handler in place, you don't need to store the files it serves in a public folder.
another sololution is
get '/pays/*img' => sub {
my $self = shift;
my $img = $self->param('img');
plugin 'RenderFile';
my #imgext = split(/\./, $img);
my $ext = $imgext[-1];
$self->render_file(
'filepath' => "/directiry/$img",
'format' => "$ext", # will change Content-Type "application/x-download" to "application/pdf"
'content_disposition' => 'inline', # will change Content-Disposition from "attachment" to "inline"
# delete file after completed
);
It is use plugin 'RenderFile';

error when sending email using Dancer2::Plugin::Email;

I am sending email using Dancer2 via the Dancer2::Plugin::Email package. The main code that I have for this is:
sub sendEmail {
my ($params,$email_address,$template) = #_;
my $text = '';
my $tt = Template->new({
INCLUDE_PATH => config->{views},
INTERPOLATE => 1,
OUTPUT => \$text
}) || die "$Template::ERROR\n";
my $out = $tt->process($template,$params);
my $email = email {
from => XXXXX,
to => $email_address,
subject => XXXXX,
body => $text,
'Content-Type' => 'text/html'
};
}
where I have hidden a couple of the fields. I have gotten the following error:
Route exception: open body: Invalid argument at
/usr/local/share/perl/5.22.1/MIME/Entity.pm line 1878. in
/usr/local/share/perl/5.22.1/Dancer2/Core/App.pm l. 1454
It is not occurring all of the time and I haven't been able to find a consistent piece of code that always fails.
I have set the host parameter of the mail server that I am using in the configuration as explained here: https://metacpan.org/pod/Dancer2::Plugin::Email Simple tests show it works, but I get sporadic errors that I can't track down.

Perl read a file and send e-mail

I have a log file of below format
D12345 joe#gmail.com
C67890 mary#gmail.com
B45678 don#gmail.com
A12309 joe#gmail.com
F45670 mary#gmail.com
F45470 joe#gmail.com
Currently I'm able to send e-mail to everyone with below e-mail body content
your Product id: $product_id
($product_id is D12345, A12309, F45470 , C67890, F45670)
Problem is 'N' e-mails are sent to an user if their e-mail id is present N times in the log file.
Example :
2 e-mails are sent to mary#gmail.com and 3 e-mails are sent to joe#gmail.com
I want to send only one e-mail to the users but having all their product ids, if single e-mail id occurs multiple times in the output file, like
your Product ids: C67890, F45670 to mary#gmail.com
and
your Product ids: D12345, A12309, F45470 to joe#gmail
My current code snippet
open my $fh, '<', "output.txt" or die "could not open file: $!";
while (my $line = <$fh>) {
my ($product_id, $to_email) = $line =~ /(\w\d+)\t(\S+)/;
my $MailFrom = 'myemail#domain.com';
my $subject = "PRODUCT ID DETAILS";
my $message = "your Product ids: $product_id";
%mail = (
To => $to_email,
From => $MailFrom,
Subject => $subject,
Message => $message
);
$mail{Smtp} = 'mail.myorg.com';
sendmail(%mail)or die $Mail::Sendmail::error;
}
close $fh;
Suggestions are really appreciated. Thanks in advance.
You must aggregate the information from the log file into a hash before sending the mails
Here's some example code which dumps the %mail hash instead of calling sendmail so that you can see the results
use strict;
use warnings 'all';
use Data::Dump;
my %users;
{
open my $fh, '<', "output.txt" or die "could not open file: $!";
while ( <$fh> ) {
my ($prod, $user) = split;
push #{ $users{$user} }, $prod;
}
}
# Convert arrays of product codes into comma-separated strings
#
$_ = join ', ', #$_ for values %users;
while ( my ($user, $prods) = each %users ) {
my %mail = (
To => $user,
From => 'myemail#domain.com',
Subject => 'PRODUCT ID DETAILS',
Message => "Your product IDs: $prods",
Smtp => 'mail.myorg.com',
);
dd \%mail
#sendmail(%mail) or die $Mail::Sendmail::error;
}
output
{
From => "myemail\#domain.com",
Message => "Your product IDs: D12345, A12309, F45470",
Smtp => "mail.myorg.com",
Subject => "PRODUCT ID DETAILS",
To => "joe\#gmail.com",
}
{
From => "myemail\#domain.com",
Message => "Your product IDs: B45678",
Smtp => "mail.myorg.com",
Subject => "PRODUCT ID DETAILS",
To => "don\#gmail.com",
}
{
From => "myemail\#domain.com",
Message => "Your product IDs: C67890, F45670",
Smtp => "mail.myorg.com",
Subject => "PRODUCT ID DETAILS",
To => "mary\#gmail.com",
}
This is not a perl question, just how to do basic coding.
I would preprocess the data to fit your needs i.e. create a hash of arrays using the email id as the key and push the product into the array.
You will end up with something like:
{
joe#gmail.com: ['D12345', 'A12309', 'F45470'],
mary#gmail.com: ['C67890', 'F45670'],
don#gmail.com: ['B45678']
}
Once you've created that data structure form your log file it should be simple enough the iterate over each key and create an email with the values.

Connecting keeps closing?

so i'm having a problem trying to automatically login to a internal website. I'm able to send a post request but in the response I always get the Header Connection: close. I've tried to pass is through the post request but it still seems to respond with Connection: close. I want to be able to navigate through the website so I need the Connection: keep-alive so that i can send more request. Could anyone tell me what I'm doing wrong? here's the code:
#usr/bin/perl
#NetTelnet.pl
use strict; use warnings;
#Sign into cfxint Unix something...
use Net::Telnet;
# Create a new instance of Net::Telnet,
my $telnetCon = new Net::Telnet (Timeout => 10,
Prompt => '/bash\$ $/') or die "Could not make connection.";
my $hostname = 'cfxint';
# Connect to the host of the users choice
$telnetCon->open(Host => $hostname,
Port => 23) or die "Could not connect to $hostname.";
use WWW::Mechanize;
my $mech = WWW::Mechanize->new(cookie_jar => {});
&login_alfresco;
sub login_cxfint {
#get username and password from user
my $CXusername = '';
my $CXpassword = '';
# Recreate the login
# Wait for the login: message and then enter the username
$telnetCon->waitfor(match => '/login:/i');
# this method adds a \n to the end of the username, it mimics hitting the enter key after entering your username
$telnetCon->print($CXusername);
# does the same as the previous command but for the password
$telnetCon->print($CXpassword);
#Wait for the login successful message
$telnetCon->waitfor();
}
sub login_alfresco{
my $ALusername = '';
my $ALpassword = '';
$mech->get('http://documents.ifds.group:8080/alfresco/faces/jsp/login.jsp');
my $res = $mech->res;
my $idfaces = '';
if($res->is_success){
my $ff = $res->content;
if($ff =~ /id="javax.faces.ViewState" value="(.*?)"/){
$idfaces = $1;
}
else {
print "javax.faces /Regex error?\n";
die;
}
}
print $idfaces, "\n";
#Send the get request for Alfresco
$mech->post('http://documents.ifds.group:8080/alfresco/faces/jsp/login.jsp',[
'loginForm:rediretURL' =>,
'loginForm:user-name' => $ALusername,
'loginForm:user-password' => $ALpassword,
'loginForm:submit' => 'Login',
'loginForm_SUBMIT' => '1',
'loginForm:_idcl' => ,
'loginForm:_link_hidden_' => ,
'javax.faces.ViewState' => $idfaces], **'Connection' =>'keep-alive'**);
$res = $mech->res;
open ALF, ">Alfresco.html";
print ALF $mech->response->as_string;
if($res->is_success){
my $ff = $res->content;
if($ff =~ /id="javax.faces.ViewState" value="(.*?)"/){
$idfaces = $1;
}
else {
print "javax.faces /Regex error?\n";
die;
}
}
print $idfaces, "\n";
#Logout
$mech->post('http://documents.ifds.group:8080/alfresco/faces/jsp/extension/browse/browse.jsp', [
'browse:serach:_option' => '0',
'browse:search' => ,
'browse:spaces-pages' => '20',
'browse:content-pages' => '50',
'browse_SUBMIT' => '1',
'id' => ,
'browse:modelist' => '',
'ref'=>'',
'browse:spacesList:sort' => ,
'browse:_idJsp7' => ,
'browse:sidebar-body:navigator' => ,
'browse:contentRichList:sort' => ,
'browse:act' => 'browse:logout',
'outcome' => 'logout',
'browse:panel' => ,
'javax.faces.ViewState' => $idfaces,])
}
You can enable keep-alive by using a connection cache:
use LWP::ConnCache;
...
$mech->conn_cache(LWP::ConnCache->new);
All that header means is that the connection will be closed upon completion of the request, instead of being kept open for possible further requests. This is perfectly normal and should not interfere with sending the request.
EDIT: If you're sending a Connection:Keep-Alive and the server is still responding with Connection:Close, then the server configuration needs to be changed. The default for HTTP/1.1 is persistent connections, so the server must explicitly be configured to send Connection:Close. See Section 8 of RFC2616.

how to query eXist using XPath?

I decided to use eXist as a database for an application that I am writing in Perl and
I am experimenting with it. The problem is that I have stored a .xml document with the following structure
<foo-bar00>
<perfdata datum="GigabitEthernet3_0_18">
<cli cmd="whatsup" detail="GigabitEthernet3/0/18" find="" given="">
<input_rate>3</input_rate>
<output_rate>3</output_rate>
</cli>
</perfdata>
<timeline>2011-5-23T11:15:33</timeline>
</foo-bar00>
and it is located in the "/db/LAB/foo-bar00/2011/5/23/11_15_33.xml" collection.
I can successfully query it, like
my $xquery = 'doc("/db/LAB/foo-bar00/2011/5/23/11_15_33.xml")' ;
or $xquery can be equal to
= doc("/db/LAB/foo-bar00/2011/5/23/11_15_33.xml")/foo-bar00/perfdata/cli/data(output_rate)
or
= doc("/db/LAB/foo-bar00/2011/5/23/11_15_33.xml")/foo-bar00/data(timeline)
my ($rc1, $set) = $eXist->executeQuery($xquery) ;
my ($rc2, $count) = $eXist->numberOfResults($set) ;
my ($rc3, #data) = $eXist->retrieveResults($set) ;
$eXist->releaseResultSet($set) ;
print Dumper(#data) ;
And the result is :
$VAR1 = {
'hitCount' => 1,
'foo-bar00' => {
'perfdata' => {
'cli' => {
'given' => '',
'detail' => 'GigabitEthernet3/0/18',
'input_rate' => '3',
'cmd' => 'whatsup',
'output_rate' => '3',
'find' => ''
},
'datum' => 'GigabitEthernet3_0_18'
},
'timeline' => '2011-5-23T11:15:33'
}
};
---> Given that I know the xml document that I want to retrieve info from.
---> Given that I want to retrieve the timeline information.
When I am writing :
my $db_xml_doc = "/db/LAB/foo-bar00/2011/5/23/11_15_33.xml" ;
my ($db_rc, $db_datum) = $eXist->queryXPath("/foo-bar00/timeline", $db_xml_doc, "") ;
print Dumper($db_datum) ;
The result is :
$VAR1 = {
'hash' => 1717362942,
'id' => 3,
'results' => [
{
'node_id' => '1.2',
'document' => '/db/LAB/foo-bar00/2011/5/23/11_15_33.xml'
}
]
};
The question is : How can I retrieve the "timeline" info ? Seems that the "node_id" variable (=1.2) can points to the "timeline" info, but how can I use it ?
Thank you.
use XML::LibXML qw( );
my $parser = XML::LibXML->new();
my $doc = $parser->parse_file('a.xml');
my $root = $doc->documentElement();
my ($timeline) = $root->findnodes('timeline');
if ($timeline) {
print("Exists: ", $timeline->textContent(), "\n");
}
or
my ($timeline) = $root->findnodes('timeline/text()');
if ($timeline) {
print("Exists: ", $timeline->getValue(), "\n");
}
I could have used /foo-bar00/timeline instead of timeline, but I didn't see the need.
Don't know if you're still interested, but you could either retrieve the doc as DOM and apply an xquery to the DOM, or, probably better, only pull out the info you want in the query that you submit to the server.
Something like this:
for $p in doc("/db/LAB/foo-bar00/2011/5/23/11_15_33.xml")//output_rate
return
<vlaue>$p</value>