We help IT Professionals succeed at work.

Upload images via e-mail

NeedExpertHelp
on
Medium Priority
276 Views
Last Modified: 2013-12-25
Hello,

I'm trying to write a dead-simple PHP script that would allow me to e-mail an image (as an attachment) to an e-mail address on my server (e.g. uploadimages@myserver.com) and then have the script upload/save the picture to my server.

How could I do it (or, alternatively, where can I find a dead-simple PHP script that already does this)?

Thanks.
Comment
Watch Question

Author

Commented:
Thanks for your reply.

What's a PIPE script?
PIPE script is a server-side script written in your favorite language like PHP that read email content and parse image header and content and write it hard disk.

If you're using CPanel then there is an option to forward email to PIPE script e.g.
http://kb.siteground.com/article/How_to_pipe_an_email_to_a_PHP_script.html

If you're working on Linux environment do the following steps.

Step 1. create a new email address e.g. uploadimages@mydomain.com
Step 2. Forward emails to your script written to handle email content e.g. /home/myusr/pipe.php
Step 3. Pipe.php is your file that parse image content from email.

Following URL have briefly describe this process.
http://activecampaign.com/support/tt/index.php?action=kb&article=145

Thanks

Author

Commented:
Thanks again for the additional info.

I will give it a try via cPanel and report back.

Where can I download the PIPE script in PHP?

Thanks.
Most Valuable Expert 2011
Author of the Year 2014

Commented:
The "dead-simple" part is going to be the issue.  It will not be dead-simple once you have email involved in the process.  So please help me understand something more about your requirement...  

If you have an image on a client computer that is capable of handling email, would you not also expect the client computer to he able to handle HTTP protocols?  And it follows that if you can use HTTP, you can use HTTP to access an image upload script on the server, right?  So what is the advantage of attaching the image to an email message, when you can simply upload it to the server with a file-upload script?

Thanks and regards, ~Ray

Author

Commented:
Ray, the answer is as simple as the script I'm looking for:  not all cell phones are compatible with type="file" uploads.

Any word on where I might find the PIPE script?
Most Valuable Expert 2011
Author of the Year 2014
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
Most Valuable Expert 2011
Author of the Year 2014

Commented:
Since you asked this question, I have had occasion to set up a PIPE script of my own.  Here are the essential moving parts.  HTH, ~Ray
#!/usr/bin/php -q
<?php // /email_pipe/index.php
 
// 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
 
$script_start_time = microtime(TRUE);
error_reporting(E_ALL);
ob_start();
 
// DATA BASE CONNECTIVITY, CONSTANTS, FUNCTIONS, ETC
require_once('config.php');
 
// TRY TO READ THE EMAIL FROM STDIN
if (!$stdin = fopen("php://stdin", "R"))
{
    echo "ERROR: UNABLE TO OPEN php://stdin \n";
    die();
}
 
// ABLE TO READ THE MAIL
else
{
    $raw_email = '';
    while (!feof($stdin))
    {
        $raw_email .= fread($stdin, 4096);
    }
    fclose($stdin);
}
$raw_email = preg_replace('| +|', ' ', $raw_email);
 
// ETC ETC...

Open in new window

Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.