I have an email backup file which is purely text. How can I retrieve the document (PDF, images, word files) attached to it as a normal file?
Select the long string of text which appears in your email. That is probably one of the attachments, it usually starts like this:
--bcaec554d754b0f76a04d9fda578--
--bcaec554d754b0f77204d9fda57a
Content-Type: application/pdf; name="test.pdf"
Content-Disposition: attachment; filename="Otest.pdf"
Content-Transfer-Encoding: base64
X-Attachment-Id: 9ba6310dffca527f_0.1
Copy this long string and paste it in the Base64 decoder found here.
Download the output and rename it by adding the appropriate extension to it. For example testfile.pdf or filename.docx.
There you go. You just recreated your lost attachment using Base64 decoding.
This is how to do it in PHP decode to a file
function base64_to_jpeg( $inputfile, $outputfile ) {
/* read data (binary) */
$ifp = fopen( $inputfile, "rb" );
$imageData = fread( $ifp, filesize( $inputfile ) );
fclose( $ifp );
/* encode & write data (binary) */
$ifp = fopen( $outputfile, "wb" );
fwrite( $ifp, base64_decode( $imageData ) );
fclose( $ifp );
/* return output filename */
return( $outputfile );
}
in HTML if you want to just display in web / using HTML
<img src="data:image/png;base64,Your_Base_64_Code_Here">
You can use For use as CSS background:
url('data:image/png;base64,Your_Base_64_Code_Here')
Related
I have a situation where I need to encrypt content in Coldfusion and then decrypt in Perl. Here's a sample Coldfusion code:
<cfscript>
input = "Amidst the roar of liberated Rome, Of nations freed, and the world overjoy'd";
encryptionKey = "8kbD1Cf8TIMvm8SRxNNfaQ==";
encryptedInput = encrypt( input, encryptionKey, "AES/ECB/PKCS5Padding", "hex" );
writeOutput( "Encrypted Input: #encryptedInput# <br />" );
</cfscript>
This produces:
27B0F3EB1286FFB462BDD3F14F5A41724DF1ED888F1BEFA7174CA981C7898ED2EF841A15CDE4332D030818B9923A2DBA0C68C8352E128A0744DF5F9FA955D3C72469FEFDAE2120DE5D74319ED666DDD0
And the Perl:
use 5.24.1;
use Crypt::ECB qw(encrypt_hex);
my $input = "Amidst the roar of liberated Rome, Of nations freed, and the world overjoy'd";
my $encryption_key = "8kbD1Cf8TIMvm8SRxNNfaQ==";
my $encrypted_input = encrypt_hex($encryption_key, 'Rijndael', $input);
say $encrypted_input;
This produces:
e220ff2efe5d41e92237622ba969f35158d20e2c9c44995d44136d928d517462980321d4d6193fe62dc942fd717128442972524207777366954e5ceb2d1812ac997e06767a27d6a0145176d717c3836b
Why is the encrypted content different? Does anyone have any insights into this?
Your encryption key is base64 encoded, but Crypt::ECB expects a raw byte string (this isn't clear from the docs, though).
use Convert::Base64;
...
my $encryption_key = decode_base64("8kbD1Cf8TIMvm8SRxNNfaQ==");
...
New output:
27b0f3eb1286ffb462bdd3f14f5a41724df1ed888f1befa7174ca981c7898ed2ef841a15cde4332d030818b9923a2dba0c68c8352e128a0744df5f9fa955d3c72469fefdae2120de5d74319ed666ddd0
I'm using Mail::IMAPClient to connect to our Outlook mail server. I can get the mail just fine and print the text version of that mail to a file. But I'm having trouble using MIME::Parser to parse through the email.
I've tried giving the parser a file handle to the text file that I wrote the email to. I've tried giving the parser just the text of the email but it won't work how I'm expecting it to work. The entity parts always equals 0.
When I dump the entity skeleton I get
Content-type: text/plain
Effective-type: text/plain
Body-file: NONE
--
I can see all of the parts of the email in the file. The two PDFs that are attached are there, encoded in base64, so I know that the script is actually retrieving the email and the attachments. I've also tried parse and parse_data.
my $msgCount = 0;
$msgCount = $imap->message_count();
#or abortMission("", "Could not get message count: ". $imap->LastError );
if ( $msgCount > 0 ) {
#get all the messages from the inbox folder
my #msgseqnos = $imap->messages
or abortMission("", "Could not retreive messages:". $imap->LastError);
my ($x, $bh, $attachment, $attachmentName);
foreach my $seqno ( #msgseqnos ) {
my $input_file;
my $parser = new MIME::Parser;
my $emailText = $imap->body_string($seqno) # should be the entire email as text.
or abortMission("", "Could not get message string: " . $imap->LastError);
$parser->ignore_errors(1);
$parser->output_to_core(1);
open my $emailFileHandle, ">", "invoiceText.txt";
print $emailFileHandle $emailText;
#$imap->message_to_file($emailFileHandle, $seqno);
my $entity = $parser->parse_data($emailText);
$entity->dump_skeleton;
if ( $entity->parts > 0 ) {
for ( my $i = 0; $i < $entity->parts; $i++ ) {
my $subentity = $entity->parts($i);
# grab attachment name and contents
foreach $x ( #attypes ) {
if ( $subentity->mime_type =~ m/$x/i ) {
$bh = $subentity->bodyhandle;
$attachment = $bh->as_string;
$attachmentName = $subentity->head->mime_attr('content-disposition.filename');
open FH, ">$attachmentName";
print FH $attachment;
close FH;
#push #attachment, $attachment;
#push #attname, $subentity->head->mime_attr('content-disposition.filename');
}
}
}
}
else {
stillAGo("eData VehicleInvoices problem", "Perl can't find an attachment in an email in the VehicleInvoices folder of eData email address");
}
close $emailFileHandle;
# say $emailText;
# next;
#open OUT_FILE, ">invoiceText.txt";
#print OUT_FILE $emailText;
#print OUT_FILE $imap->bodypart_string($seqno,1);
#close OUT_FILE;
#print $emailText;
}
}
I'm trying to retrieve the attachments from emails automatically and save them to disk to be processed by another job.
I'd like to include the invoiceText.txt file so people can see the actual output but it's 1200 lines long. I'm not sure where to upload a file to link in here.
The body_string method doesn't return the entire email. As the documentation describes, and the name implies, it returns the body of the message, excluding the headers. That is why dump_skeleton shows no headers apart from the defaults
What you probably want, although I haven't tried it, is message_string, which does return the entire email
I see you've used message_to_file but commented it out. That would probably have worked if you got MIME::Parse to read from the file
Lots of questions here from people trying to implement gzip encoding in Zend - I need to do the opposite!
I have a controller which extends the standard Zend_Controller_Action. My downloadAction has a PDF file as it's response body. That works well, except that the downloaded file isn't correctly recognised by the client browsers.
The downloaded file is identified as a 'Zip Archive' by the browser download. When saved and double-clicked it opens correctly as a PDF. The response header shows Content-Encoding:gzip, so I figure that's likely the culprit.
The core of my action is:
$this->_helper->layout()->disableLayout();
$this->_helper->viewRenderer->setNoRender(true);
if ($fd = fopen($pdfpath.$pdf->Filename,'r'))
{
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="summary.PDF"');
while(!feof($fd))
{
$buffer = fread($fd, 2048);
echo $buffer;
}
fclose($fd);
}
There is some other code before this piece, but it does nothing more exciting than populate the variables.
How would I go about disabling the Content-Encoding:gzip header for just this response, or if that's the wrong end of the stick (it would be good to use compression, but not at the expense of user experience), how do I get the client to correctly identify the downloaded file once the compression has been reversed?
I would recommend to use framework's Zend_Controller_Response_Http instead of header() function, usually I specify "default" headers with gzip compression etc. in my Bootstrap for all responses, and override them in actions for some special reasons:
public function indexAction()
{
$frontContoller = $this->getFrontController();
$this->_helper->layout()->disableLayout();
$this->_helper->viewRenderer->setNoRender(true);
$response = new Zend_Controller_Response_Http();
$response
->setHeader('Content-Type', 'application/pdf')
->setHeader('Content-Disposition', 'attachment; filename=summary.pdf')
->setHeader('Expires', ''.gmdate('D, d M Y H:i:s', strtotime('31.08.1986')) . ' GMT', true)
->setHeader('Cache-Control', 'no-cache')
->setHeader('Pragma', 'no-cache', true);
$response->setBody(file_get_contents('/full/path/to/summary.pdf'));
$frontContoller->setResponse($response);
}
am generating pdf using Zend_pdf
its saving after creating
I want to open it instead of saving.
When i access the url directly
Use render() method
// Set PDF headers
header ('Content-Type:', 'application/pdf');
header ('Content-Disposition:', 'inline;');
// Output pdf
echo $pdf->render();
I couldn't find a way to open a PDF directly, so I did this instead:
<?php
// Save PDF into file
$oPdf->save("./pdfcache/filename.pdf");
// Set headers
header('Content-Type: application/pdf');
header('Content-Disposition: inline; filename=filename.pdf');
header('Cache-Control: private, max-age=0, must-revalidate');
header('Pragma: public');
ini_set('zlib.output_compression','0');
// Get File Contents and echo to output
echo file_get_contents("./pdfcache/filename.pdf");
// Prevent anything else from being outputted
die();
It's not perfect, but it does the job for me.
I'm trying to get facebook's stream.publish to make a wall post that includes some custom text and a dynamically generated image from the site. The Image is only available as Base64 as it is drawn by the user before the publish action is initiated. Facebook doesn't seem to like the src being passed in as a Base64 string. Does anyone know a workaround or will I be forced to save the image to server first and then provide a link (I'd really rather not do this).
You cannot pass base64 image to Facebook in JavaScript. You'll need to send it to your server and convert it to a png/jpeg or something and upload it to Facebook from there (this will need user permissions of some sort). Either that, or store it on the server and then use the URL that will serve a png/jpg in JavaScript.
You can do as follow in PHP :
function base64_to_jpeg( $base64_string, $output_file ) {
$ifp = fopen( $output_file, "wb" );
fwrite( $ifp, base64_decode( $base64_string) );
fclose( $ifp );
return( $output_file );
}
$facebook->setFileUploadSupport(true);
$image = base64_to_jpeg( $your_base64_string, 'tmp.jpg' );
$args = array('message' => 'Some message');
$args['image'] = '#' . realpath( $image );
$data = $facebook->api('/your_user_id/photos', 'post', $args);
unlink($image);