-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
153 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"}} |