Solved

Save email attachments sent from Mac

Posted on 2014-02-03
19
246 Views
Last Modified: 2014-10-07
Background history:
Some years ago a client was receiving large numbers of photos via email from contractors on a daily basis and I was asked to automate the system from them.
I created a secure area on the website where contractors could upload their photos simply by dragging a folder from their PC into a box on a web page.
This page used a java applet (an amended version of this http://www.radinks.com/upload/)
This method worked fine for anyone using windows but not a Mac (but this was only a few at the time). Everyone at the time was using a digital camera.

Since then more and more of the contractors are now using smart phones or tablets to take photos and there are substantially more Mac users.

I decided it was time to update the system of uploading photos and thought the best way was probably to go back to emailing in the photos to an email address that was piped to a php script which could process the photos. The idea being that regardless of where the photos were (android phone, iPhone, tablet, PC, Mac etc), they could be attached to an email and sent in.

So the first step is to save the attached photos.
After hunting around I found this class
http://stuporglue.org/mailreader-php-parse-e-mail-and-save-attachments-php-version-3/
Code here
https://github.com/stuporglue/mailreader

On testing it works perfectly apart from if the email is sent from a Mac. The only way I can get it to work on a Mac is if only one photo is attached and it's attached by dragging from the desktop.

I did write to the developer but he replied that he didn't have time to help at the moment. I notice that his web page has recently been changed and that he's now charging for help. He does say that the class has support for inline content type (from mail app on mac)

So my question is, does anyone know what might need to be changed to the class to make it work with emails  (with multiple attachments) sent from a Mac?

Or failing that, does anyone have another solution to the problem?
0
Comment
Question by:fionafenton
  • 7
  • 4
  • 3
  • +2
19 Comments
 
LVL 38

Expert Comment

by:Tom Beck
ID: 39829374
I wouldn't be able to solve the email attachment problem, but have you looked into services like Dropbox or even Google Drive. It's drag and drop just like your original system. Works on any platform. The problems with using email are that the files cannot be centrally managed, there are too many variables in email, size restrictions, mime types, embedded vs attached, etc., and the emails tend to live on a server somewhere forever.
0
 
LVL 1

Author Comment

by:fionafenton
ID: 39829422
Dropbox or similar isn't an option.
Once I've extracted the images I need to 'do things' with them.

I can amend the mailreader class to only accept jpegs below a certain size. The only other information I need to capture is the email address and subject line.
Anything with an unrecognised email address, no subject line, no attachments, large attachments, wrong format attachments will be rejected with a reply email.

The mailreader class does everything I want it to except recognise multiple attachments sent from a Mac
0
 
LVL 38

Assisted Solution

by:Tom Beck
Tom Beck earned 125 total points
ID: 39829444
Once I've extracted the images I need to 'do things' with them.
Not understanding how that eliminates Dropbox or similar.

I hope you get the answer to your question. Lots of PHP experts on EE.
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39829451
I have never tried it with image attachments, but I believe that an email pipe would work.  The pipe gets the entire messages, headers and everything included.  Here is my teaching example of a pipe script.

IIRC the use of output buffering was important on my servers.  If the pipe produced anything to stdout like an echo or message, the server responded to the client with a "bounced mail" message.

#!/usr/bin/php -q
<?php
/**
 * /email_pipe/index.php
 * DATED: 2009-05-12
 *
 * THIS IS AN EMAIL PIPE SCRIPT.
 * THIS SCRIPT IS STARTED AUTOMATICALLY FOR EACH MESSAGE.
 * NOTE THAT THIS SCRIPT IS ABOVE THE /public_html/ DIRECTORY TO PREVENT ACCIDENTAL EXECUTION
 *
 * --> HOW DO WE KNOW WHICH EMAIL MESSAGES GET SENT HERE?
 * THIS SCRIPT RECEIVES MESSAGES SENT TO email_pipe@your.org
 * CREATE AN EMAIL MAILBOX EXCLUSIVELY FOR AUTOMATED PROCESSING.
 * SET UP AN EMAIL FORWARD FOR THAT MAILBOX IN cPANEL->EMAIL LIKE THIS:
 * 1...5...10...15...20...25...
 * |/home/{account}/email_pipe/index.php
 *
 * --> WHEN YOU UPLOAD, THIS SCRIPT WILL BE MARKED RW-R-R BUT THAT IS WRONG
 * THIS SCRIPT MUST BE MARKED EXECUTABLE x0755
 * YOU CAN USE FTP SOFTWARE TO CHMOD TO RWX-RX-RX
 *
 * --> NOTE THE FIRST LINE OF THIS SCRIPT MUST SAY #!/usr/bin/php -q STARTING IN COLUMN ONE
 * 1...5...10...15...20...25...
 * #!/usr/bin/php -q
 * <?php ... PROGRAM CODE FOLLOWS
 */
error_reporting(E_ALL);

// USE THE OUTPUT BUFFER - THIS DOES NOT HAVE BROWSER OUTPUT
ob_start();

// COLLECT THE INFORMATION HERE
$raw_email = NULL;

// TRY TO READ THE EMAIL FROM STDIN
if (!$stdin = fopen("php://stdin", "R"))
{
    echo 'ERROR: UNABLE TO OPEN php://stdin' . PHP_EOL;
}

// ABLE TO READ THE MAIL
else
{
    while (!feof($stdin))
    {
        $raw_email .= fread($stdin, 4096);
    }
    fclose($stdin);
}


// REMOVE MULTIPLE BLANKS - AND OTHER PROCESSING AS MIGHT BE NEEDED
$raw_email = preg_replace('/ +/', ' ', $raw_email);

// SPEW WHAT WE GOT, IF ANYTHING, INTO THE OUTPUT BUFFER
var_dump($raw_email);

// CAPTURE THE OUTPUT BUFFER AND SEND IT TO SOMEONE ELSE VIA EMAIL
$buf = ob_get_contents();
mail ('you@your.org', 'EMAIL PIPE DATA', $buf);

// PREVENT ANY BROWSER OUTPUT - MAY CAUSE ERROR RESPONSES AND BOUNCED MESSAGES
ob_end_clean();

Open in new window

0
 
LVL 1

Author Comment

by:fionafenton
ID: 39830020
I have no problem with piping the email and reading it, or extracting what I want from it, except when the email is sent from a Mac with multiple attachments.

The mailreader class (link in my original email) basically uses your code Ray to read the email and then uses Pear's mimedecode to extract the various parts (http://pear.php.net/manual/en/package.mail.mail-mimedecode.decode.php)
0
 
LVL 82

Assisted Solution

by:Dave Baldwin
Dave Baldwin earned 125 total points
ID: 39830120
Are you picking up both "inline" and "attachment" from "$return->disposition" ?  When my Mac friends send multiple images in an email, the attachment headers look like this:

--Apple-Mail=_3F882181-A900-45E2-9908-DCA1820D40EE
Content-Transfer-Encoding: base64
Content-Disposition: inline;
	filename=IMG_2476.jpg
Content-Type: image/jpeg;
	name="IMG_2476.jpg"
Content-Id: <2354BBA8-8612-4225-9ED7-03D1F91AC7A3>

Open in new window

0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39830145
Can you show us a sample return object from the Pear decode() method?
0
 
LVL 1

Author Comment

by:fionafenton
ID: 39832113
There are several methods to email photos from a Mac, so I've done a test with each, turned debug on and this is what debug is returning for each

Using iPhoto
Found body part type text/plain
Found body part type multipart/related
Found body part type text/html
Found body part type image/jpg
Found body part type image/jpg
Found body part type image/jpg
From : xxxd@xxx.co.uk
Subject : Mac via iPhoto
Body :


Saved Files :
Array
(
) 

Open in new window

Within the headers each photo is shown as:
Content-Type: image/jpg
MIME-Version: 1.0
Content-Transfer-Encoding: base64
Content-ID: <image13CE9DC2-455E-41DD-8977-E1938EC994E3>
Content-Disposition: inline; filename="IMG_0351.jpg"

Using add attachments
Found body part type image/JPG
Found body part type image/JPG
Found body part type image/JPG
From : xxx@xxx.co.uk
Subject : Mac via email add attachments
Body : No plain text body found
Saved Files :
Array
(
) 

Open in new window

Headers:
Content-Disposition: inline;
filename=P1040008.jpeg
Content-Type: image/JPG;
name="P1040008.jpeg"
Content-Transfer-Encoding: base64

Drag photos from desktop into email
Found body part type image/JPG
Found body part type image/JPG
Found body part type image/JPG
From : xxx@xxx.co.uk
Subject : Mac via email drag from desktop multiple photos
Body : No plain text body found
Saved Files :
Array
(
) 

Open in new window

Headers:
Content-Disposition: inline;
filename=IMG_0322.jpeg
Content-Type: image/JPG;
name="IMG_0322.jpeg"
Content-Transfer-Encoding: base64

Using Photo Browser
Found body part type image/JPG
Found body part type image/jpg
Found body part type image/jpg
From : xxx@xxx.co.uk
Subject : Mac via email using photo browser
Body : No plain text body found
Saved Files :
Array
(
) 

Open in new window

Headers:
Content-Disposition: inline;
filename=P1040145.jpeg
Content-Type: image/JPG;
name="P1040145.jpeg"
Content-Transfer-Encoding: base64

The part of the code where "Body : No plain text body found" is generated is
// We might also have uuencoded files. Check for those.
        if(!isset($this->body)){
            if(isset($this->decoded->body)){
                $this->body = $this->decoded->body;
            }else{
                $this->body = "No plain text body found";
            }
        }

Open in new window

I'm guessing that it might be this that's not working correctly for Mac emails?
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 26

Assisted Solution

by:mrcoffee365
mrcoffee365 earned 125 total points
ID: 39854380
Email is not as standard a format as you might hope.  That's why a somewhat generically written piece of software can work for almost everyone except X.  Which in your case is Macs, but could as easily be one of the many other email mechanisms, such as Lotus Notes.

The developer of the freeware you found is right, you'll have to actually do some development to make the reader work with the style of email attachment you're seeing from Macs and phones.

If you don't have that development experience, you can hire people to do it for you.

It's just programming -- look for the different types of headers you're seeing, and write code to handle the different formats.  We've done this several times, and I can tell  you from experience that email formats are not as standard as we'd all like to believe.
0
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 125 total points
ID: 39854796
What MrCoffee365 said.  It's not a question with an answer; it's a research and development project with days of painstaking work that would start by developing an exhaustive test data set.  The nature of email pipes makes them hard to debug because they cannot have any browser output, since they are not started by a client request from the browser.  So you have to build your diagnostics into the pipe script itself.  Automated unit testing is non-existent; there are no mock objects for something like this.

Do you have access to a PHP user group near you?  If so you might find someone who would take this on as a freelance project.  Make sure they can show you prior experience in authoring an email pipe script.

But that said, if you can get the original author to work the issue for only $75/hour that may be your best approach.

Best of luck with it, ~Ray
0
 
LVL 1

Author Comment

by:fionafenton
ID: 39855727
The number of Mac users is only a handful and these can be given explicit instructions on how to attach photos. So I only need one solution for these.

What I have is almost there and suspect only needs a few lines of code added/amended.
I just need a starting point and can probably work out the rest myself.

I have already built in debugging into the pipe script and have a test data set of all the variations we're willing to accept.
0
 
LVL 26

Expert Comment

by:mrcoffee365
ID: 39856698
Unless you have a specific question, this isn't the place to get people to do programming work for you.  You do realize that none of us are paid -- this is a volunteer forum?  I suggest you pay that original developer for the few hours it will take to get the changes made.

Or, if you have done some work, but are getting error messages you don't understand, then you can ask about those.
0
 
LVL 1

Author Comment

by:fionafenton
ID: 39857839
I know you're volunteers, but I pay a monthly subscription to enable me to post questions here.

Does the fact that I believe I may have narrowed down the problem to a few lines of code not constitute a specific question? And clarify it by saying
I'm guessing that it might be this that's not working correctly for Mac emails?
I have been in contact with the original developer but he has told me that he hasn't got any time to look at it until the summer. I'm also not very confident that he has the ability/knowledge to be able to fix it.

I'm not asking anyone to do programming work for me - just a nudge in the right direction, even if it's only some links to some good sources of relevant info.
0
 
LVL 26

Expert Comment

by:mrcoffee365
ID: 39857974
Post the few lines of code and ask the question.  Show the incorrect actions which result from running the code you think is not working correctly.  This is an advice forum, not one where users pay to have people write programs for them.
0
 
LVL 1

Author Comment

by:fionafenton
ID: 39858568
I already have?? See my post of 4th February.
0
 
LVL 26

Expert Comment

by:mrcoffee365
ID: 39863313
Okay -- that's a script to read the input email.  Have you tried anything which reads the mac headers?
0
 
LVL 1

Author Closing Comment

by:fionafenton
ID: 40365579
I never did find the solution to this so closing the question.
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Browsers only know CSS so your awesome SASS code needs to be translated into normal CSS. Here I'll try to explain what you should aim for in order to take full advantage of SASS.
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now