From f9891cdf67f6db6b374edbc4bbc105d4a41d365c Mon Sep 17 00:00:00 2001 From: Mikel Madariaga Date: Thu, 26 May 2022 12:54:44 +0200 Subject: [PATCH] allow multiple email attachments --- Domain/Model/Mailer/Attachment.php | 37 ++++++++++-- Domain/Model/Mailer/Message.php | 97 ++++++++++++++---------------- 2 files changed, 75 insertions(+), 59 deletions(-) diff --git a/Domain/Model/Mailer/Attachment.php b/Domain/Model/Mailer/Attachment.php index c0802ba..d303b64 100644 --- a/Domain/Model/Mailer/Attachment.php +++ b/Domain/Model/Mailer/Attachment.php @@ -2,26 +2,51 @@ namespace Ivoz\Core\Domain\Model\Mailer; +use Ivoz\Core\Domain\Assert\Assertion; + class Attachment { + public const TYPE_FILEPATH = 'file'; + public const TYPE_CONTENT = 'content'; + + public const ALLOWED_TYPES = [ + self::TYPE_FILEPATH, + self::TYPE_CONTENT, + ]; + + private string $type; + public function __construct( - private string $file, - private string $filename, - private string $mimetype + private string $filePath, + private ?string $filename = null, + private ?string $mimetype = null, + string $type = self::TYPE_FILEPATH ) { + Assertion::choice( + $type, + self::ALLOWED_TYPES, + 'type "%s" is not an element of the valid values: %s' + ); + + $this->type = $type; + } + + public function getType(): string + { + return $this->type; } public function getFile(): string { - return $this->file; + return $this->filePath; } - public function getFilename(): string + public function getFilename(): ?string { return $this->filename; } - public function getMimetype(): string + public function getMimetype(): ?string { return $this->mimetype; } diff --git a/Domain/Model/Mailer/Message.php b/Domain/Model/Mailer/Message.php index 3ecf552..6061814 100644 --- a/Domain/Model/Mailer/Message.php +++ b/Domain/Model/Mailer/Message.php @@ -38,10 +38,13 @@ class Message protected $toAddress; /** - * @var ?Attachment + * @var Attachment[] */ - protected $attachment; + protected $attachments = []; + /** + * @internal + */ public function toEmail(): Email { $message = new Email(); @@ -64,38 +67,41 @@ public function toEmail(): Email ) ->to($this->getToAddress()); - if ($this->attachment) { - $message->attach( - $this->attachment->getFile(), - $this->attachment->getFilename(), - $this->attachment->getMimetype(), - ); + foreach ($this->attachments as $attachment) { + + if ($attachment->getType() === Attachment::TYPE_FILEPATH) { + $message->attachFromPath( + $attachment->getFile(), + $attachment->getFilename(), + $attachment->getMimetype(), + ); + } else { + + $stream = fopen('php://memory', 'rw+'); + fwrite($stream, $attachment->getFile()); + rewind($stream); + + $message->attach( + $stream, + $attachment->getFilename(), + $attachment->getMimetype(), + ); + } } return $message; } - /** - * @return string - */ public function getBody(): string { return $this->body; } - /** - * @return string - */ public function getBodyType(): string { return $this->bodyType; } - /** - * @param string $body - * @param string $bodyType - * @return Message - */ public function setBody(string $body, string $bodyType = 'text/plain'): Message { $this->body = $body; @@ -103,87 +109,72 @@ public function setBody(string $body, string $bodyType = 'text/plain'): Message return $this; } - /** - * @return string - */ public function getSubject(): string { return $this->subject; } - /** - * @param string $subject - * @return Message - */ public function setSubject(string $subject): Message { $this->subject = $subject; return $this; } - /** - * @return string - */ public function getFromAddress(): string { return $this->fromAddress; } - /** - * @param string $fromAddress - * @return Message - */ public function setFromAddress(string $fromAddress): Message { $this->fromAddress = $fromAddress; return $this; } - /** - * @return string - */ public function getFromName(): string { return $this->fromName; } - /** - * @param string $fromName - * @return Message - */ public function setFromName(string $fromName): Message { $this->fromName = $fromName; return $this; } - /** - * @return string - */ public function getToAddress(): string { return $this->toAddress; } - /** - * @param string $toAddress - * @return Message - */ public function setToAddress(string $toAddress): Message { $this->toAddress = $toAddress; return $this; } - /** - * @return void - */ - public function setAttachment($file, $filename, $mimetype) + public function setAttachment($file, $filename, $mimetype, $type = Attachment::TYPE_FILEPATH): Message + { + $this->attachments = []; + $this->addAttachment( + $file, + $filename, + $mimetype, + $type + ); + + return $this; + } + + public function addAttachment($file, $filename, $mimetype, $type = Attachment::TYPE_FILEPATH): Message { - $this->attachment = new Attachment( + $this->attachments[] = new Attachment( $file, $filename, - $mimetype + $mimetype, + $type ); + + return $this; } }