SamMonk
09-18-2009, 07:57 PM
Hi guys,
Can someone help me finish this script i found. I want to be able to email myself photos and have them put into a directory. This script gets me all the way to the point of isolating the content I want, but how can I write it to a directory on my server. I will be emailing jpeg's from my phone to later display.
<?php
// Read email and process picture attachment according to the subject line.
ini_set("display_errors", "1");
$server = "{zimprops.com:143/notls}INBOX";
$username = "username";
$password = "password";
$mbox = imap_open($server, $username, $password);
echo("mbox = " . $mbox . "<br><br>");
$headers = imap_headers($mbox);
if ($headers == false) {
print_r(imap_errors());
} else {
$j = 0;
foreach ($headers as $val) {
$j += 1;
$info = imap_headerinfo($mbox,$j);
// echo "Recent -> " . $info->Recent;
echo "<br><br>-----------------------------";
echo "<br>Unseen -> " . $info->Unseen;
echo "<br>Subject -> " . $info->subject;
echo "<br>Message ID -> " . $info->message_id;
echo "<br>Msgno -> " . $info->Msgno;
$structure = imap_fetchstructure($mbox, $info->Msgno);
$attachments = array();
if (isset($structure->parts) && count($structure->parts)) {
for ($i = 0; $i < count($structure->parts); $i++) {
$attachments[$i] = array(
'is_attachment' => false,
'filename' => '',
'name' => '',
'attachment' => ''
);
if($structure->parts[$i]->ifdparameters) {
foreach($structure->parts[$i]->dparameters as $object) {
if(strtolower($object->attribute) == 'filename') {
$attachments[$i]['is_attachment'] = true;
$attachments[$i]['filename'] = $object->value;
}
}
}
if($structure->parts[$i]->ifparameters) {
foreach($structure->parts[$i]->parameters as $object) {
if(strtolower($object->attribute) == 'name') {
$attachments[$i]['is_attachment'] = true;
$attachments[$i]['name'] = $object->value;
}
}
}
if($attachments[$i]['is_attachment']) {
$attachments[$i]['attachment'] = imap_fetchbody(
$mbox, $info->Msgno, $i+1);
if($structure->parts[$i]->encoding == 3) {
// 3 = BASE64
$attachments[$i]['attachment'] =
base64_decode($attachments[$i]['attachment']);
} elseif($structure->parts[$i]->encoding == 4) {
// 4 = QUOTED-PRINTABLE
$attachments[$i]['attachment'] =
quoted_printable_decode($attachments[$i]['attachment']);
}
echo "<br>" . $attachments[$i]['filename'];
echo "<br>" . $attachments[$i]['attachment'];
}
}
}
}
}
imap_close($mbox);
?>
I found this on another forum and I think it comes very close to doing what I want to do. It seems to just be missing the final part.
Thank you,
-Sam
Can someone help me finish this script i found. I want to be able to email myself photos and have them put into a directory. This script gets me all the way to the point of isolating the content I want, but how can I write it to a directory on my server. I will be emailing jpeg's from my phone to later display.
<?php
// Read email and process picture attachment according to the subject line.
ini_set("display_errors", "1");
$server = "{zimprops.com:143/notls}INBOX";
$username = "username";
$password = "password";
$mbox = imap_open($server, $username, $password);
echo("mbox = " . $mbox . "<br><br>");
$headers = imap_headers($mbox);
if ($headers == false) {
print_r(imap_errors());
} else {
$j = 0;
foreach ($headers as $val) {
$j += 1;
$info = imap_headerinfo($mbox,$j);
// echo "Recent -> " . $info->Recent;
echo "<br><br>-----------------------------";
echo "<br>Unseen -> " . $info->Unseen;
echo "<br>Subject -> " . $info->subject;
echo "<br>Message ID -> " . $info->message_id;
echo "<br>Msgno -> " . $info->Msgno;
$structure = imap_fetchstructure($mbox, $info->Msgno);
$attachments = array();
if (isset($structure->parts) && count($structure->parts)) {
for ($i = 0; $i < count($structure->parts); $i++) {
$attachments[$i] = array(
'is_attachment' => false,
'filename' => '',
'name' => '',
'attachment' => ''
);
if($structure->parts[$i]->ifdparameters) {
foreach($structure->parts[$i]->dparameters as $object) {
if(strtolower($object->attribute) == 'filename') {
$attachments[$i]['is_attachment'] = true;
$attachments[$i]['filename'] = $object->value;
}
}
}
if($structure->parts[$i]->ifparameters) {
foreach($structure->parts[$i]->parameters as $object) {
if(strtolower($object->attribute) == 'name') {
$attachments[$i]['is_attachment'] = true;
$attachments[$i]['name'] = $object->value;
}
}
}
if($attachments[$i]['is_attachment']) {
$attachments[$i]['attachment'] = imap_fetchbody(
$mbox, $info->Msgno, $i+1);
if($structure->parts[$i]->encoding == 3) {
// 3 = BASE64
$attachments[$i]['attachment'] =
base64_decode($attachments[$i]['attachment']);
} elseif($structure->parts[$i]->encoding == 4) {
// 4 = QUOTED-PRINTABLE
$attachments[$i]['attachment'] =
quoted_printable_decode($attachments[$i]['attachment']);
}
echo "<br>" . $attachments[$i]['filename'];
echo "<br>" . $attachments[$i]['attachment'];
}
}
}
}
}
imap_close($mbox);
?>
I found this on another forum and I think it comes very close to doing what I want to do. It seems to just be missing the final part.
Thank you,
-Sam