Skip to content

Commit

Permalink
Added MailHelper class
Browse files Browse the repository at this point in the history
  • Loading branch information
alfredbuehler committed Feb 23, 2023
1 parent 221a6fb commit eb2623e
Show file tree
Hide file tree
Showing 5 changed files with 174 additions and 174 deletions.
46 changes: 6 additions & 40 deletions Action/ConvertEmailToComment.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,19 +81,13 @@ public function doAction(array $data)
$project = $this->projectModel->getById($data['project_id']);
$emails = array();

$mailbox = $this->login();

try {
// Search in mailbox folder for specific emails
// PHP.net imap_search criteria: http://php.net/manual/en/function.imap-search.php
$mails_ids = $mailbox->searchMailbox('UNSEEN TO ' . self::PREFIX);
} catch(PhpImap\Exceptions\ConnectionException $ex) {
die();
if (($mailbox = $this->helper->mailHelper->login()) === false) {
return;
}

foreach ($mails_ids as $mail_id) {
$i = 0;
$mails_ids = $this->helper->mailHelper->getUnseenMails($mailbox, self::PREFIX);

foreach ($mails_ids as $mail_id) {
// Get mail by $mail_id
$email = $mailbox->getMail(
$mail_id, // ID of the email, you want to get
Expand All @@ -102,12 +96,7 @@ public function doAction(array $data)

$from_name = (isset($email->fromName)) ? $email->fromName : $email->fromAddress;
$from_email = $email->fromAddress;
foreach ($email->to as $to) {
if ($i === 0 && $to != null) {
(strpos($to, self::PREFIX) == 0) ? $task_id = str_replace(self::PREFIX, '', $to) : $task_id = null;
}
$i++;
}
$task_id = $this->helper->mailHelper->getItemId($email, self::PREFIX);
$subject = $email->subject;
$message_id = $email->messageId;
$date = $email->date;
Expand Down Expand Up @@ -160,31 +149,8 @@ public function doAction(array $data)
$this->commentModel->create($values);
}

$option = $this->configModel->get('mailmagik_pref', '2');

if ($option == 2) {
$mailbox->markMailAsRead($mail_id);
} else {
$mailbox->deleteMail($mail_id);
}
$this->helper->mailHelper->processMessage($mailbox, $mail_id);
}
}
}

public function login()
{
$server = $this->configModel->get('mailmagik_server', '');
$port = $this->configModel->get('mailmagik_port', '');
$user = $this->configModel->get('mailmagik_user', '');
$password = $this->configModel->get('mailmagik_password', '');

$mailbox = new PhpImap\Mailbox(
'{'.$server.':' . $port . '/imap/ssl}INBOX',
$user,
$password,
false
);

return $mailbox;
}
}
52 changes: 9 additions & 43 deletions Action/ConvertEmailToTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@

require __DIR__.'/../vendor/autoload.php';

use Kanboard\Action\Base;
use Kanboard\Controller\BaseController;
use Kanboard\Model\UserModel;
use Kanboard\Model\ProjectModel;
use PhpImap;
use Kanboard\Model\TaskModel;
use Kanboard\Action\Base;
use Kanboard\Model\UserModel;
use League\HTMLToMarkdown\HtmlConverter;
use PhpImap;

/**
* Action to convert email to a task
Expand Down Expand Up @@ -82,19 +82,13 @@ public function doAction(array $data)
$project = $this->projectModel->getById($data['project_id']);
$emails = array();

$mailbox = $this->login();

try {
// Search in mailbox folder for specific emails
// PHP.net imap_search criteria: http://php.net/manual/en/function.imap-search.php
$mails_ids = $mailbox->searchMailbox('UNSEEN TO ' . self::PREFIX);
} catch(PhpImap\Exceptions\ConnectionException $ex) {
die();
if (($mailbox = $this->helper->mailHelper->login()) === false) {
return;
}

foreach ($mails_ids as $mail_id) {
$i = 0;
$mails_ids = $this->helper->mailHelper->getUnseenMails($mailbox, self::PREFIX);

foreach ($mails_ids as $mail_id) {
// Get mail by $mail_id
$email = $mailbox->getMail(
$mail_id, // ID of the email, you want to get
Expand All @@ -103,12 +97,7 @@ public function doAction(array $data)

$from_name = (isset($email->fromName)) ? $email->fromName : $email->fromAddress;
$from_email = $email->fromAddress;
foreach ($email->to as $to) {
if ($i === 0 && $to != null) {
(strpos($to, self::PREFIX) == 0) ? $project_id = str_replace(self::PREFIX, '', $to) : $project_id = null;
}
$i++;
}
$project_id = $this->helper->mailHelper->getItemId($email, self::PREFIX);
$subject = $email->subject;
$message_id = $email->messageId;
$date = $email->date;
Expand Down Expand Up @@ -184,34 +173,11 @@ public function doAction(array $data)
}
}

$option = $this->configModel->get('mailmagik_pref', '2');

if ($option == 2) {
$mailbox->markMailAsRead($mail_id);
} else {
$mailbox->deleteMail($mail_id);
}
$this->helper->mailHelper->processMessage($mailbox, $mail_id);
}
}
}

public function login()
{
$server = $this->configModel->get('mailmagik_server', '');
$port = $this->configModel->get('mailmagik_port', '');
$user = $this->configModel->get('mailmagik_user', '');
$password = $this->configModel->get('mailmagik_password', '');

$mailbox = new PhpImap\Mailbox(
'{'.$server.':' . $port . '/imap/ssl}INBOX',
$user,
$password,
false
);

return $mailbox;
}

/**
* Scan and extract task attributes from subject.
*
Expand Down
125 changes: 35 additions & 90 deletions Controller/EmailViewController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@
require __DIR__.'/../vendor/autoload.php';

use Kanboard\Controller\BaseController;
use Kanboard\Model\UserModel;
use Kanboard\Model\ProjectModel;
use Kanboard\Model\TaskFinderModel;
use Kanboard\Model\CommentModel;
use Kanboard\Model\TaskFileModel;
use Kanboard\Model\TaskLinkModel;
use Kanboard\Model\LinkModel;
use Kanboard\Model\ProjectModel;
use Kanboard\Model\TaskExternalLinkModel;
use Kanboard\Model\TaskFileModel;
use Kanboard\Model\TaskFinderModel;
use Kanboard\Model\TaskLinkModel;
use Kanboard\Model\TaskTagModel;
use Kanboard\Model\UserMetadataModel;
use PhpImap;
use Kanboard\Model\UserModel;
use League\HTMLToMarkdown\HtmlConverter;
use PhpImap;

/**
* Mailmagik Plugin
Expand All @@ -30,18 +30,18 @@ class EmailViewController extends BaseController

public function load()
{
$mailbox = $this->login();
$task = $this->getTask();

if ($mailbox != false) {
if (($mailbox = $this->helper->mailHelper->login()) == false) {
$this->flashFailure();
$this->response->redirect($this->helper->url->to('TaskViewController', 'show', array('task_id' => $task['id']), false, '', '', $this->request->isAjax()));
} else {
$this->response->html($this->helper->layout->task('mailmagik:task_emails/task_load', array(
'task' => $task,
'project' => $this->projectModel->getById($task['project_id']),
'title' => $task['title'],
'tags' => $this->taskTagModel->getTagsByTask($task['id']),
)));
} else {
$this->response->redirect($this->helper->url->to('TaskViewController', 'show', array('task_id' => $task['id']), false, '', '', $this->request->isAjax()));
}
}

Expand All @@ -50,23 +50,13 @@ public function view()
$task = $this->getTask();
$emails = array();

$mailbox = $this->login();

if ($mailbox != false) {
try {
// Search in mailbox folder for specific emails
// PHP.net imap_search criteria: http://php.net/manual/en/function.imap-search.php
// Here, we search for "all" emails
$mails_ids = $mailbox->searchMailbox('TO ' . self::PREFIX);
} catch(PhpImap\Exceptions\ConnectionException $ex) {
$this->flash->failure(t('IMAP Server connection could not be established!'));
$this->response->redirect($this->helper->url->to('TaskViewController', 'show', array('task_id' => $task['id']), false, '', '', $this->request->isAjax()));
die();
}
if (($mailbox = $this->helper->mailHelper->login()) == false) {
$this->flashFailure();
$this->response->redirect($this->helper->url->to('TaskViewController', 'show', array('task_id' => $task['id']), false, '', '', $this->request->isAjax()));
} else {
$mails_ids = $this->helper->mailHelper->getTaskMails($mailbox, self::PREFIX);

foreach ($mails_ids as $mail_id) {
$i = 0;

// Get mail by $mail_id
$email = $mailbox->getMail(
$mail_id, // ID of the email, you want to get
Expand All @@ -75,12 +65,7 @@ public function view()

$from_name = (isset($email->fromName)) ? $email->fromName : $email->fromAddress;
$from_email = $email->fromAddress;
foreach ($email->to as $to) {
if ($i === 0 && $to != null) {
(strpos($to, self::PREFIX) == 0) ? $task_id = trim(str_replace(self::PREFIX, '', $to), ' ') : $task_id = null;
}
$i++;
}
$task_id = $this->helper->mailHelper->getItemId($email, self::PREFIX);
$subject = $email->subject;
$message_id = $email->messageId;
$date = $email->date;
Expand Down Expand Up @@ -191,11 +176,12 @@ public function delete()
$mail_id = $this->request->getIntegerParam('mail_id');
$task_id = $this->request->getIntegerParam('task_id');

$mailbox = $this->login();

$mailbox->deleteMail($mail_id);
$mailbox->disconnect();

if (($mailbox = $this->helper->mailHelper->login()) == false) {
$this->flashFailure();
} else {
$mailbox->deleteMail($mail_id);
$mailbox->disconnect();
}
$this->view($task_id);
}

Expand All @@ -211,9 +197,9 @@ public function convertToTask()
$task_id = $this->request->getIntegerParam('task_id');
$task = $this->getTask();

$mailbox = $this->login();

if ($mailbox != false) {
if (($mailbox = $this->helper->mailHelper->login()) == false) {
$this->flashFailure();
} else {
$email = $mailbox->getMail(
$mail_id,
false
Expand All @@ -231,14 +217,12 @@ public function convertToTask()
}
$message = $email->textPlain;


if ($email->hasAttachments()) {
$has_attach = 'y';
} else {
$has_attach = 'n';
}


$task_id = $this->taskCreationModel->create(array(
'project_id' => $task['project_id'],
'title' => $subject,
Expand All @@ -261,15 +245,8 @@ public function convertToTask()
unlink($tmp_name);
}
}
}


$option = $this->configModel->get('mailmagik_pref', '2');

if ($option == 2) {
$mailbox->markMailAsRead($mail_id);
} else {
$mailbox->deleteMail($mail_id);
$this->helper->mailHelper->processMessage($mailbox, $mail_id);
}

$this->response->redirect($this->helper->url->to('TaskViewController', 'show', array('task_id' => $task_id), false, '', '', $this->request->isAjax()));
Expand All @@ -287,9 +264,10 @@ public function convertToComment()
$mail_id = $this->request->getIntegerParam('mail_id');
$task_id = $this->request->getIntegerParam('task_id');
$task = $this->getTask();
$mailbox = $this->login();

if ($mailbox != false) {
if (($mailbox = $this->helper->mailHelper->login()) == false) {
$this->flashFailure();
} else {
$email = $mailbox->getMail(
$mail_id,
false
Expand All @@ -316,10 +294,10 @@ public function convertToComment()

if (!$this->userModel->getByEmail($from_email)) {
$connect_to_user = null;
$comment = '*Email converted to comment and originally sent by ' . $from_email . '*' ."\n\n" . (isset($subject) ? "#$subject\n\n" : '') . (isset($message) ? $message : '');
$comment = '*Email converted to comment and originally sent by ' . $from_email . '*' . "\n\n" . (isset($subject) ? "#$subject\n\n" : '') . (isset($message) ? $message : '');
} else {
$connect_to_user = $this->userModel->getByEmail($from_email);
$comment = '*Email converted to comment by ' . $user['username']. '*' ."\n\n" . (isset($subject) ? "#$subject\n\n" : '') . (isset($message) ? $message : '');
$comment = '*Email converted to comment by ' . $user['username'] . '*' . "\n\n" . (isset($subject) ? "#$subject\n\n" : '') . (isset($message) ? $message : '');
}

$values = array(
Expand All @@ -328,49 +306,16 @@ public function convertToComment()
'user_id' => is_null($connect_to_user) ? $user['id'] : $connect_to_user['id'],
);


$comment_id = $this->commentModel->create($values);


$option = $this->configModel->get('mailmagik_pref', '2');

if ($option == 2) {
$mailbox->markMailAsRead($mail_id);
} else {
$mailbox->deleteMail($mail_id);
}

$task_id = $task_id.'#comment-'.$comment_id;
$task_id = $task_id . '#comment-' . $comment_id;
$this->helper->mailHelper->processMessage($mailbox, $mail_id);

$this->response->redirect($this->helper->url->to('TaskViewController', 'show', array('task_id' => $task_id), false, '', '', $this->request->isAjax(), 'comment-'.$comment_id));
}
}


/**
* Email delete
*
* @access public
*/
private function login()
private function flashFailure()
{
$server = $this->configModel->get('mailmagik_server', '');
$port = $this->configModel->get('mailmagik_port', '');
$user = $this->configModel->get('mailmagik_user', '');
$password = $this->configModel->get('mailmagik_password', '');

if ($server != '' && $port != '' && $user != '' && $password != '') {
$mailbox = new PhpImap\Mailbox(
'{'.$server.':' . $port . '/imap/ssl}INBOX',
$user,
$password,
false
);

return $mailbox;
} else {
$this->flash->failure(t('IMAP Server is missing config settings information!'));
return false;
}
$this->flash->failure(t('IMAP Server connection could not be established!'));
}
}

5 comments on commit eb2623e

@alfredbuehler
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@creecros Could you please review these changes? With your OK, I would rebase the branch to main.

@creecros
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@alfredbuehler ya, as soon as i get a chance. i started playing with a new idea for a plugin, using machine learning to make predictions for new tasks based on previous task history i.e. predict a tasks completion date. kind of silly, probably wont actually do anything with it, at least not any time soon until i can really figure it out.

@creecros
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@alfredbuehler everything seems to function just fine, im not seeing any issues.

unrelated when converting a task email to a task:

2023/02/23 13:49:08 [error] 15#15: *1 FastCGI sent in stderr: "PHP message: PHP Fatal error:  Uncaught PicoDb\SQLException: SQL Error[HY000]: SQLSTATE[HY000]: General error: 5 database is locked in /var/www/app/libs/picodb/lib/PicoDb/StatementHandler.php:353,
  thrown in /var/www/app/libs/picodb/lib/PicoDb/StatementHandler.php on line 353" while reading upstream, client: 172.17.0.1, server: localhost, request: "GET /?controller=FileViewerController&action=thumbnail&file_id=4&task_id=6 HTTP/1.1", upstream: "fastcgi://unix:/var/run/php-fpm.sock:", host: "192.168.1.2:41111"

@alfredbuehler
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't recognize such trouble, but I'll have an eye on it.

@creecros
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ive seen these before, and tbh they are intermitent. could just be the machine i have it running on at the moment and point in time, maybe its a slow day.

Please sign in to comment.