From d71fbb448e27d172f3fb9b9b4f5e2a20648e85c7 Mon Sep 17 00:00:00 2001 From: Osiozekhai Aliu Date: Wed, 6 Nov 2024 21:18:28 +0100 Subject: [PATCH] added console command to demo usage --- Console/Mail.php | 109 ++++++++++++++++++++++++++++++++++ Model/TransportBuilder.php | 8 +-- README.md | 77 +++--------------------- etc/email_templates.xml | 22 +++++++ view/frontend/email/test.html | 11 ++++ 5 files changed, 153 insertions(+), 74 deletions(-) create mode 100644 Console/Mail.php create mode 100644 etc/email_templates.xml create mode 100644 view/frontend/email/test.html diff --git a/Console/Mail.php b/Console/Mail.php new file mode 100644 index 0000000..5bb8088 --- /dev/null +++ b/Console/Mail.php @@ -0,0 +1,109 @@ +transportBuilderFactory = $transportBuilderFactory; + $this->appState = $appState; + } + + /** + * Configure command + * + * @return void + */ + protected function configure(): void + { + $this->setName('mail:tester') + ->setDescription('Mail with Attachment Example'); + + parent::configure(); + } + + /** + * Execute coamnnd + * + * @param InputInterface $input + * @param OutputInterface $output + * @return int + */ + protected function execute(InputInterface $input, OutputInterface $output): int + { + try { + $this->sendEmail(); + $output->writeln('Email sent successfully.'); + return Command::SUCCESS; + } catch (MailException | LocalizedException $e) { + $output->writeln('Error sending email: ' . $e->getMessage() . ''); + return Command::FAILURE; + } + } + + /** + * Send Email + * + * @return void + * @throws LocalizedException + * @throws MailException + */ + public function sendEmail() + { + $this->appState->setAreaCode(Area::AREA_FRONTEND); + + $transportBuilder = $this->transportBuilderFactory->create(); + + $transportBuilder + ->setTemplateIdentifier('test') + ->setTemplateVars(['var1' => 'value1', 'var2' => 'value2']) + ->setTemplateOptions(['area' => Area::AREA_FRONTEND, 'store' => 1]) + ->setFromByScope('general') + ->addTo('recipient@example.com', 'Recipient Name') + ->addCc('cc@example.com', 'CC Name') + ->addBcc('bcc@example.com') + ->setReplyTo('replyto@example.com', 'ReplyTo Name') + ->addAttachment('Attachment content', 'attachment.txt', 'text/plain'); + + $transport = $transportBuilder->getTransport(); + $transport->sendMessage(); + } +} diff --git a/Model/TransportBuilder.php b/Model/TransportBuilder.php index 9eb3a46..2e6bcb8 100644 --- a/Model/TransportBuilder.php +++ b/Model/TransportBuilder.php @@ -324,10 +324,10 @@ protected function getTemplate(): TemplateInterface private function getTemplateType(TemplateInterface $template): ?string { switch ($template->getType()) { - case self::TYPE_TEXT: - return MimeInterface::TYPE_TEXT; - case self::TYPE_HTML: - return MimeInterface::TYPE_HTML; + case self::TYPE_TEXT: + return MimeInterface::TYPE_TEXT; + case self::TYPE_HTML: + return MimeInterface::TYPE_HTML; } throw new InvalidArgumentException('Unknown template type'); } diff --git a/README.md b/README.md index 25c6bef..3c9e58d 100644 --- a/README.md +++ b/README.md @@ -15,81 +15,18 @@ To install the `MagentoMailhAttachment` module, use Composer: composer require osio/magento-mail_attachment bin/magento setup:upgrade + bin/magento cache:flush ## Usage -Here is a basic example of how to use the `MagentoMailhAttachment` module to send an email with an attachment: +the class `Osio\MagentoMailAttachment\Console` is a basic example usage +how to use the `MagentoMailhAttachment` module to send an email with an attachment: +You can test the Console command with `bin/magento mail:tester ` +> use a mail cacher like [Mailhog](https://github.com/mailhog/MailHog) to catch the E-Mails on yout locahost - transportBuilder = new TransportBuilder( - $escaper, - $templateFactory, - $messageFactory->create(), - $senderResolver, - $objectManager, - $partFactory, - $mailTransportFactory, - $messageFactory, - $emailMessageInterfaceFactory, - $mimeMessageInterfaceFactory, - $mimePartInterfaceFactory, - $addressConverter - ); - } - - public function sendEmail() - { - $this->transportBuilder - ->setTemplateIdentifier('email_template_identifier') - ->setTemplateVars(['var1' => 'value1', 'var2' => 'value2']) - ->setTemplateOptions(['area' => 'frontend', 'store' => 1]) - ->setFromByScope('general') - ->addTo('recipient@example.com', 'Recipient Name') - ->addCc('cc@example.com', 'CC Name') - ->addBcc('bcc@example.com') - ->setReplyTo('replyto@example.com', 'ReplyTo Name') - ->addAttachment('Attachment content', 'attachment.txt', 'text/plain'); - - $transport = $this->transportBuilder->getTransport(); - $transport->sendMessage(); - } - } - -### Contributions are welcome! +## Contributions are welcome! Please submit a pull request or open an issue to discuss your changes. -## License - +## License This module is open-source and licensed under the MIT License. diff --git a/etc/email_templates.xml b/etc/email_templates.xml new file mode 100644 index 0000000..3c138ff --- /dev/null +++ b/etc/email_templates.xml @@ -0,0 +1,22 @@ + + + +