Skip to content

Commit

Permalink
Fix: sending email from special accounts
Browse files Browse the repository at this point in the history
  • Loading branch information
Greg Roach committed Sep 21, 2019
1 parent 2c3dad1 commit 502cab9
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 20 deletions.
11 changes: 8 additions & 3 deletions app/Services/MailService.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
use Swift_SmtpTransport;
use Swift_Transport;
use Throwable;
use function app;
use function filter_var;
use function function_exists;
use function gethostbyaddr;
Expand Down Expand Up @@ -66,14 +67,18 @@ public function send(UserInterface $from, UserInterface $to, UserInterface $repl
$message_text = str_replace("\n", "\r\n", $message_text);
$message_html = str_replace("\n", "\r\n", $message_html);

// Special accounts do not have an email address. Use the system one.
$from_email = $from->email() ?: $this->senderEmail();
$reply_to_email = $reply_to->email() ?: $this->senderEmail();

$message = (new Swift_Message())
->setSubject($subject)
->setFrom($from->email(), $from->realName())
->setFrom($from_email, $from->realName())
->setTo($to->email(), $to->realName())
->setBody($message_html, 'text/html');

if ($from->email() !== $reply_to->email()) {
$message->setReplyTo($reply_to->email(), $reply_to->realName());
if ($from_email !== $reply_to_email) {
$message->setReplyTo($reply_to_email, $reply_to->realName());
}

$dkim_domain = Site::getPreference('DKIM_DOMAIN');
Expand Down
3 changes: 2 additions & 1 deletion app/SiteUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
namespace Fisharebest\Webtrees;

use Fisharebest\Webtrees\Contracts\UserInterface;
use Fisharebest\Webtrees\Services\MailService;

/**
* The site can act as a user, for example to send email.
Expand All @@ -44,7 +45,7 @@ public function id(): int
*/
public function email(): string
{
return Site::getPreference('SMTP_FROM_NAME', 'no-reply@localhost');
return '';
}

/**
Expand Down
36 changes: 25 additions & 11 deletions app/TreeUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
namespace Fisharebest\Webtrees;

use Fisharebest\Webtrees\Contracts\UserInterface;
use Fisharebest\Webtrees\Services\UserService;

/**
* A tree can act as a user, for example to send email.
Expand Down Expand Up @@ -56,38 +57,51 @@ public function id(): int
*/
public function email(): string
{
return Site::getPreference('SMTP_FROM_NAME', 'no-reply@localhost');
$user_service = app(UserService::class);
$contact_id = (int) $this->getPreference('CONTACT_USER_ID');

if ($contact_id === 0) {
return '';
}

$contact = $user_service->find($contact_id);

if ($contact instanceof User) {
return $contact->email();
}

return '';
}

/**
* The user‘s real name.
* @param string $setting_name
* @param string $default
*
* @return string
*/
public function realName(): string
public function getPreference(string $setting_name, string $default = ''): string
{
return $this->tree->title();
return $default;
}

/**
* The user‘s login name.
* The user‘s real name.
*
* @return string
*/
public function userName(): string
public function realName(): string
{
return '';
return $this->tree->title();
}

/**
* @param string $setting_name
* @param string $default
* The user‘s login name.
*
* @return string
*/
public function getPreference(string $setting_name, string $default = ''): string
public function userName(): string
{
return $default;
return '';
}

/**
Expand Down
5 changes: 1 addition & 4 deletions tests/app/SiteUserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,9 @@ public function testConstructor(): void

$this->assertInstanceOf(UserInterface::class, $user);
$this->assertSame(0, $user->id());
$this->assertSame('no-reply@localhost', $user->email());
$this->assertSame('', $user->email());
$this->assertSame('webtrees', $user->realName());
$this->assertSame('', $user->userName());

Site::setPreference('SMTP_FROM_NAME', 'foo@example.com');
$this->assertSame('foo@example.com', $user->email());
}

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/app/TreeUserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function testConstructor(): void

$this->assertInstanceOf(UserInterface::class, $user);
$this->assertSame(0, $user->id());
$this->assertSame('no-reply@localhost', $user->email());
$this->assertSame('', $user->email());
$this->assertSame('title', $user->realName());
$this->assertSame('', $user->userName());
}
Expand Down

0 comments on commit 502cab9

Please sign in to comment.