Skip to content
Snippets Groups Projects
Commit 6b0b6a5a authored by Oleksandr Tymoshchuk's avatar Oleksandr Tymoshchuk Committed by Ivan Doroshenko
Browse files

Resolve #3342280 "Update unione php"

parent d461ca54
No related branches found
No related tags found
1 merge request!9Resolve #3342280 "Update unione php"
......@@ -20,7 +20,7 @@
"license": "GPL-2.0-or-later",
"minimum-stability": "dev",
"require": {
"unione/unione-php": "^v0.0.1",
"unione/unione-php": "^v1.0.0",
"drupal/mailsystem": "*",
"html2text/html2text": "^4.0.1"
}
......
......@@ -3,7 +3,6 @@
namespace Drupal\unione\Plugin\Mail;
use Drupal\Component\Render\MarkupInterface;
use Drupal\Component\Serialization\Json;
use Drupal\Core\Config\ImmutableConfig;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Mail\MailInterface;
......@@ -11,8 +10,7 @@ use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Html2Text\Html2Text;
use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Unione\UniOneClient;
use Unione\UnioneClient;
/**
* Default UniOne mail system plugin.
*
......@@ -38,9 +36,9 @@ class UniOneMail implements MailInterface, ContainerFactoryPluginInterface {
/**
* UniOne client.
*
* @var \Unione\UniOneClient
* @var \Unione\UnioneClient
*/
protected UniOneClient $client;
protected UnioneClient $client;
/**
* Configuration object.
......@@ -84,7 +82,7 @@ class UniOneMail implements MailInterface, ContainerFactoryPluginInterface {
$this->config = $settings;
$this->logger = $logger;
$this->fileStorage = $file_storage;
$this->client = new UniOneClient($this->config->get('endpoint'), $this->config->get('api_key'));
$this->client = new UnioneClient($this->config->get('api_key'), $this->config->get('endpoint'));
$this->isDebug = $this->config->get('debug_mode') ?? FALSE;
}
......@@ -112,20 +110,19 @@ class UniOneMail implements MailInterface, ContainerFactoryPluginInterface {
public function mail(array $message) {
[$body, $headers] = $this->buildMessage($message);
$response = $this->client->send($body, $headers);
$decode_response = Json::decode($response);
$response = $this->client->emails()->send($body, $headers);
if ($decode_response['status'] === 'success') {
if (!empty($response['status']) && $response['status'] === 'success') {
// Debug mode: log all messages.
if ($this->isDebug) {
$this->logger->notice('Successfully queued message from %from to %to.', [
'%from' => $message['from'],
'%to' => implode(', ', $decode_response['emails']),
'%to' => implode(', ', $response['emails']),
]);
}
}
if ($decode_response['failed_emails']) {
if (!empty($response['failed_emails'])) {
$failed_emails =& $decode_response['failed_emails'];
array_walk($failed_emails, static function (&$value, $key) {
$value = "{$key}: {$value}";
......@@ -141,7 +138,7 @@ class UniOneMail implements MailInterface, ContainerFactoryPluginInterface {
]);
}
return $decode_response['status'] === 'success';
return $response['status'] === 'success';
}
/**
......@@ -176,23 +173,23 @@ class UniOneMail implements MailInterface, ContainerFactoryPluginInterface {
'from_name' => $name,
'recipients' => $recipients,
'subject' => $message['subject'],
'body_html' => $message['body'] instanceof MarkupInterface
'body' => ['html' => $message['body'] instanceof MarkupInterface
? $message['body']->__toString()
: $message['body'],
: $message['body']],
];
// Remove HTML version if the message does not support HTML.
if (isset($message['params']['html']) && !$message['params']['html']) {
unset($unione_message['body_html']);
unset($unione_message['body']['html']);
}
// Set text version of the message.
if (isset($message['plain'])) {
$unione_message['body_plaintext'] = $message['plain'];
$unione_message['body']['plaintext'] = $message['plain'];
}
else {
$converter = new Html2Text($message['body'], ['width' => 0]);
$unione_message['body_plaintext'] = $converter->getText();
$unione_message['body']['plaintext'] = $converter->getText();
}
// Add Reply-To as header according to UniOne API.
......@@ -272,7 +269,7 @@ class UniOneMail implements MailInterface, ContainerFactoryPluginInterface {
*/
public function checkEmail(string $email): ?array {
if (!str_contains($email, '<')) {
return [$email];
return [trim($email)];
}
if (!preg_match(self::FROM_STRING_PATTERN, $email, $matches)) {
......@@ -287,7 +284,7 @@ class UniOneMail implements MailInterface, ContainerFactoryPluginInterface {
return NULL;
}
return [$matches['addrSpec'], $matches['displayName']];
return [trim($matches['addrSpec']), trim($matches['displayName'])];
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment