Skip to content

Commit

Permalink
fix send message bug
Browse files Browse the repository at this point in the history
  • Loading branch information
hafael committed Aug 29, 2023
1 parent 3fbd58d commit 4f36690
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 10 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ Add entry to [root-of-laravel]/config/mail.php:
'transport' => 'azure',
'endpoint' => env('AZURE_MAIL_ENDPOINT', 'https://{communicatonServiceName}.communication.azure.com'),
'access_key' => env('AZURE_MAIL_KEY'),
'api_version' => env('AZURE_MAIL_API_VERSION', '2023-03-31'), //optional
'disable_user_tracking' => env('AZURE_MAIL_DISABLE_TRACKING', false), //optional
'api_version' => env('AZURE_MAIL_API_VERSION', '2023-03-31'),
'disable_user_tracking' => env('AZURE_MAIL_DISABLE_TRACKING', false),
],
]

Expand All @@ -79,8 +79,8 @@ Add entry to [root-of-laravel]/.env:
# Azure Service entries
AZURE_MAIL_ENDPOINT="https://{communicatonServiceName}.communication.azure.com"
AZURE_MAIL_KEY="{base64accessToken}"
AZURE_MAIL_API_VERSION="2021-10-01-preview" #optional
AZURE_MAIL_DISABLE_TRACKING=false #optional
# AZURE_MAIL_API_VERSION=2023-03-31 #optional
# AZURE_MAIL_DISABLE_TRACKING=false #optional
```

Expand Down
22 changes: 20 additions & 2 deletions src/AzureMailerApiTransport.php
Original file line number Diff line number Diff line change
Expand Up @@ -322,12 +322,14 @@ private function getSignedHeaders(array $payload, Email $message): array
$signature = $this->getAuthenticationSignature($stringToSign);

//get GUID part of message id to identify the long running operation
$messageId = explode('@', $message->generateMessageId(), 2)[0];
//$messageId = explode('@', $message->generateMessageId(), 2)[0];

$messageId = $this->generateMessageId();

$headers = [
'Content-Type' => 'application/json',
'repeatability-request-id' => $messageId,
'operation-id' => $messageId,
'Operation-Id' => $messageId,
'repeatability-first-sent' => $utcNow,
'x-ms-date' => $utcNow,
'x-ms-content-sha256' => $contentHash,
Expand All @@ -338,6 +340,22 @@ private function getSignedHeaders(array $payload, Email $message): array
//return signed headers to http client request
return $headers;
}

/**
* Identify the long running operation.
*
* @return array
*/
private function generateMessageId(): string
{
$data = random_bytes(16);
assert(strlen($data) == 16);
$data[6] = chr(ord($data[6]) & 0x0f | 0x40);
$data[8] = chr(ord($data[8]) & 0x3f | 0x80);

// Output the 36 character UUID.
return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4));
}

/**
* Custom email headers to be passed.
Expand Down
8 changes: 4 additions & 4 deletions src/AzureMailerServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ protected function registerIlluminateMailer()
return (new AzureMailerTransportFactory)->create(
new Dsn(
'azure+api',
config('services.azure.endpoint'),
config('services.azure.access_key'),
config('services.azure.api_version'),
config('services.azure.disable_user_tracking')
config('mail.mailers.azure.endpoint'),
config('mail.mailers.azure.access_key'),
config('mail.mailers.azure.api_version'),
config('mail.mailers.azure.disable_user_tracking')
)
);
});
Expand Down

0 comments on commit 4f36690

Please sign in to comment.