Skip to content

Commit

Permalink
allow multiple email attachments
Browse files Browse the repository at this point in the history
  • Loading branch information
mmadariaga committed May 26, 2022
1 parent bf23282 commit f9891cd
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 59 deletions.
37 changes: 31 additions & 6 deletions Domain/Model/Mailer/Attachment.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
97 changes: 44 additions & 53 deletions Domain/Model/Mailer/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,13 @@ class Message
protected $toAddress;

/**
* @var ?Attachment
* @var Attachment[]
*/
protected $attachment;
protected $attachments = [];

/**
* @internal
*/
public function toEmail(): Email
{
$message = new Email();
Expand All @@ -64,126 +67,114 @@ 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;
$this->bodyType = $bodyType;
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;
}
}

0 comments on commit f9891cd

Please sign in to comment.