Skip to content

Commit

Permalink
Added attachment validation.
Browse files Browse the repository at this point in the history
  • Loading branch information
tomshaw committed Apr 1, 2024
1 parent 8df7c3d commit b608019
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions src/Api/GoogleMail.php
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,10 @@ public function attachments(array $paths): self
*/
public function send(): Message
{
if (count($this->attachments)) {
$this->validateAttachments($this->attachments);
}

$validated = $this->validateMessage();

$message = $this->buildMessage($validated);
Expand Down Expand Up @@ -384,6 +388,34 @@ protected function buildMessage(array $validated): string
return $headers;
}

/**
* Validates the email attachments.
*
* The maximum total size is set to 25MB, which is the limit for attachments sent through Gmail.
*
* @param array $attachments The attachments to validate.
*
* @throws \Exception If the attachments are invalid.
*/
protected function validateAttachments(array $attachments): void
{
$totalSize = 0;
$maxTotalSize = 25 * 1024 * 1024; // 25 MB

foreach ($attachments as $attachment) {
if (! file_exists($attachment)) {
throw new \Exception("File $attachment does not exist");
}
if (! is_readable($attachment)) {
throw new \Exception("File $attachment is not readable");
}
$totalSize += filesize($attachment);
}
if ($totalSize > $maxTotalSize) {
throw new \Exception('Total size of attachments exceeds the maximum size limit');
}
}

/**
* Encodes a message into a URL-safe format.
*
Expand Down

0 comments on commit b608019

Please sign in to comment.