diff --git a/lib/private/Collaboration/Collaborators/MailPlugin.php b/lib/private/Collaboration/Collaborators/MailPlugin.php
index d28bd3692a4db8bd676add5fbc491e26e055c082..2e946c4a872b6f454092161b9a63462e30a7b7c6 100644
--- a/lib/private/Collaboration/Collaborators/MailPlugin.php
+++ b/lib/private/Collaboration/Collaborators/MailPlugin.php
@@ -30,10 +30,13 @@ use OCP\Collaboration\Collaborators\SearchResultType;
 use OCP\Contacts\IManager;
 use OCP\Federation\ICloudIdManager;
 use OCP\IConfig;
+use OCP\IGroupManager;
+use OCP\IUserSession;
 use OCP\Share;
 
 class MailPlugin implements ISearchPlugin {
 	protected $shareeEnumeration;
+	protected $shareWithGroupOnly;
 
 	/** @var IManager */
 	private $contactsManager;
@@ -42,12 +45,21 @@ class MailPlugin implements ISearchPlugin {
 	/** @var IConfig */
 	private $config;
 
-	public function __construct(IManager $contactsManager, ICloudIdManager $cloudIdManager, IConfig $config) {
+	/** @var IGroupManager */
+	private $groupManager;
+
+	/** @var IUserSession */
+	private $userSession;
+
+	public function __construct(IManager $contactsManager, ICloudIdManager $cloudIdManager, IConfig $config, IGroupManager $groupManager, IUserSession $userSession) {
 		$this->contactsManager = $contactsManager;
 		$this->cloudIdManager = $cloudIdManager;
 		$this->config = $config;
+		$this->groupManager = $groupManager;
+		$this->userSession = $userSession;
 
 		$this->shareeEnumeration = $this->config->getAppValue('core', 'shareapi_allow_share_dialog_user_enumeration', 'yes') === 'yes';
+		$this->shareWithGroupOnly = $this->config->getAppValue('core', 'shareapi_only_share_with_group_members', 'no') === 'yes';
 	}
 
 	/**
@@ -77,6 +89,22 @@ class MailPlugin implements ISearchPlugin {
 					$exactEmailMatch = strtolower($emailAddress) === $lowerSearch;
 
 					if (isset($contact['isLocalSystemBook'])) {
+						if ($this->shareWithGroupOnly) {
+							/*
+							 * Check if the user may share with the user associated with the e-mail of the just found contact
+							 */
+							$userGroups = $this->groupManager->getUserGroupIds($this->userSession->getUser());
+							$found = false;
+							foreach ($userGroups as $userGroup) {
+								if ($this->groupManager->isInGroup($contact['UID'], $userGroup)) {
+									$found = true;
+									break;
+								}
+							}
+							if (!$found) {
+								continue;
+							}
+						}
 						if ($exactEmailMatch) {
 							try {
 								$cloud = $this->cloudIdManager->resolveCloudId($contact['CLOUD'][0]);
diff --git a/tests/lib/Collaboration/Collaborators/MailPluginTest.php b/tests/lib/Collaboration/Collaborators/MailPluginTest.php
index 9c9d9cff9091839c2de556aba6164db9e0062033..b728ae521e2e6851c65ef3015931006ba5cc6757 100644
--- a/tests/lib/Collaboration/Collaborators/MailPluginTest.php
+++ b/tests/lib/Collaboration/Collaborators/MailPluginTest.php
@@ -31,6 +31,8 @@ use OCP\Collaboration\Collaborators\SearchResultType;
 use OCP\Contacts\IManager;
 use OCP\Federation\ICloudIdManager;
 use OCP\IConfig;
+use OCP\IGroupManager;
+use OCP\IUserSession;
 use OCP\Share;
 use Test\TestCase;
 
@@ -50,17 +52,25 @@ class MailPluginTest extends TestCase {
 	/** @var  SearchResult */
 	protected $searchResult;
 
+	/** @var  IGroupManager|\PHPUnit_Framework_MockObject_MockObject */
+	protected $groupManager;
+
+	/** @var  IUserSession|\PHPUnit_Framework_MockObject_MockObject */
+	protected $userSession;
+
 	public function setUp() {
 		parent::setUp();
 
 		$this->config = $this->createMock(IConfig::class);
 		$this->contactsManager = $this->createMock(IManager::class);
+		$this->groupManager = $this->createMock(IGroupManager::class);
+		$this->userSession = $this->createMock(IUserSession::class);
 		$this->cloudIdManager = new CloudIdManager();
 		$this->searchResult = new SearchResult();
 	}
 
 	public function instantiatePlugin() {
-		$this->plugin = new MailPlugin($this->contactsManager, $this->cloudIdManager, $this->config);
+		$this->plugin = new MailPlugin($this->contactsManager, $this->cloudIdManager, $this->config, $this->groupManager, $this->userSession);
 	}
 
 	/**
@@ -333,4 +343,131 @@ class MailPluginTest extends TestCase {
 			]
 		];
 	}
+
+	/**
+	 * @dataProvider dataGetEmailGroupsOnly
+	 *
+	 * @param string $searchTerm
+	 * @param array $contacts
+	 * @param array $expected
+	 * @param bool $exactIdMatch
+	 * @param bool $reachedEnd
+	 * @param array groups
+	 */
+	public function testSearchGroupsOnly($searchTerm, $contacts, $expected, $exactIdMatch, $reachedEnd, $userToGroupMapping) {
+		$this->config->expects($this->any())
+			->method('getAppValue')
+			->willReturnCallback(
+				function($appName, $key, $default) {
+					if ($appName === 'core' && $key === 'shareapi_allow_share_dialog_user_enumeration') {
+						return 'yes';
+					} else if ($appName === 'core' && $key === 'shareapi_only_share_with_group_members') {
+						return 'yes';
+					}
+					return $default;
+				}
+			);
+
+		$this->instantiatePlugin();
+
+		/** @var \OCP\IUser | \PHPUnit_Framework_MockObject_MockObject */
+		$currentUser = $this->createMock('\OCP\IUser');
+
+		$currentUser->expects($this->any())
+			->method('getUID')
+			->willReturn('currentUser');
+
+		$this->contactsManager->expects($this->any())
+			->method('search')
+			->with($searchTerm, ['EMAIL', 'FN'])
+			->willReturn($contacts);
+
+		$this->userSession->expects($this->any())
+			->method('getUser')
+			->willReturn($currentUser);
+
+		$this->groupManager->expects($this->any())
+			->method('getUserGroupIds')
+			->willReturnCallback(function(\OCP\IUser $user) use ($userToGroupMapping) {
+				return $userToGroupMapping[$user->getUID()];
+			});
+
+		$this->groupManager->expects($this->any())
+			->method('isInGroup')
+			->willReturnCallback(function($userId, $group) use ($userToGroupMapping) {
+				return in_array($group, $userToGroupMapping[$userId]);
+			});
+
+		$moreResults = $this->plugin->search($searchTerm, 0, 0, $this->searchResult);
+		$result = $this->searchResult->asArray();
+
+		$this->assertSame($exactIdMatch, $this->searchResult->hasExactIdMatch(new SearchResultType('emails')));
+		$this->assertEquals($expected, $result);
+		$this->assertSame($reachedEnd, $moreResults);
+	}
+
+	public function dataGetEmailGroupsOnly() {
+		return [
+			// The user `User` can share with the current user
+			[
+				'test',
+				[
+					[
+						'FN' => 'User',
+						'EMAIL' => ['test@example.com'],
+						'CLOUD' => ['test@localhost'],
+						'isLocalSystemBook' => true,
+						'UID' => 'User'
+					]
+				],
+				['users' => [['label' => 'User (test@example.com)','value' => ['shareType' => 0, 'shareWith' => 'test'],]], 'emails' => [], 'exact' => ['emails' => [], 'users' => []]],
+				false,
+				true,
+				[
+					"currentUser" => ["group1"],
+					"User" => ["group1"]
+				]
+			],
+			// The user `User` cannot share with the current user
+			[
+			'test',
+				[
+					[
+						'FN' => 'User',
+						'EMAIL' => ['test@example.com'],
+						'CLOUD' => ['test@localhost'],
+						'isLocalSystemBook' => true,
+						'UID' => 'User'
+					]
+				],
+				['emails'=> [], 'exact' => ['emails' => []]],
+				false,
+				true,
+				[
+					"currentUser" => ["group1"],
+					"User" => ["group2"]
+				]
+			],
+			// The user `User` cannot share with the current user, but there is an exact match on the e-mail address -> share by e-mail
+			[
+			'test@example.com',
+				[
+					[
+						'FN' => 'User',
+						'EMAIL' => ['test@example.com'],
+						'CLOUD' => ['test@localhost'],
+						'isLocalSystemBook' => true,
+						'UID' => 'User'
+					]
+				],
+				['emails' => [], 'exact' => ['emails' => [['label' => 'test@example.com', 'value' => ['shareType' => 4,'shareWith' => 'test@example.com']]]]],
+				false,
+				true,
+				[
+					"currentUser" => ["group1"],
+					"User" => ["group2"]
+				]
+			]
+		];
+	}
 }