nextcloud-mail2deck/index.php

81 wiersze
3.5 KiB
PHP
Czysty Zwykły widok Historia

2019-12-13 18:09:05 +00:00
<?php
2019-12-13 19:10:45 +00:00
error_reporting(E_ERROR | E_PARSE);
2019-12-13 18:22:40 +00:00
require_once("config.php");
require_once('lib/DeckClass.php');
2019-12-13 18:09:05 +00:00
$inbox = imap_open("{" . MAIL_SERVER . ":" . MAIL_SERVER_PORT . MAIL_SERVER_FLAGS . "}INBOX", MAIL_USER, MAIL_PASSWORD)
2019-12-13 18:22:40 +00:00
or die("can't connect:" . imap_last_error());
2019-12-13 18:09:05 +00:00
2019-12-16 17:57:55 +00:00
$emails = imap_search($inbox, 'UNSEEN');
2019-12-13 18:09:05 +00:00
2019-12-16 17:56:37 +00:00
if ($emails)
for ($j = 0; $j < count($emails) && $j < 5; $j++) {
2019-12-16 17:56:37 +00:00
$structure = imap_fetchstructure($inbox, $emails[$j]);
$attachments = array();
$attNames = array();
2019-12-16 17:56:37 +00:00
if (isset($structure->parts) && count($structure->parts)) {
for ($i = 0; $i < count($structure->parts); $i++) {
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;
}
}
2019-12-13 18:22:40 +00:00
}
2019-12-16 17:56:37 +00:00
if ($attachments[$i]['is_attachment']) {
$attachments[$i]['attachment'] = imap_fetchbody($inbox, $emails[$j], $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']);
}
2019-12-13 18:22:40 +00:00
}
}
}
for ($i = 1; $i <= count($attachments); $i++) {
if(! file_exists(getcwd() . '/attachments')) {
mkdir(getcwd() . '/attachments');
}
2019-12-16 17:56:37 +00:00
if ($attachments[$i]['is_attachment'] == 1) {
$filename = $attachments[$i]['name'];
if (empty($filename)) $filename = $attachments[$i]['filename'];
2019-12-13 18:22:40 +00:00
$fp = fopen(getcwd() . '/attachments/' . $filename, "w+");
2019-12-16 17:56:37 +00:00
fwrite($fp, $attachments[$i]['attachment']);
fclose($fp);
array_push($attNames, $attachments[$i]['filename']);
2019-12-16 17:56:37 +00:00
}
}
2019-12-13 18:22:40 +00:00
2019-12-16 17:56:37 +00:00
$overview = imap_headerinfo($inbox, $emails[$j]);
$data = new stdClass();
$data->title = DECODE_SPECIAL_CHARACTERS ? mb_decode_mimeheader($overview->subject) : $overview->subject;
$data->type = "plain";
if(count($attachments)) {
$data->attachments = $attNames;
$description = DECODE_SPECIAL_CHARACTERS ? quoted_printable_decode(imap_fetchbody($inbox, $emails[$j], 1.1)) : imap_fetchbody($inbox, $emails[$j], 1.1);
2019-12-16 17:56:37 +00:00
} else {
$description = DECODE_SPECIAL_CHARACTERS ? quoted_printable_decode(imap_fetchbody($inbox, $emails[$j], 1)) : imap_fetchbody($inbox, $emails[$j], 1);
2019-12-16 17:56:37 +00:00
}
$data->description = $description;
2019-12-13 18:22:40 +00:00
2019-12-16 17:56:37 +00:00
$newcard = new DeckClass();
$newcard->addCard($data);
2019-12-13 18:22:40 +00:00
}
imap_close($inbox);
?>