kopia lustrzana https://github.com/newroco/mail2deck
Delete mail after processing and use psr-4 for autoload. fixes #17
rodzic
a809d456cd
commit
395d9ed512
|
@ -1,5 +1,10 @@
|
||||||
{
|
{
|
||||||
"require": {
|
"require": {
|
||||||
"league/html-to-markdown": "^5.1"
|
"league/html-to-markdown": "^5.1"
|
||||||
|
},
|
||||||
|
"autoload": {
|
||||||
|
"psr-4": {
|
||||||
|
"Mail2Deck\\": "lib/"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,4 +11,5 @@ define("MAIL_PASSWORD", "****");
|
||||||
define("DECODE_SPECIAL_CHARACTERS", true); //requires mbstring, if false special characters (like öäüß) won't be displayed correctly
|
define("DECODE_SPECIAL_CHARACTERS", true); //requires mbstring, if false special characters (like öäüß) won't be displayed correctly
|
||||||
define("ASSIGN_SENDER", true); // if true, sender will be assigned to card if has NC account
|
define("ASSIGN_SENDER", true); // if true, sender will be assigned to card if has NC account
|
||||||
define("MAIL_NOTIFICATION", true); // if true, send notifications when a new card was created or an error occured
|
define("MAIL_NOTIFICATION", true); // if true, send notifications when a new card was created or an error occured
|
||||||
|
define("DELETE_MAIL_AFTER_PROCESSING", true);
|
||||||
?>
|
?>
|
||||||
|
|
207
index.php
207
index.php
|
@ -2,117 +2,124 @@
|
||||||
error_reporting(E_ERROR | E_PARSE);
|
error_reporting(E_ERROR | E_PARSE);
|
||||||
require __DIR__ . '/vendor/autoload.php';
|
require __DIR__ . '/vendor/autoload.php';
|
||||||
require_once("config.php");
|
require_once("config.php");
|
||||||
require_once('lib/DeckClass.php');
|
|
||||||
require_once('lib/MailClass.php');
|
use Mail2Deck\MailClass;
|
||||||
require_once('lib/ConvertToMD.php');
|
use Mail2Deck\DeckClass;
|
||||||
|
use Mail2Deck\ConvertToMD;
|
||||||
|
|
||||||
$inbox = new MailClass();
|
$inbox = new MailClass();
|
||||||
$emails = $inbox->getNewMessages();
|
$emails = $inbox->getNewMessages();
|
||||||
|
|
||||||
if ($emails)
|
if(!$emails) return; // nothing to do
|
||||||
for ($j = 0; $j < count($emails) && $j < 5; $j++) {
|
|
||||||
$structure = $inbox->fetchMessageStructure($emails[$j]);
|
|
||||||
$base64encode = false;
|
|
||||||
if($structure->encoding == 3) {
|
|
||||||
$base64encode = true; // BASE64
|
|
||||||
}
|
|
||||||
$attachments = array();
|
|
||||||
$attNames = array();
|
|
||||||
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) {
|
for ($j = 0; $j < count($emails) && $j < 5; $j++) {
|
||||||
foreach ($structure->parts[$i]->parameters as $object) {
|
$structure = $inbox->fetchMessageStructure($emails[$j]);
|
||||||
if (strtolower($object->attribute) == 'name') {
|
$base64encode = false;
|
||||||
$attachments[$i]['is_attachment'] = true;
|
if($structure->encoding == 3) {
|
||||||
$attachments[$i]['name'] = $object->value;
|
$base64encode = true; // BASE64
|
||||||
}
|
}
|
||||||
}
|
$attachments = array();
|
||||||
}
|
$attNames = array();
|
||||||
|
if (isset($structure->parts) && count($structure->parts)) {
|
||||||
if ($attachments[$i]['is_attachment']) {
|
for ($i = 0; $i < count($structure->parts); $i++) {
|
||||||
$attachments[$i]['attachment'] = $inbox->fetchMessageBody($emails[$j], $i+1);
|
if ($structure->parts[$i]->ifdparameters) {
|
||||||
if ($structure->parts[$i]->encoding == 3) { // 3 = BASE64
|
foreach ($structure->parts[$i]->dparameters as $object) {
|
||||||
$attachments[$i]['attachment'] = base64_decode($attachments[$i]['attachment']);
|
if (strtolower($object->attribute) == 'filename') {
|
||||||
}
|
$attachments[$i]['is_attachment'] = true;
|
||||||
elseif ($structure->parts[$i]->encoding == 4) { // 4 = QUOTED-PRINTABLE
|
$attachments[$i]['filename'] = $object->value;
|
||||||
$attachments[$i]['attachment'] = quoted_printable_decode($attachments[$i]['attachment']);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
for ($i = 1; $i <= count($attachments); $i++) {
|
if ($structure->parts[$i]->ifparameters) {
|
||||||
if(! file_exists(getcwd() . '/attachments')) {
|
foreach ($structure->parts[$i]->parameters as $object) {
|
||||||
mkdir(getcwd() . '/attachments');
|
if (strtolower($object->attribute) == 'name') {
|
||||||
|
$attachments[$i]['is_attachment'] = true;
|
||||||
|
$attachments[$i]['name'] = $object->value;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if ($attachments[$i]['is_attachment'] == 1) {
|
|
||||||
$filename = $attachments[$i]['name'];
|
|
||||||
if (empty($filename)) $filename = $attachments[$i]['filename'];
|
|
||||||
|
|
||||||
$fp = fopen(getcwd() . '/attachments/' . $filename, "w+");
|
if ($attachments[$i]['is_attachment']) {
|
||||||
fwrite($fp, $attachments[$i]['attachment']);
|
$attachments[$i]['attachment'] = $inbox->fetchMessageBody($emails[$j], $i+1);
|
||||||
fclose($fp);
|
if ($structure->parts[$i]->encoding == 3) { // 3 = BASE64
|
||||||
array_push($attNames, $attachments[$i]['filename']);
|
$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']);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$overview = $inbox->headerInfo($emails[$j]);
|
|
||||||
$board = null;
|
|
||||||
if(isset($overview->{'X-Original-To'}) && strstr($overview->{'X-Original-To'}, '+')) {
|
|
||||||
$board = strstr(substr($overview->{'X-Original-To'}, strpos($overview->{'X-Original-To'}, '+') + 1), '@', true);
|
|
||||||
} else {
|
|
||||||
if(strstr($overview->to[0]->mailbox, '+')) {
|
|
||||||
$board = substr($overview->to[0]->mailbox, strpos($overview->to[0]->mailbox, '+') + 1);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
if(strstr($board, '+')) $board = str_replace('+', ' ', $board);
|
|
||||||
|
|
||||||
$data = new stdClass();
|
|
||||||
$data->title = DECODE_SPECIAL_CHARACTERS ? mb_decode_mimeheader($overview->subject) : $overview->subject;
|
|
||||||
$data->type = "plain";
|
|
||||||
$data->order = -time();
|
|
||||||
$body = $inbox->fetchMessageBody($emails[$j], 1.1);
|
|
||||||
if ($body == "") {
|
|
||||||
$body = $inbox->fetchMessageBody($emails[$j], 1);
|
|
||||||
}
|
|
||||||
if(count($attachments)) {
|
|
||||||
$data->attachments = $attNames;
|
|
||||||
$description = DECODE_SPECIAL_CHARACTERS ? quoted_printable_decode($body) : $body;
|
|
||||||
} else {
|
|
||||||
$description = DECODE_SPECIAL_CHARACTERS ? quoted_printable_decode($body) : $body;
|
|
||||||
}
|
|
||||||
if($base64encode) {
|
|
||||||
$description = base64_decode($description);
|
|
||||||
}
|
|
||||||
if($description != strip_tags($description)) {
|
|
||||||
$description = (new ConvertToMD($description))->execute();
|
|
||||||
}
|
|
||||||
$data->description = $description;
|
|
||||||
$mailSender = new stdClass();
|
|
||||||
$mailSender->userId = $overview->reply_to[0]->mailbox;
|
|
||||||
|
|
||||||
$newcard = new DeckClass();
|
|
||||||
$response = $newcard->addCard($data, $mailSender, $board);
|
|
||||||
$mailSender->origin .= "{$overview->reply_to[0]->mailbox}@{$overview->reply_to[0]->host}";
|
|
||||||
|
|
||||||
if(MAIL_NOTIFICATION) {
|
|
||||||
if($response) {
|
|
||||||
$inbox->reply($mailSender->origin, $response);
|
|
||||||
} else {
|
|
||||||
$inbox->reply($mailSender->origin);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if(!$response) {
|
|
||||||
foreach($attNames as $attachment) unlink(getcwd() . "/attachments/" . $attachment);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
for ($i = 1; $i <= count($attachments); $i++) {
|
||||||
|
if(! file_exists(getcwd() . '/attachments')) {
|
||||||
|
mkdir(getcwd() . '/attachments');
|
||||||
|
}
|
||||||
|
if ($attachments[$i]['is_attachment'] == 1) {
|
||||||
|
$filename = $attachments[$i]['name'];
|
||||||
|
if (empty($filename)) $filename = $attachments[$i]['filename'];
|
||||||
|
|
||||||
|
$fp = fopen(getcwd() . '/attachments/' . $filename, "w+");
|
||||||
|
fwrite($fp, $attachments[$i]['attachment']);
|
||||||
|
fclose($fp);
|
||||||
|
array_push($attNames, $attachments[$i]['filename']);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$overview = $inbox->headerInfo($emails[$j]);
|
||||||
|
$board = null;
|
||||||
|
if(isset($overview->{'X-Original-To'}) && strstr($overview->{'X-Original-To'}, '+')) {
|
||||||
|
$board = strstr(substr($overview->{'X-Original-To'}, strpos($overview->{'X-Original-To'}, '+') + 1), '@', true);
|
||||||
|
} else {
|
||||||
|
if(strstr($overview->to[0]->mailbox, '+')) {
|
||||||
|
$board = substr($overview->to[0]->mailbox, strpos($overview->to[0]->mailbox, '+') + 1);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
if(strstr($board, '+')) $board = str_replace('+', ' ', $board);
|
||||||
|
|
||||||
|
$data = new stdClass();
|
||||||
|
$data->title = DECODE_SPECIAL_CHARACTERS ? mb_decode_mimeheader($overview->subject) : $overview->subject;
|
||||||
|
$data->type = "plain";
|
||||||
|
$data->order = -time();
|
||||||
|
$body = $inbox->fetchMessageBody($emails[$j], 1.1);
|
||||||
|
if ($body == "") {
|
||||||
|
$body = $inbox->fetchMessageBody($emails[$j], 1);
|
||||||
|
}
|
||||||
|
if(count($attachments)) {
|
||||||
|
$data->attachments = $attNames;
|
||||||
|
$description = DECODE_SPECIAL_CHARACTERS ? quoted_printable_decode($body) : $body;
|
||||||
|
} else {
|
||||||
|
$description = DECODE_SPECIAL_CHARACTERS ? quoted_printable_decode($body) : $body;
|
||||||
|
}
|
||||||
|
if($base64encode) {
|
||||||
|
$description = base64_decode($description);
|
||||||
|
}
|
||||||
|
if($description != strip_tags($description)) {
|
||||||
|
$description = (new ConvertToMD($description))->execute();
|
||||||
|
}
|
||||||
|
$data->description = $description;
|
||||||
|
$mailSender = new stdClass();
|
||||||
|
$mailSender->userId = $overview->reply_to[0]->mailbox;
|
||||||
|
|
||||||
|
$newcard = new DeckClass();
|
||||||
|
$response = $newcard->addCard($data, $mailSender, $board);
|
||||||
|
$mailSender->origin .= "{$overview->reply_to[0]->mailbox}@{$overview->reply_to[0]->host}";
|
||||||
|
|
||||||
|
if(MAIL_NOTIFICATION) {
|
||||||
|
if($response) {
|
||||||
|
$inbox->reply($mailSender->origin, $response);
|
||||||
|
} else {
|
||||||
|
$inbox->reply($mailSender->origin);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(!$response) {
|
||||||
|
foreach($attNames as $attachment) unlink(getcwd() . "/attachments/" . $attachment);
|
||||||
|
}
|
||||||
|
|
||||||
|
//remove email after processing
|
||||||
|
if(DELETE_MAIL_AFTER_PROCESSING) {
|
||||||
|
$inbox->delete($emails[$j]);
|
||||||
|
}
|
||||||
|
}
|
||||||
?>
|
?>
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
namespace Mail2Deck;
|
||||||
|
|
||||||
use League\HTMLToMarkdown\HtmlConverter;
|
use League\HTMLToMarkdown\HtmlConverter;
|
||||||
|
|
||||||
class ConvertToMD {
|
class ConvertToMD {
|
||||||
|
@ -18,5 +20,3 @@ class ConvertToMD {
|
||||||
return $this->converter->convert($this->html);
|
return $this->converter->convert($this->html);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
?>
|
|
|
@ -1,5 +1,10 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
namespace Mail2Deck;
|
||||||
|
|
||||||
|
use stdClass;
|
||||||
|
use CURLFile;
|
||||||
|
|
||||||
class DeckClass {
|
class DeckClass {
|
||||||
private $responseCode;
|
private $responseCode;
|
||||||
|
|
||||||
|
@ -141,4 +146,3 @@ class DeckClass {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
?>
|
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
namespace Mail2Deck;
|
||||||
|
|
||||||
class MailClass {
|
class MailClass {
|
||||||
private $inbox;
|
private $inbox;
|
||||||
|
|
||||||
|
@ -60,8 +62,8 @@ class MailClass {
|
||||||
);
|
);
|
||||||
|
|
||||||
if($response) {
|
if($response) {
|
||||||
$body = "<h1>A new card has been created on board <a href=\"" . NC_SERVER . "index.php/apps/deck/#/board/{$response->board}" . "\">{$response->boardTitle}</a>.</h1>
|
$body = "<h1>A new card has been created on board <a href=\"" . NC_SERVER . "/index.php/apps/deck/#/board/{$response->board}" . "\">{$response->boardTitle}</a>.</h1>
|
||||||
<p>Check out this <a href=\"" . NC_SERVER . "index.php/apps/deck/#/board/{$response->board}/card/{$response->id}" . "\">link</a> to see the newly created card.</p>
|
<p>Check out this <a href=\"" . NC_SERVER . "/index.php/apps/deck/#/board/{$response->board}/card/{$response->id}" . "\">link</a> to see the newly created card.</p>
|
||||||
<p>Card ID is {$response->id}</p>";
|
<p>Card ID is {$response->id}</p>";
|
||||||
$subject = 'A new card has been created!';
|
$subject = 'A new card has been created!';
|
||||||
} else {
|
} else {
|
||||||
|
@ -76,4 +78,17 @@ class MailClass {
|
||||||
|
|
||||||
mail($sender, $subject, $message, $headers);
|
mail($sender, $subject, $message, $headers);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Deletes a mail
|
||||||
|
*
|
||||||
|
* @param $email email id that you want to delete
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function delete(int $email)
|
||||||
|
{
|
||||||
|
imap_delete($this->inbox, $email);
|
||||||
|
imap_expunge($this->inbox);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Ładowanie…
Reference in New Issue