Skip to content

Commit

Permalink
added console command to demo usage
Browse files Browse the repository at this point in the history
  • Loading branch information
aliuosio committed Nov 6, 2024
1 parent 04760b7 commit d71fbb4
Show file tree
Hide file tree
Showing 5 changed files with 153 additions and 74 deletions.
109 changes: 109 additions & 0 deletions Console/Mail.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<?php declare(strict_types=1);
/**
* @author Osiozekhai Aliu
* @package Osio_MagentoMailAttachment
* @copyright Copyright (c) 2024 Osio
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Osio\MagentoMailAttachment\Console;

use Magento\Framework\App\Area;
use Magento\Framework\App\State;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\MailException;
use Osio\MagentoMailAttachment\Model\TransportBuilderFactory;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class Mail extends Command
{

/**
* @var TransportBuilderFactory $transportBuilderFactory
*/
private TransportBuilderFactory $transportBuilderFactory;

/**
* @var State $appState
*/
private State $appState;

/**
* @param TransportBuilderFactory $transportBuilderFactory
* @param State $appState
* @param string|null $name
*/
public function __construct(
TransportBuilderFactory $transportBuilderFactory,
State $appState,
?string $name = null
) {
parent::__construct($name);
$this->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('<info>Email sent successfully.</info>');
return Command::SUCCESS;
} catch (MailException | LocalizedException $e) {
$output->writeln('<error>Error sending email: ' . $e->getMessage() . '</error>');
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();
}
}
8 changes: 4 additions & 4 deletions Model/TransportBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
Expand Down
77 changes: 7 additions & 70 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
<?php

use Osio\MagentoMailAttachment\Model\TransportBuilder;
use Magento\Framework\Mail\Template\FactoryInterface;
use Magento\Framework\Mail\Template\SenderResolverInterface;
use Magento\Framework\ObjectManagerInterface;
use Laminas\Mime\PartFactory;
use Magento\Framework\Escaper;
use Magento\Framework\Mail\TransportInterfaceFactory;
use Magento\Framework\Mail\MessageInterfaceFactory;
use Magento\Framework\Mail\EmailMessageInterfaceFactory;
use Magento\Framework\Mail\MimeMessageInterfaceFactory;
use Magento\Framework\Mail\MimePartInterfaceFactory;
use Magento\Framework\Mail\AddressConverter;

class EmailSender
{
private TransportBuilder $transportBuilder;

public function __construct(
Escaper $escaper,
FactoryInterface $templateFactory,
SenderResolverInterface $senderResolver,
ObjectManagerInterface $objectManager,
PartFactory $partFactory,
TransportInterfaceFactory $mailTransportFactory,
MessageInterfaceFactory $messageFactory,
EmailMessageInterfaceFactory $emailMessageInterfaceFactory,
MimeMessageInterfaceFactory $mimeMessageInterfaceFactory,
MimePartInterfaceFactory $mimePartInterfaceFactory,
AddressConverter $addressConverter
) {
$this->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.
22 changes: 22 additions & 0 deletions etc/email_templates.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0"?>
<!--
/**
* @author Osiozekhai Aliu
* @package Osio_MagentoAutoPatch
* @copyright Copyright (c) 2024 Osio
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/email_templates.xsd">
<template
area="frontend"
file="test.html"
id="test"
label="Notification"
module="Osio_MagentoMailAttachment"
type="html"
/>
</config>
11 changes: 11 additions & 0 deletions view/frontend/email/test.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!--@subject Test Mail Subject {{var store.getFrontendName()}} @-->

{{template config_path="design/email/header_template"}}

<h1>Bla Bla Bla</h1>
<ul>
<li>var1: {{var var1}}</li>
<li>var2: {{var var2}}</li>
</ul>

{{template config_path="design/email/footer_template"}}

0 comments on commit d71fbb4

Please sign in to comment.