Skip to content
This repository has been archived by the owner on Apr 5, 2020. It is now read-only.

Commit

Permalink
Add ability to override sender E-Mail
Browse files Browse the repository at this point in the history
  • Loading branch information
dmorlitz authored and fguillot committed Oct 16, 2018
1 parent fd0952f commit bae2d14
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 10 deletions.
22 changes: 14 additions & 8 deletions EmailHandler.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?php

namespace Kanboard\Plugin\Mailgun;

require_once __DIR__.'/vendor/autoload.php';
Expand Down Expand Up @@ -95,19 +94,26 @@ public function validate(array $payload)
return false;
}

// The project must have a short name
$project = $this->projectModel->getByEmail($payload['recipient']);

if (empty($project)) {
$this->logger->info('Mailgun: ignored => project not found');
return false;
}

// The user must exists in Kanboard
$user = $this->userModel->getByEmail($payload['sender']);

// Check to see if a catchall user was specified - if the original sender is unrecognized
if (empty($user)) {
$this->logger->info('Mailgun: ignored => user not found');
return false;
$catchAllAddress = $this->projectMetadataModel->get($project['id'], 'mailgun_catch_all');
$user = $this->userModel->getByEmail($catchAllAddress);
$this->logger->info('Mailgun: unknown user mapped to ' . $user['name'] . ' (' . $user['email'] . ') in project ' . $project['name']);
}

// The project must have a short name
$project = $this->projectModel->getByEmail($payload['recipient']);

if (empty($project)) {
$this->logger->info('Mailgun: ignored => project not found');
if (empty($user)) {
$this->logger->info('Mailgun: ignored => user not found');
return false;
}

Expand Down
2 changes: 2 additions & 0 deletions Locale/fr_FR/translations.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,7 @@

return array(
'Help on Mailgun integration' => 'Aide sur l\'intégration avec Mailgun',
'Catch-All Email Address' => 'Adresse e-mail « attrape-tout » (catch-all)',
'Blank value will cause unknown senders to be ignored' => 'Une valeur vide ignore les expéditeurs inconnus'
);

5 changes: 3 additions & 2 deletions Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ class Plugin extends Base
public function initialize()
{
$this->emailClient->setTransport('mailgun', '\Kanboard\Plugin\Mailgun\EmailHandler');
$this->template->hook->attach('template:project:integrations', 'mailgun:project/integration');
$this->template->hook->attach('template:config:integrations', 'mailgun:config/integration');
$this->route->addRoute('/mailgun/handler/:token', 'WebhookController', 'receiver', 'mailgun');
$this->applicationAccessMap->add('WebhookController', 'receiver', Role::APP_PUBLIC);
$this->route->addRoute('/mailgun/handler/:token', 'WebhookController', 'receiver', 'mailgun');
}

public function onStartup()
Expand All @@ -39,7 +40,7 @@ public function getPluginAuthor()

public function getPluginVersion()
{
return '1.0.10';
return '1.0.11';
}

public function getPluginHomepage()
Expand Down
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,14 @@ Notes
Changes
-------

### Version 1.0.11

- For unknown senders you can now accept them into a project by specifying a user to accept the task - this is configurable on a project level - leaving the setting blank will cause unknown senders to be ignored.

### Version 1.0.10

- Update help link URL

### Version 1.0.9

- Tasks created by incoming email are assigned to the recipient
Expand Down
14 changes: 14 additions & 0 deletions Template/project/integration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<h3><img src="<?= $this->url->dir() ?>plugins/Mailgun/mailgun-icon.png"/>&nbsp;Mailgun</h3>
<div class="panel">
<?= $this->form->label(t('Catch-All Email Address'), 'mailgun_catch_all') ?>
<?= $this->form->email('mailgun_catch_all', $values) ?>

<p class="form-help">
<?= t('Blank value will cause unknown senders to be ignored') ?> -
<a href="https://github.com/kanboard/plugin-mailgun#installation" target="_blank"><?= t('Help on Mailgun integration') ?></a>
</p>

<div class="form-actions">
<input type="submit" value="<?= t('Save') ?>" class="btn btn-blue">
</div>
</div>
33 changes: 33 additions & 0 deletions Test/EmailHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Kanboard\Plugin\Mailgun\EmailHandler;
use Kanboard\Model\TaskFinderModel;
use Kanboard\Model\ProjectModel;
use Kanboard\Model\ProjectMetadataModel;
use Kanboard\Model\ProjectUserRoleModel;
use Kanboard\Model\UserModel;
use Kanboard\Core\Security\Role;
Expand Down Expand Up @@ -116,6 +117,38 @@ public function testHandlePayload()
$this->assertEquals(2, $task['creator_id']);
}

public function testHandlePayloadFromAnyone()
{
$emailHandler = new EmailHandler($this->container);
$projectModel = new ProjectModel($this->container);
$projectUserRoleModel = new ProjectUserRoleModel($this->container);
$userModel = new UserModel($this->container);
$taskFinderModel = new TaskFinderModel($this->container);
$projectMetadataModel = new ProjectMetadataModel($this->container);

$this->assertEquals(2, $userModel->create(array('username' => 'anyone', 'email' => 'anyone@localhost')));

$this->assertEquals(1, $projectModel->create(array('name' => 'test1')));
$this->assertEquals(2, $projectModel->create(array('name' => 'test2', 'email' => 'test2@localhost')));

// Allow project 2 to receive E-Mail from any sender
$this->assertTrue($projectMetadataModel->save(2, array('mailgun_catch_all' => 'anyone@localhost')));

// Message is from a user not in a project - and should be mapped to the project user
$this->assertFalse($emailHandler->receiveEmail(array('sender' => 'me@localhost', 'subject' => 'Email task', 'recipient' => 'test2@localhost', 'stripped-text' => 'boo')));
$this->assertTrue($projectUserRoleModel->addUser(2, 2, Role::PROJECT_MEMBER));

// The task must be created
$this->assertTrue($emailHandler->receiveEmail(array('sender' => '[email protected]', 'subject' => 'Email task', 'recipient' => 'test2@localhost', 'stripped-html' => '<strong>boo</strong>')));

$task = $taskFinderModel->getById(1);
$this->assertNotEmpty($task);
$this->assertEquals(2, $task['project_id']);
$this->assertEquals('Email task', $task['title']);
$this->assertEquals('**boo**', $task['description']);
$this->assertEquals(2, $task['creator_id']);
}

public function testGetSubject()
{
$handler = new EmailHandler($this->container);
Expand Down

0 comments on commit bae2d14

Please sign in to comment.