I've come across a bit of a problem and was wondering if anyone could point me in the right direction, I'm writing a email parser that parses emails and extracts particular info.
It parses plain text emails(the emails are in a set format) using regex, this works fine.
If an email has an attachment which is of type 3 && encoding = 3 && disposition = attachment then save the attachment to local disk.
My problem is step 2:
I foreach through each email structure if it has more than 2 parts I attempt to decode the attachment and save it to disk, but this is failing.
If I try to simply use file_put_contents I get a 0 KB File, I have given full permissions to the wamp server user, So I do not think it's file permissions and I have verified I can write by using mkdir.
file_put_contents(APPPATH . "attachments/". $partofpart->dparameters[0]->value, $partofpart); //$partofpart is part[2] of the full email structure
So I attempted to decode the part and save the decoded part instead using file_put_contents
$attachment = base64_decode($partofpart);
var_dump($attachment);
//then save the attachment with attachment name
file_put_contents(APPPATH . "attachments/". $partofpart->dparameters[0]->value, $attachment);
but this returns an error saying base64_decode requires an string not an object, how do I then convert my email-part-attachment to a string for use by base64_decode?
Even just writing that out I feel I'm missing something obvious and shouldn't require the extra steps.
Am I right in thinking the part IS the attachment? All the parameters lead me to believe so, Type, Subtype, size etc are all correct for the attachment.
Link to the entire scraper model
Thanks for reading and any help.
Below is a vardump of parts that meet the type/encoding/disposition checks
I have worked out the problem, I was not fetching the actual pdf back just its structure
So I wrote a function to retrieve the body of the particular email then used imap_base64 to encode then finally file_put_contents.
function getAttachment($msg_index, $part)
{
$mailbody = imap_fetchbody($this->conn,$msg_index,$part);
return $mailbody;
}
$attachment = imap_base64($this->email_model->getAttachment($email['index'], "2"));
//mkdir(APPPATH . "attachmentzs/");
//then save the attachment with attachment name
file_put_contents(APPPATH . "attachments/". $partofpart->dparameters[0]->value, $attachment);
Related
I got the docs from the third party that is sending me a file over http protocol and I need to write a script that will successfully receive the sent file. Content-type is set as application/gzip so I can't pick up the file uploaded using a $_FILES variable as it would be easy with multipart/form-data.
This link gave me a hint: http://php.net/manual/en/features.file-upload.post-method.php
Note:
Be sure your file upload form has attribute enctype="multipart/form-data" otherwise the file upload will not work.
I tried to reproduce their "client" side to test my server using the example in the following url http://blog.derakkilgo.com/2009/06/07/send-a-file-via-post-with-curl-and-php/
And to ensure crosdomain posting is available, I used a function posted and explained by #slashingweapon CORS with php headers
There must be a way to do it - Halp!
Hi what I understand is you just need to download the file that has been uploaded to a server, try getting the file in binary mode using the below code:
ftp_fget($conn_id, $file, FTP_BINARY, 0);
Ref: http://php.net/manual/en/function.ftp-get.php
The connection details must be shared with you by the 3rd party who is sharing the file.
Thanks to #fusion3k who pointed me to the right direction.
$rawData = file_get_contents("php://input") gave the the RAW data from which I had to parse the file from. It was painful to extract it because it was a binary file. I used
Explode the data with "\n" as a delimiter $rawData = explode("\n", $rawData)
Skip first few lines (it was 3 lines of header data for me) and take the rest into $valuableData
Convert the last line of data from string to binary using $convertedLine = unpack("H*", $valuableData[count($valuableData)-1])
Cut out the last byte of data from the last row $convertedLine[1] = substr($convertedLine[1], 0, -2) ( have no idea why is `unpack returning an array)
$valuableData[count($valuableData)-1] = pack("H*", $convertedLine[1])
implode("\n", $valuableData) and write that to a file
I'm trying to send a .png image to my user via phpmailer. The image is shown when I use <img> tags, but I want it to display as a real attachment that the user can open/save/print (like in this screenshot). I read that I can use $mail->addStringAttachment for this. So I tried this, and it does send an attachment with the email, but when I try to open it, it says that Windows Picture Viewer can't open the file. Also saving to my computer and then opening with Paint doesn't work, it tells me thats not a valid file or something. I think this is because it's no static image, but an image generated by an API, namely:
$qr = 'http://api.qrserver.com/v1/create-qr-code/?data=' . $guid . '&size=250x250';
So this image should be sent as an attachment. Does anyone know how I can make this work?
I got it to work fine as an attachment by doing the following:
$qr = file_get_contents("https://api.qrserver.com/v1/create-qr-code/?size=150x150&data=Example");
$mail->addStringAttachment($qr, "qr.png");
The reason it's failing is that you're trying to attach the URL as image data. You need to fetch the data from the URL first, then attach it to something.
Go one step at a time - make sure that you're getting back valid image before trying to email it - e.g.
file_put_contents('qr.png', file_get_contents($qr));
and make sure you get a valid image saved in there. When you know that's working, then try and email it with
$mail->addStringAttachment(file_get_contents($qr) 'qr.png');
Though perhaps with a bit more error checking!
I'm using Drupal CMS on top of PHP and IIS. When I send emails containing embed images, the images are not displayed in Outlook.
I'm trying to isolate the problem.
There is nothing in Drupal or Outlook which will allow me to view the complete message body with headers.
Is there a way to configure PHP to write the email to a folder on disk instead of sending the email?
you can write it to a text file instead of sending, just need to find the place where it happens:
$folder=dirname(__FILE__)."/emaildir";
$txtfilename=time().'.txt';
$emailstr=$header . "/n" . $message . "/n";
instead of mailto() or whatever function just write to file
$fh=fopen($txtfilename,"w");
$fwrite($fh,$emailstr);
fclose($fh);
This is written from my head, you might want to check for mistakes but you get the picture
I have a script that currently takes some data from a wordpress DB and then loops over the returned rows and uses fputcsv(). The file handle was setup with:
$fh = fopen('php://output', 'w');
The script is linked to a link of a webpage and when you click the link it downloads the CSV (using the content-disposition header).
Is it possible to write to php://output this CSV file and then use PHP's mail() function to send it in an attachment?
I have a mail function I've written that will set the MIME type to multipart/mixed, I'm just not sure how to create the actual attachment to be emailed.
Use tmpfile() instead. Write your CSV data to that temp file, then attach it to your email.
Plus, don't write your own mime handling/generating functions. Use a library like PHPMailer or Swiftmailer to do it for you. Far easier and far more reliable. Part of their attachment handling code allows you specify the filename the user sees, so even though it might be "/tmp/abc123def", it'll show up as "data.csv" (or whatever you specify) in the actual email.
I am having some issues with trying to get SwiftMailer to attach a file I have created with FPDF. Basically I have a page called createPDF.php that is dynamically generated based on the ID number in the URL. This page is set to output the PDF inline using $pdf->Output("filename.pdf",I);. What I want to do is to be able to attach this file to an email using SwiftMailer from another page simply by calling my createPDF.php?id=xxx link.
From the PHP page where I want to send the email from, everything works, except the attachment. It attaches something, but not what I want and it is not viewable in a PDF viewer on my local machine. The line specific to the attaching the file is:
->attach(Swift_Attachment::fromPath('createPDF.php?id=xxxx'))
This does not work, but surely, it must be possible without saving the file on my web server by FPDF.
Is this possible? If so, how?
Thanks!
The problem here is Swiftmailer gets the file contents, it does not execute your php file. So the contents of your PDF will the code that is in createPDF.php.
why cant you safe the file first? You should be able to safe it and delete it when your email is sent.
<?php
$id = "xxx";
$fileName = "tmp/".sha1(time()+mt_rand(0,99999999));
include "createPDF.php"; //saves it to $fileName
->attach(Swift_Attachment::fromFile( $fileName )->setFilename('blaha.pdf'));
unlink($fileName);
Ok, so I just figured this out.
Basically I made a new PHP file with the bulk of my createPDF.php file as a function and simply passed in two variables into the function as my $id and an $output variable. $output is simply the way that FPDF outputs the file — inline, etc... I then set the function to return the output of the FPDF. In my createPDF.php file I simply call my function passing in $id and 'I' as the variables so it displays the correct PDF inline in the browser.
In my sendEmail function I simply pass in $id and 'S' and set it to a variable $content, which I pass into SwiftMailer as an attachment.
Works great.
Thanks for your help!