1
0
Fork 0
mirror of synced 2024-06-14 08:44:49 +12:00
appwrite/src/Appwrite/Event/Mail.php

423 lines
8.2 KiB
PHP
Raw Normal View History

<?php
namespace Appwrite\Event;
use Utopia\Queue\Client;
use Utopia\Queue\Connection;
class Mail extends Event
{
protected string $recipient = '';
protected string $name = '';
2023-06-04 20:19:49 +12:00
protected string $subject = '';
protected string $body = '';
2023-03-12 15:12:09 +13:00
protected array $smtp = [];
2023-08-28 10:45:37 +12:00
protected array $variables = [];
2023-12-07 02:52:13 +13:00
protected string $bodyTemplate = '';
2023-12-09 08:57:15 +13:00
protected array $attachment = [];
public function __construct(protected Connection $connection)
{
2023-06-02 15:54:34 +12:00
parent::__construct($connection);
$this
->setQueue(Event::MAILS_QUEUE_NAME)
->setClass(Event::MAILS_CLASS_NAME);
}
2022-04-19 04:21:45 +12:00
/**
2023-06-04 20:19:49 +12:00
* Sets subject for the mail event.
2022-04-19 04:21:45 +12:00
*
2023-06-04 20:19:49 +12:00
* @param string $subject
2022-04-19 04:21:45 +12:00
* @return self
*/
2023-06-04 20:19:49 +12:00
public function setSubject(string $subject): self
{
2023-06-04 20:19:49 +12:00
$this->subject = $subject;
return $this;
}
2022-04-19 04:21:45 +12:00
/**
2023-03-13 22:21:16 +13:00
* Returns subject for the mail event.
2022-04-19 04:21:45 +12:00
*
2023-06-04 20:19:49 +12:00
* @return string
2022-04-19 04:21:45 +12:00
*/
2023-06-04 20:19:49 +12:00
public function getSubject(): string
{
2023-06-04 20:19:49 +12:00
return $this->subject;
}
2022-04-19 04:21:45 +12:00
/**
* Sets recipient for the mail event.
*
* @param string $recipient
* @return self
*/
public function setRecipient(string $recipient): self
{
$this->recipient = $recipient;
return $this;
}
2022-04-19 04:21:45 +12:00
/**
* Returns set recipient for mail event.
*
* @return string
*/
public function getRecipient(): string
{
return $this->recipient;
}
2022-04-19 04:21:45 +12:00
/**
* Sets body for the mail event.
2022-04-19 04:21:45 +12:00
*
* @param string $body
2022-04-19 04:21:45 +12:00
* @return self
*/
public function setBody(string $body): self
{
$this->body = $body;
return $this;
}
2022-04-19 04:21:45 +12:00
/**
* Returns body for the mail event.
2022-04-19 04:21:45 +12:00
*
* @return string
*/
public function getBody(): string
{
return $this->body;
}
2022-04-19 04:21:45 +12:00
/**
* Sets name for the mail event.
*
* @param string $name
* @return self
*/
public function setName(string $name): self
{
$this->name = $name;
return $this;
}
2022-04-19 04:21:45 +12:00
/**
* Returns set name for the mail event.
*
* @return string
*/
public function getName(): string
{
return $this->name;
}
2023-12-07 02:52:13 +13:00
/**
* Sets bodyTemplate for the mail event.
*
* @param string $bodyTemplate
* @return self
*/
public function setbodyTemplate(string $bodyTemplate): self
{
$this->bodyTemplate = $bodyTemplate;
return $this;
}
/**
* Returns subject for the mail event.
*
* @return string
*/
public function getbodyTemplate(): string
{
return $this->bodyTemplate;
}
2022-04-19 04:21:45 +12:00
/**
2023-03-13 19:23:23 +13:00
* Set SMTP Host
2022-04-19 04:21:45 +12:00
*
2023-03-13 19:23:23 +13:00
* @param string $host
2022-04-19 04:21:45 +12:00
* @return self
*/
2023-03-13 19:23:23 +13:00
public function setSmtpHost(string $host): self
{
2023-03-13 19:23:23 +13:00
$this->smtp['host'] = $host;
return $this;
}
2023-03-13 19:23:23 +13:00
/**
* Set SMTP port
*
* @param int port
* @return self
*/
public function setSmtpPort(int $port): self
{
$this->smtp['port'] = $port;
return $this;
}
2022-04-19 04:21:45 +12:00
/**
2023-03-13 19:23:23 +13:00
* Set SMTP username
*
* @param string $username
* @return self
*/
public function setSmtpUsername(string $username): self
{
2023-08-26 03:13:25 +12:00
$this->smtp['username'] = $username;
2023-03-13 19:23:23 +13:00
return $this;
}
/**
* Set SMTP password
*
* @param string $password
* @return self
*/
public function setSmtpPassword(string $password): self
{
2023-08-26 03:13:25 +12:00
$this->smtp['password'] = $password;
return $this;
}
/**
* Set SMTP secure
*
* @param string $password
* @return self
*/
public function setSmtpSecure(string $secure): self
{
$this->smtp['secure'] = $secure;
2023-03-13 19:23:23 +13:00
return $this;
}
/**
* Set SMTP sender email
*
* @param string $senderEmail
* @return self
*/
public function setSmtpSenderEmail(string $senderEmail): self
{
$this->smtp['senderEmail'] = $senderEmail;
return $this;
}
2023-08-26 03:13:25 +12:00
/**
* Set SMTP sender name
*
* @param string $senderName
* @return self
*/
public function setSmtpSenderName(string $senderName): self
{
$this->smtp['senderName'] = $senderName;
return $this;
}
2023-03-13 19:23:23 +13:00
/**
* Set SMTP reply to
*
* @param string $replyTo
* @return self
*/
public function setSmtpReplyTo(string $replyTo): self
{
$this->smtp['replyTo'] = $replyTo;
2023-03-12 15:12:09 +13:00
return $this;
}
/**
* Get SMTP
2022-04-19 04:21:45 +12:00
*
* @return string
*/
2023-03-13 19:23:23 +13:00
public function getSmtpHost(): string
{
2023-03-13 19:23:23 +13:00
return $this->smtp['host'] ?? '';
}
/**
* Get SMTP port
*
* @return integer
*/
public function getSmtpPort(): int
{
return $this->smtp['port'] ?? 0;
}
/**
* Get SMTP username
*
* @return string
*/
public function getSmtpUsername(): string
{
return $this->smtp['username'] ?? '';
}
/**
* Get SMTP password
*
* @return string
*/
public function getSmtpPassword(): string
{
return $this->smtp['password'] ?? '';
}
2023-08-26 03:13:25 +12:00
/**
* Get SMTP secure
*
* @return string
*/
public function getSmtpSecure(): string
{
return $this->smtp['secure'] ?? '';
}
2023-03-13 19:23:23 +13:00
/**
* Get SMTP sender email
*
* @return string
*/
public function getSmtpSenderEmail(): string
{
return $this->smtp['senderEmail'] ?? '';
}
2023-08-26 03:13:25 +12:00
/**
* Get SMTP sender name
*
* @return string
*/
public function getSmtpSenderName(): string
{
return $this->smtp['senderName'] ?? '';
}
2023-03-13 19:23:23 +13:00
/**
* Get SMTP reply to
*
* @return string
*/
public function getSmtpReplyTo(): string
2023-03-12 15:12:09 +13:00
{
2023-03-13 19:23:23 +13:00
return $this->smtp['replyTo'] ?? '';
2023-03-12 15:12:09 +13:00
}
2023-08-28 10:45:37 +12:00
/**
* Get Email Variables
2023-08-28 17:53:29 +12:00
*
2023-08-28 10:45:37 +12:00
* @return array
*/
public function getVariables(): array
{
return $this->variables;
}
/**
* Set Email Variables
2023-08-28 17:53:29 +12:00
*
2023-08-28 10:45:37 +12:00
* @param array $variables
* @return self
*/
public function setVariables(array $variables): self
{
$this->variables = $variables;
return $this;
}
2024-02-25 03:33:38 +13:00
/**
* Set attachment
* @param string $content
* @param string $filename
* @param string $encoding
* @param string $type
* @return self
*/
2023-12-09 08:57:15 +13:00
public function setAttachment(string $content, string $filename, string $encoding = 'base64', string $type = 'plain/text')
{
$this->attachment = [
2023-12-09 10:34:39 +13:00
'content' => base64_encode($content),
2023-12-09 08:57:15 +13:00
'filename' => $filename,
'encoding' => $encoding,
'type' => $type,
];
2023-12-09 10:34:39 +13:00
return $this;
2023-12-09 08:57:15 +13:00
}
2024-02-25 03:33:38 +13:00
/**
* Get attachment
2024-02-25 03:49:17 +13:00
*
2024-02-25 03:33:38 +13:00
* @return array
*/
2023-12-09 08:57:15 +13:00
public function getAttachment(): array
{
return $this->attachment;
}
2024-02-25 03:33:38 +13:00
/**
* Reset attachment
2024-02-25 03:49:17 +13:00
*
2024-02-25 03:33:38 +13:00
* @return self
*/
public function resetAttachment(): self
{
$this->attachment = [];
return $this;
}
2024-02-25 03:56:53 +13:00
/**
* Reset
*
* @return self
*/
public function reset(): self
{
$this->project = null;
$this->recipient = '';
$this->name = '';
$this->subject = '';
$this->body = '';
$this->variables = [];
$this->bodyTemplate = '';
$this->attachment = [];
2024-02-25 04:18:18 +13:00
return $this;
2024-02-25 03:56:53 +13:00
}
2022-04-19 04:21:45 +12:00
/**
* Executes the event and sends it to the mails worker.
*
* @return string|bool
* @throws \InvalidArgumentException
*/
public function trigger(): string|bool
{
$client = new Client($this->queue, $this->connection);
return $client->enqueue([
2024-02-16 04:12:37 +13:00
'project' => $this->project,
'recipient' => $this->recipient,
'name' => $this->name,
2023-06-04 20:19:49 +12:00
'subject' => $this->subject,
2023-12-07 02:52:13 +13:00
'bodyTemplate' => $this->bodyTemplate,
2023-06-04 20:19:49 +12:00
'body' => $this->body,
2023-03-13 22:21:16 +13:00
'smtp' => $this->smtp,
2023-08-28 10:45:37 +12:00
'variables' => $this->variables,
2023-12-09 08:57:15 +13:00
'attachment' => $this->attachment,
'events' => Event::generateEvents($this->getEvent(), $this->getParams())
]);
}
2023-12-09 19:41:24 +13:00
}