1
0
Fork 0
mirror of synced 2024-10-01 17:58:02 +13:00

mail support string as attachment

This commit is contained in:
Damodar Lohani 2023-12-08 19:57:15 +00:00
parent f16aa598e2
commit 386cee5137
2 changed files with 26 additions and 0 deletions

View file

@ -13,6 +13,7 @@ class Mail extends Event
protected string $body = '';
protected array $smtp = [];
protected array $variables = [];
protected array $attachment = [];
public function __construct(protected Connection $connection)
{
@ -313,6 +314,21 @@ class Mail extends Event
return $this;
}
public function setAttachment(string $content, string $filename, string $encoding = 'base64', string $type = 'plain/text')
{
$this->attachment = [
'content' => $content,
'filename' => $filename,
'encoding' => $encoding,
'type' => $type,
];
}
public function getAttachment(): array
{
return $this->attachment;
}
/**
* Executes the event and sends it to the mails worker.
*
@ -330,6 +346,7 @@ class Mail extends Event
'body' => $this->body,
'smtp' => $this->smtp,
'variables' => $this->variables,
'attachment' => $this->attachment,
'events' => Event::generateEvents($this->getEvent(), $this->getParams())
]);
}

View file

@ -59,6 +59,7 @@ class Mails extends Action
$variables = $payload['variables'];
$name = $payload['name'];
$body = $payload['body'];
$attachment = $payload['attachment'] ?? [];
$bodyTemplate = Template::fromFile(__DIR__ . '/../../../../app/config/locale/templates/email-base.tpl');
$bodyTemplate->setParam('{{body}}', $body);
@ -89,6 +90,14 @@ class Mails extends Action
$mail->Subject = $subject;
$mail->Body = $body;
$mail->AltBody = \strip_tags($body);
if(!empty($attachment['content'] ?? '')) {
$mail->AddStringAttachment(
$attachment['content'],
$attachment['filename'] ?? 'unknown.file',
$attachment['encoding'] ?? PHPMailer::ENCODING_BASE64,
$attachment['type'] ?? 'plain/text'
);
}
try {
$mail->send();