0

I want download a report that I'm receiving every day on my inbox. This email contains a link to a platform (where I have to be logged-in on the platform to open the link correctly, otherwise I will be redirected to login page) and finally there is another link that downloads the report to my computer.

My question is: It's possible to make a PHP script that can do this automatically?. All the process to read the email and identify the link it's easy, I want some guidelines to continue working.

Thanks!

David
  • 503
  • 3
  • 7
  • 18

4 Answers4

1

This may be harder than you anticipate and PHP may not be the best language to achieve what you are after as it can not run on top of Outlook or any other desktop based email client. To do this you would have to run the PHP code from the command line, and have it set to regularly intercept all your emails and check to see if the email is from a specific user, and if it is parse the email for the two necessary links. Once you have the links you can have the code establish a cURL connection to the login form and pass the username and password, ensuring to pass any cookies defined from the authenticated session, and then establish a new cURL link to the download link to download the file to the local file system, after which you can send a new email to yourself attaching the file as a native attachment, dependant on file size.

Information on sending cookies with cURL can be found at How can I send cookies using PHP curl in addition to CURLOPT_COOKIEFILE?

The net result though is that this would have to run in the background continuously and would have to be set to regularly connect to your email server and check all emails for the existence of emails from the automated email sender.

An easier solution would be to find out if the automated tool can be setup to simply send the file as an email attachment so that you don't have to run the excess code. In addition any time the initial email structure changes or the download or login links change you would have to update the code to deal with the associated changes.

Main point is that PHP isn't the most ideal solution for what you are trying to do and what you are trying to do, in any language, is going to be a complex task to achieve.

Community
  • 1
  • 1
Chris Rutherfurd
  • 1,617
  • 1
  • 15
  • 32
  • I'm not the owner of the platform, and It's not possible to send the file as an attachment. About when to run the script, I can schedule a cron task to execute the script at certain time. – David Feb 16 '16 at 08:54
  • Does the download link remain the same each time or does it change for each email? – Chris Rutherfurd Feb 16 '16 at 08:59
  • The link that is in the email is like: https://example.com/Reports?accountId=765049 and doesn't change. On the other hand, when I follow the first link, I can find the second link that looks like: https://example.com/Reports/ReportDownload?reportJobId=0123456789 which every day is different. – David Feb 16 '16 at 09:06
0

When you already have the email link you could login to the platform using a curl request and afterwards make the same call again. the second time you'd be logged in and curl would download your report.

devnull
  • 558
  • 4
  • 18
0

You could use the cURL library. How does your platform identifies you ? If you can use cookies, look for CURLOPT_COOKIE

curl_setopt($ch, CURLOPT_COOKIE, session_name() . '=' . session_id());

tbobm
  • 113
  • 1
  • 9
0

i have the a similar issue, i want to open on googlesheets a series of reports from my email, managed to do it when its attached evenif is a zip, but when its only a link o cannot do it, heres the script i use, maybe someone could upgraded to open links and import the csv

function importCSV() {

// Change the report name Report
var threads = GmailApp.search("subject:XXXXXX  has:attachment from:XXXXX@google.com newer_than:1d");
var message = threads[0].getMessages()[threads[0].getMessages().length-1];
var allAttachment = message.getAttachments();
var attachment = allAttachment[0];
var contentType = attachment.getContentType();
var name = attachment.getName();

if (attachment.getContentType() === "text/csv") {
    // Change the sheet name, where you want the data to appear
    var sheetActive = SpreadsheetApp.openById("1slVepI8s2Ekdiha0u4NpqFLZ4NnJrhyc8in9hYNzJA0");
    var sheet = sheetActive.getSheetByName("Youtube");
    var csvData = Utilities.parseCsv(attachment.getDataAsString(), ",");

    // This clears the document from previous data
    sheet.clearContents().clearFormats();

    // Import the CSV file to the spreadsheet
    sheet.getRange(1, 1, csvData.length, csvData[0].length).setValues(csvData);
}
else if (attachment.getContentType() === "application/zip") {

    attachment.setContentTypeFromExtension();

    // Unzip Attachment Change the sheet name, where you want the data to appear
    var sheetActive = SpreadsheetApp.openById("1slVepI8s2Ekdiha0u4NpqFLZ4NnJrhyc8in9hYNzJA0");
    var sheet = sheetActive.getSheetByName("Youtube");

    try {
        
        var unzip = Utilities.unzip(attachment)[0];
        var csv = unzip.getDataAsString();
        var csvData2 = Utilities.parseCsv(csv, ",");

        // This clears the document from previous data
        sheet.clearContents().clearFormats();

        // Import the CSV file to the spreadsheet
        sheet.getRange(1, 1, csvData2.length, csvData2[0].length).setValues(csvData2);
        
    } catch (e) {

        // Logs an ERROR message.
        console.error('myFunction() yielded an error: ' + e);
    }
}}
user9869932
  • 6,571
  • 3
  • 55
  • 49
  • Hello and welcome to SO. Please take your time to write in good English, read-proof the answer and format it properly next time. – Daemon Painter Nov 12 '20 at 14:50