Jump to content

Upload via email?


rule69

Recommended Posts

<?php

// Get picture attachments for uploading to Gallery
// 08-13-06 v0.5

$pid=2;

$type = array("text", "multipart", "message", "application", "audio", "image", "video", "other");
$encoding = array("7bit", "8bit", "binary", "base64", "quoted-printable", "other");

$tempdir = "tempdir";
if(!is_dir($tempdir))
{
mkdir($tempdir);
}

// open POP connection
// use the next line for gmail WITHOUT stunnel
// $inbox = @imap_open ("{pop.gmail.com:995/pop3}INBOX", "[email protected]", "password");
// use the next line for gmail WITH stunnel (change the port)
// $inbox = @imap_open ("{127.0.0.1:1234/pop3}INBOX", "[email protected]", "password");
// use the next line for NORMAL POP connection !!! CHANGE !!! server, username, password
$inbox = @imap_open ("{localhost:110/pop3}", "rule63", "1234");

// parse message body
function parse($structure)
{
global $type;
global $encoding;

// create an array to hold message sections
$ret = array();

// split structure into parts
$parts = $structure->parts;

for($x=0; $x<sizeof($parts); $x++)
{
$ret[$x]["pid"] = ($x+1);
$this2 = $parts[$x];

// default to text
if ($this->type == "")
{
$this->type = 0;
}
$ret[$x]["type"] = $type[$this->type] . "/" . strtolower($this->subtype);

// default to 7bit
if ($this->encoding == "")
{
$this->encoding = 0;
}
$ret[$x]["encoding"] = $encoding[$this->encoding];

$ret[$x]["size"] = strtolower($this->bytes);

$ret[$x]["disposition"] = strtolower($this->disposition);

if ($this->ifparameters == 1)
{
$params = $this->parameters;
foreach ($params as $p)
{
if($p->attribute == "NAME")
{
$ret[$x]["name"] = $p->value;
break;
}
}
}
}
return $ret;
}

function get_attachments($arr)
{
for($x=0; $x < sizeof($arr); $x++)
{
if($arr[$x]["disposition"] == "attachment")
{
$ret[] = $arr[$x];
}
}
return $ret;
}

$count = @imap_num_msg($inbox);
echo "count: " . $count . "\n";

// get message headers and structure
for ($c = 1; $c <= $count; $c++)
{
$id = $c;
$headers = imap_header($inbox, $id);
$structure = imap_fetchstructure($inbox, $id);

// if multipart, parse
if(sizeof($structure->parts) > 1)
{
$sections = parse($structure);
$attachments = get_attachments($sections);
}

// debug
// echo "Structure of message: " . $id . "\n";
// print_r($structure);
// echo "\n";
// echo "Sections of message: " . $id . "\n";
// print_r($sections);
// echo "\n";
// debug

// look for specified part
for($x=0; $x<sizeof($sections); $x++)
{
if($sections[$x]["pid"] == $pid)
{
$dtype = $sections[$x]["type"];
$dencoding = $sections[$x]["encoding"];
$filename = $sections[$x]["name"];
}
}

// debug
// echo " type: " . $dtype . "\n";
// echo "encoding: " . $dencoding . "\n";
// echo "filename: " . $filename . "\n";
// echo " id: " . $id . "\n\n";
// debug

$attachment = imap_fetchbody($inbox, $id, $pid);

if ($dencoding == "base64")
{
// Decode and save attachment to a file
list($usec, $sec) = explode(" ", microtime());
$usec = substr($usec,2,3);
$name = date('Ymd.His');
$fp=fopen($tempdir . "/Photo." . $name . "." . $usec . ".jpg","w");
fwrite($fp,imap_base64($attachment));
fclose($fp);
}
// else
// {
// add handlers for other encodings here
echo $attachment;
// }
}

// delete all emails
for ($i = 1; $i <= $count; $i++)
{
imap_delete($inbox, $i);
}
imap_expunge($inbox);

imap_close($inbox);

?>

 

I found this code snippet on the net.. that helps with saving images to a folder from a email attachment.. tried and tried but all it does is creat the temp directory and delete the mail from the inbox without storing the attched image..  ???.. any1 got any suggestions? of a working snippet or could get this right... thanks

Link to comment
https://forums.phpfreaks.com/topic/104017-upload-via-email/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.