config = $config; $this->entityManager = $entityManager; $this->injectableFactory = $injectableFactory; $this->transportFactory = $transportFactory; $this->log = $log; } private function createSender(): Sender { return new Sender( $this->config, $this->entityManager, $this->injectableFactory, $this->log, $this->transportFactory, $this->getInboundEmailService(), $this->getSystemInboundEmail() ); } /** * Create a builder. */ public function create(): Sender { return $this->createSender(); } /** * With parameters. * * @param SenderParams|array $params */ public function withParams($params): Sender { return $this->createSender()->withParams($params); } /** * With specific SMTP parameters. * * @param SmtpParams|array $params */ public function withSmtpParams($params): Sender { return $this->createSender()->withSmtpParams($params); } /** * With specific attachments. * * @param iterable<\Espo\Entities\Attachment> $attachmentList */ public function withAttachments(iterable $attachmentList): Sender { return $this->createSender()->withAttachments($attachmentList); } /** * With envelope options. * * @param array $options */ public function withEnvelopeOptions(array $options): Sender { return $this->createSender()->withEnvelopeOptions($options); } /** * Set a message instance. */ public function withMessage(Message $message): Sender { return $this->createSender()->withMessage($message); } /** * Whether system SMTP is configured. */ public function hasSystemSmtp(): bool { if ($this->config->get('smtpServer')) { return true; } if ($this->getSystemInboundEmail()) { return true; } return false; } private function getSystemInboundEmail(): ?InboundEmail { $address = $this->config->get('outboundEmailFromAddress'); if (!$this->systemInboundEmailIsCached && $address) { $this->systemInboundEmail = $this->entityManager ->getRDBRepository('InboundEmail') ->where([ 'status' => 'Active', 'useSmtp' => true, 'emailAddress' => $address, ]) ->findOne(); } $this->systemInboundEmailIsCached = true; return $this->systemInboundEmail; } private function getInboundEmailService(): InboundEmailService { if (!$this->inboundEmailService) { $this->inboundEmailService = $this->injectableFactory->create(InboundEmailService::class); } return $this->inboundEmailService; } /** * Send an email. * * @throws \Espo\Core\Exceptions\Error */ public function send(Email $email): void { $this->createSender()->send($email); } /** * Generate a message ID. */ static public function generateMessageId(Email $email): string { return Sender::generateMessageId($email); } }