1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
You may not change or alter any portion of this comment or credits of supporting |
4
|
|
|
developers from this source code or any supporting source code which is considered |
5
|
|
|
copyrighted (c) material of the original comment or credit authors. |
6
|
|
|
|
7
|
|
|
This program is distributed in the hope that it will be useful, |
8
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
9
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
use Xmf\Assert; |
13
|
|
|
use Xoops\Core\Kernel\Handlers\XoopsUser; |
|
|
|
|
14
|
|
|
use Xoops\Core\Service\AbstractContract; |
15
|
|
|
use Xoops\Core\Service\Response; |
16
|
|
|
use Xoops\Core\Service\Contract\UserEmailMessageInterface; |
17
|
|
|
use Xoops\Core\Service\Data\Message; |
18
|
|
|
use Xoops\Core\Service\Data\Email; |
19
|
|
|
use Xoops\Core\Service\Data\EmailAddress; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* phpmailer module |
23
|
|
|
* |
24
|
|
|
* @copyright 2018 XOOPS Project (https://xoops.org) |
25
|
|
|
* @license GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html) |
26
|
|
|
* @author Richard Griffith <[email protected]> |
27
|
|
|
* @link https://xoops.org |
28
|
|
|
*/ |
29
|
|
|
class PhpMailerMessageProvider extends AbstractContract implements UserEmailMessageInterface |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* getName - get a short name for this service provider. This should be unique within the |
33
|
|
|
* scope of the named service, so using module dirname is suggested. |
34
|
|
|
* |
35
|
|
|
* @return string - a unique name for the service provider |
36
|
|
|
*/ |
37
|
|
|
public function getName() |
38
|
|
|
{ |
39
|
|
|
return 'phpmailermessage'; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* getDescription - get human readable description of the service provider |
44
|
|
|
* |
45
|
|
|
* @return string |
46
|
|
|
*/ |
47
|
|
|
public function getDescription() |
48
|
|
|
{ |
49
|
|
|
return 'User messages by email using PHPMailer.'; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param \Xoops\Core\Service\Response $response |
54
|
|
|
* @param \Xoops\Core\Service\Data\Message $message |
55
|
|
|
* |
56
|
|
|
* @return void - reports success or failure through $response->success |
57
|
|
|
*/ |
58
|
|
|
public function sendMessage(Response $response, Message $message) |
59
|
|
|
{ |
60
|
|
|
try { |
61
|
|
|
$email = new Email( |
62
|
|
|
$message->getSubject(), |
63
|
|
|
$message->getBody(), |
64
|
|
|
$this->getEmailAddressByUser($message->getFromId()), |
65
|
|
|
$this->getEmailAddressByUser($message->getToId()) |
66
|
|
|
); |
67
|
|
|
} catch (\InvalidArgumentException | \LogicException $e) { |
68
|
|
|
$response->setSuccess(false)->addErrorMessage($e->getMessage()); |
69
|
|
|
return; |
70
|
|
|
} |
71
|
|
|
// Relay to Email Service |
72
|
|
|
$emailResponse = \Xoops::getInstance()->service('email')->sendEmail($email); |
73
|
|
|
if (!$emailResponse->isSuccess()) { |
74
|
|
|
$response->setSuccess(false); |
75
|
|
|
$response->addErrorMessage($emailResponse->getErrorMessage()); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param int $userid |
81
|
|
|
* |
82
|
|
|
* @return \Xoops\Core\Service\Data\EmailAddress |
83
|
|
|
* |
84
|
|
|
* @throws \InvalidArgumentException -- bad userid |
85
|
|
|
*/ |
86
|
|
|
protected function getEmailAddressByUser(int $userid) : EmailAddress |
87
|
|
|
{ |
88
|
|
|
$userHandler = \Xoops::getInstance()->getHandlerMember(); |
89
|
|
|
$user = $userHandler->getUser($userid); |
90
|
|
|
Assert::isInstanceOf($user, XoopsUser::class); |
91
|
|
|
$name = empty($user->name()) ? $user->uname() : $user->name(); |
92
|
|
|
return new EmailAddress($user->email(), $name); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
Let?s assume that you have a directory layout like this:
and let?s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: