Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Mail sender name #338

Merged
merged 3 commits into from
Jun 21, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions lib/Mailer/NotificationMailer.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
use OCP\Template;
use OCA\Notifications\Configuration\OptionsStorage;
use OCP\IURLGenerator;
use OCP\Defaults;
use OCP\Util;

/**
* The class will focus on sending notifications via email. In addition, some email-related
Expand All @@ -44,14 +46,40 @@ class NotificationMailer {

/** @var IURLGenerator */
private $urlGenerator;

/** @var string */
protected $senderAddress;

/** @var string */
protected $senderName;

public function __construct(IManager $manager, IMailer $mailer, OptionsStorage $optionsStorage, IURLGenerator $urlGenerator) {
$this->manager = $manager;
$this->mailer = $mailer;
$this->optionsStorage = $optionsStorage;
$this->urlGenerator = $urlGenerator;
}

/**
* Get the sender data
* @param string $setting Either `email` or `name`
* @return string
*/
protected function getSenderData($setting) {
if (empty($this->senderAddress)) {
$this->senderAddress = Util::getDefaultEmailAddress('no-reply');
}
if (empty($this->senderName)) {
$defaults = new Defaults();
$this->senderName = $defaults->getName();
}

if ($setting === 'email') {
return $this->senderAddress;
}
return $this->senderName;
}

/**
* Send a notification via email to the list of email addresses passed as parameter
* @param INotification $notification the notification to be sent
Expand All @@ -74,6 +102,7 @@ public function sendNotification(INotification $notification, $serverUrl, $email

$emailMessage = $this->mailer->createMessage();
$emailMessage->setTo([$emailAddress]);
$emailMessage->setFrom([$this->getSenderData('email') => $this->getSenderData('name')]);

$notificationLink = $notification->getLink();
$urlComponents = \parse_url($notificationLink);
Expand Down