feat: message send
Signed-off-by: Sebastian Krupinski <krupinski01@gmail.com>
This commit is contained in:
@@ -908,99 +908,106 @@ class RemoteMailService {
|
||||
}
|
||||
|
||||
/**
|
||||
* send entity
|
||||
*
|
||||
* @since Release 1.0.0
|
||||
* Submit a fresh message by creating it as a draft and then creating a submission.
|
||||
*
|
||||
* @since Release 2.0.0
|
||||
*/
|
||||
public function entitySend(string $identity, MailMessageObject $message, ?string $presendLocation = null, ?string $postsendLocation = null): string {
|
||||
// determine if pre-send location is present
|
||||
if ($presendLocation === null || empty($presendLocation)) {
|
||||
throw new Exception('Pre-Send Location is missing', 1);
|
||||
}
|
||||
// determine if post-send location is present
|
||||
if ($postsendLocation === null || empty($postsendLocation)) {
|
||||
throw new Exception('Post-Send Location is missing', 1);
|
||||
}
|
||||
// determine if we have the basic required data and fail otherwise
|
||||
if (empty($message->getFrom())) {
|
||||
throw new Exception('Missing Requirements: Message MUST have a From address', 1);
|
||||
}
|
||||
if (empty($message->getTo())) {
|
||||
throw new Exception('Missing Requirements: Message MUST have a To address(es)', 1);
|
||||
}
|
||||
// determine if message has attachments
|
||||
if (count($message->getAttachments()) > 0) {
|
||||
// process attachments first
|
||||
$message = $this->depositAttachmentsFromMessage($message);
|
||||
}
|
||||
// convert from address object to string
|
||||
$from = $message->getFrom()->getAddress();
|
||||
// convert to, cc and bcc address object arrays to single strings array
|
||||
$to = array_map(
|
||||
function ($entry) { return $entry->getAddress(); },
|
||||
array_merge($message->getTo(), $message->getCc(), $message->getBcc())
|
||||
);
|
||||
unset($cc, $bcc);
|
||||
// construct set request
|
||||
public function entitySubmitFresh(string $identityId, array $message, string $preSendTarget, string $postSentTarget): string {
|
||||
|
||||
$mail = new MailParametersRequest();
|
||||
$mail->parametersRaw($message);
|
||||
|
||||
$messageCreateId = 'm_' . uniqid();
|
||||
$submissionCreateId = 's_' . uniqid();
|
||||
|
||||
$r0 = new MailSet($this->dataAccount, null, $this->resourceNamespace, $this->resourceEntityLabel);
|
||||
$r0->create('1', $message)->in($presendLocation);
|
||||
// construct set request
|
||||
$createdMessage = $r0->create($messageCreateId, $mail);
|
||||
$createdMessage->in($preSendTarget);
|
||||
$r1 = new MailSubmissionSet($this->dataAccount, null, $this->resourceNamespace, $this->resourceEntityLabel);
|
||||
// construct envelope
|
||||
$e1 = $r1->create('2');
|
||||
$e1->identity($identity);
|
||||
$e1->message('#1');
|
||||
$e1->from($from);
|
||||
$e1->to($to);
|
||||
// transceive
|
||||
$submission = $r1->create($submissionCreateId);
|
||||
$submission->identity($identityId);
|
||||
$submission->message('#' . $messageCreateId);
|
||||
|
||||
$bundle = $this->dataStore->perform([$r0, $r1]);
|
||||
// extract response
|
||||
$response = $bundle->response(1);
|
||||
// return collection information
|
||||
return (string)$response->created()['2']['id'];
|
||||
if ($response instanceof ResponseException) {
|
||||
if ($response->type() === 'unknownMethod') {
|
||||
throw new JmapUnknownMethod($response->description(), 1);
|
||||
}
|
||||
throw new Exception($response->type() . ': ' . $response->description(), 1);
|
||||
}
|
||||
|
||||
return $this->extractSubmissionIdentifier($response, $submissionCreateId);
|
||||
}
|
||||
|
||||
/**
|
||||
* retrieve collection entity attachment from remote storage
|
||||
*
|
||||
* @since Release 1.0.0
|
||||
* Submit an existing draft message.
|
||||
*
|
||||
* @since Release 2.0.0
|
||||
*/
|
||||
public function depositAttachmentsFromMessage(MailMessageObject $message): MailMessageObject {
|
||||
public function entitySubmitDraft(string $identityId, string $draftId, ?array $patch = null): string {
|
||||
if ($draftId === '') {
|
||||
throw new Exception('Draft identifier is required for draft submission', 1);
|
||||
}
|
||||
|
||||
$parameters = $message->toJmap();
|
||||
$attachments = $message->getAttachments();
|
||||
$matches = [];
|
||||
$requests = [];
|
||||
|
||||
$this->findAttachmentParts($parameters['bodyStructure'], $matches);
|
||||
if ($patch !== null && $patch !== []) {
|
||||
$draftMutation = new MailParametersRequest();
|
||||
$draftMutation->parametersRaw($patch);
|
||||
$r0 = new MailSet($this->dataAccount, null, $this->resourceNamespace, $this->resourceEntityLabel);
|
||||
$r0->update($draftId, $draftMutation);
|
||||
$requests[] = $r0;
|
||||
}
|
||||
|
||||
foreach ($attachments as $attachment) {
|
||||
$part = $attachment->toJmap();
|
||||
if (isset($matches[$part->getId()])) {
|
||||
// deposit attachment in data store
|
||||
$response = $this->blobDeposit($account, $part->getType(), $attachment->getContents());
|
||||
// transfer blobId and size to mail part
|
||||
$matches[$part->getId()]->blobId = $response['blobId'];
|
||||
$matches[$part->getId()]->size = $response['size'];
|
||||
unset($matches[$part->getId()]->partId);
|
||||
$submissionCreateId = 's_' . uniqid();
|
||||
$r1 = new MailSubmissionSet($this->dataAccount, null, $this->resourceNamespace, $this->resourceEntityLabel);
|
||||
$submission = $r1->create($submissionCreateId);
|
||||
$submission->identity($identityId);
|
||||
$submission->message($draftId);
|
||||
$requests[] = $r1;
|
||||
|
||||
$bundle = $this->dataStore->perform($requests);
|
||||
$responseIndex = count($requests) - 1;
|
||||
$response = $bundle->response($responseIndex);
|
||||
if ($response instanceof ResponseException) {
|
||||
if ($response->type() === 'unknownMethod') {
|
||||
throw new JmapUnknownMethod($response->description(), 1);
|
||||
}
|
||||
throw new Exception($response->type() . ': ' . $response->description(), 1);
|
||||
}
|
||||
|
||||
return $this->extractSubmissionIdentifier($response, $submissionCreateId);
|
||||
}
|
||||
|
||||
private function collectionByRole(string $role): ?string {
|
||||
$filter = $this->collectionListFilter();
|
||||
$filter->condition('role', $role);
|
||||
|
||||
$collections = $this->collectionList(null, $filter, null);
|
||||
if ($collections === []) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return (string)array_key_first($collections);
|
||||
}
|
||||
|
||||
private function extractSubmissionIdentifier(object $response, string $createId): string {
|
||||
if (method_exists($response, 'createSuccess')) {
|
||||
$success = $response->createSuccess($createId);
|
||||
if (is_array($success) && isset($success['id'])) {
|
||||
return (string)$success['id'];
|
||||
}
|
||||
}
|
||||
|
||||
return (new MailMessageObject())->fromJmap($parameters);
|
||||
|
||||
}
|
||||
|
||||
protected function findAttachmentParts(object &$part, array &$matches) {
|
||||
|
||||
if ($part->disposition === 'attachment' || $part->disposition === 'inline') {
|
||||
$matches[$part->partId] = $part;
|
||||
}
|
||||
|
||||
foreach ($part->subParts as $entry) {
|
||||
$this->findAttachmentParts($entry, $matches);
|
||||
if (method_exists($response, 'created')) {
|
||||
$created = $response->created();
|
||||
if (is_array($created) && isset($created[$createId]['id'])) {
|
||||
return (string)$created[$createId]['id'];
|
||||
}
|
||||
}
|
||||
|
||||
throw new Exception('Submission succeeded without a returned identifier', 1);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user