Skip to content

Commit

Permalink
Merge pull request #52 from nextcloud/server-1716-activity-sending-ti…
Browse files Browse the repository at this point in the history
…meout

Catch errors when one email could not be sent
  • Loading branch information
MorrisJobke authored Oct 13, 2016
2 parents f8e28bf + b5fdbf2 commit 9986193
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
10 changes: 8 additions & 2 deletions lib/BackgroundJob/EmailNotification.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ protected function runStep($limit, $sendTime) {
// Send Email
$default_lang = $this->config->getSystemValue('default_language', 'en');
$defaultTimeZone = date_default_timezone_get();

$deleteItemsForUsers = [];
foreach ($affectedUsers as $user) {
if (empty($userEmails[$user])) {
// The user did not setup an email address
Expand All @@ -118,11 +120,15 @@ protected function runStep($limit, $sendTime) {

$language = (!empty($userLanguages[$user])) ? $userLanguages[$user] : $default_lang;
$timezone = (!empty($userTimezones[$user])) ? $userTimezones[$user] : $defaultTimeZone;
$this->mqHandler->sendEmailToUser($user, $userEmails[$user], $language, $timezone, $sendTime);
if ($this->mqHandler->sendEmailToUser($user, $userEmails[$user], $language, $timezone, $sendTime)) {
$deleteItemsForUsers[] = $user;
} else {
$this->logger->debug("Failed sending activity mail to user '" . $user . "'.", ['app' => 'activity']);
}
}

// Delete all entries we dealt with
$this->mqHandler->deleteSentItems($affectedUsers, $sendTime);
$this->mqHandler->deleteSentItems($deleteItemsForUsers, $sendTime);

return sizeof($affectedUsers);
}
Expand Down
11 changes: 9 additions & 2 deletions lib/MailQueueHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -218,11 +218,12 @@ protected function getSenderData($setting) {
* @param string $lang Selected language of the recipient
* @param string $timezone Selected timezone of the recipient
* @param int $maxTime
* @return bool True if the entries should be removed, false otherwise
*/
public function sendEmailToUser($userName, $email, $lang, $timezone, $maxTime) {
$user = $this->userManager->get($userName);
if (!$user instanceof IUser) {
return;
return true;
}

list($mailData, $skippedCount) = $this->getItemsForUser($userName, $maxTime);
Expand Down Expand Up @@ -270,9 +271,15 @@ public function sendEmailToUser($userName, $email, $lang, $timezone, $maxTime) {
$message->setSubject((string) $l->t('Activity notification'));
$message->setPlainBody($emailText);
$message->setFrom([$this->getSenderData('email') => $this->getSenderData('name')]);
$this->mailer->send($message);

try {
$this->mailer->send($message);
} catch (\Exception $e) {
return false;
}

$this->activityManager->setCurrentUserId(null);
return true;
}

/**
Expand Down

0 comments on commit 9986193

Please sign in to comment.