Created
September 29, 2022 11:10
-
-
Save rafaelola/3d1da302e65ca1d0a0d890a68cf9af91 to your computer and use it in GitHub Desktop.
Email Service for attaching file to emails recipients
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
<?php | |
namespace Develo\DailyAuctionReport\Service; | |
use Magento\Framework\App\Config\ScopeConfigInterface; | |
use Magento\Framework\Mail\Template\TransportBuilder; | |
use Laminas\Mime\Mime; | |
use Laminas\Mime\Part; | |
use Laminas\Mime\Message; | |
use Magento\Framework\DataObject; | |
use Magento\Framework\Mail\TransportInterface; | |
use Magento\Framework\Filesystem\Io\File; | |
use Magento\Framework\Translate\Inline\StateInterface; | |
use Magento\Store\Model\StoreManagerInterface; | |
use Magento\Framework\App\Area; | |
class EmailService | |
{ | |
/** | |
* @var TransportBuilder | |
*/ | |
protected $transportBuilder; | |
/** | |
* @var ScopeConfigInterface | |
*/ | |
protected $scopeConfig; | |
/** | |
* @var File | |
*/ | |
protected $file; | |
/** | |
* @var StoreManagerInterface | |
*/ | |
protected $storeManager; | |
/** | |
* Daily Completed Auction Report Template Id | |
*/ | |
const COMPLETED_AUCTION_TEMPLATE_ID = 'daily_completed_auction_report'; | |
/** | |
* Sender email config path | |
*/ | |
const XML_PATH_SUPPORT_SENDER_EMAIL = 'trans_email/ident_support/email'; | |
/** | |
* Sender name config path | |
*/ | |
const XML_PATH_SUPPORT_SENDER_NAME = 'trans_email/ident_support/name'; | |
const XML_REPORT_EMAIL_RECIPIENTS = 'auction_daily_reports/general/report_email_recipients'; | |
/** | |
* @var StateInterface | |
*/ | |
protected $inlineTranslation; | |
/** | |
* EmailService constructor | |
* | |
* @param TransportBuilder $transportBuilder | |
* @param ScopeConfigInterface $scopeConfig | |
* @param File $file | |
* @param StoreManagerInterface $storeManager | |
* @param StateInterface $inlineTranslation | |
*/ | |
public function __construct( | |
TransportBuilder $transportBuilder, | |
ScopeConfigInterface $scopeConfig, | |
File $file, | |
StoreManagerInterface $storeManager, | |
StateInterface $inlineTranslation | |
) { | |
$this->transportBuilder = $transportBuilder; | |
$this->scopeConfig = $scopeConfig; | |
$this->file = $file; | |
$this->storeManager = $storeManager; | |
$this->inlineTranslation = $inlineTranslation; | |
} | |
/** | |
* Send the email for a Help Center submission. | |
* | |
* @param DataObject $templateParams | |
* @param array $attachments | |
* | |
* @throws \Magento\Framework\Exception\LocalizedException | |
* @throws \Magento\Framework\Exception\MailException | |
* @throws \Magento\Framework\Exception\NoSuchEntityException | |
*/ | |
public function send(DataObject $templateParams, array $attachments = []): void | |
{ | |
$storeId = $this->storeManager->getStore()->getId(); | |
$this->inlineTranslation->suspend(); | |
$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); | |
$logger = $objectManager->create(\Psr\Log\LoggerInterface::class); | |
foreach ($this->getConfigReportRecipients() as $recipient) { | |
// Build transport | |
/** @var \Magento\Framework\Mail\TransportInterface $transport */ | |
$transport = $this->transportBuilder | |
->setTemplateOptions(['area' => Area::AREA_FRONTEND, 'store' => $storeId]) | |
->setTemplateIdentifier(self::COMPLETED_AUCTION_TEMPLATE_ID) | |
->setTemplateVars($templateParams->toArray()) | |
->setFromByScope('support', 1) | |
->addTo($recipient['email'], $recipient['name']) | |
->getTransport(); | |
// Attach Files to transport | |
foreach ($attachments as $a) { | |
$transport = $this->addAttachment($transport, $a); | |
} | |
// Send transport | |
try{ | |
$transport->sendMessage(); | |
} catch (\Exception $exception){ | |
$logger->debug(" Email Service Exception.". $exception->getMessage()); | |
} | |
} | |
$this->inlineTranslation->resume(); | |
} | |
/** | |
* Add an attachment to the message inside the transport builder. | |
* | |
* @param TransportInterface $transport | |
* @param array $file Sanitized index from $_FILES | |
* | |
* @return TransportInterface | |
*/ | |
protected function addAttachment(TransportInterface $transport, array $file): TransportInterface | |
{ | |
$part = $this->createAttachment($file); | |
$html = $transport->getMessage()->getBody()->generateMessage(); | |
$bodyMessage = new Part($html); | |
$bodyMessage->type = 'text/html'; | |
$bodyPart = new Message(); | |
$bodyPart->setParts(array($bodyMessage,$part)); | |
$transport->getMessage()->setBody($bodyPart); | |
$contentTypeHeader = $transport->getMessage()->getZendMessage()->getHeaders()->get('Content-Type'); | |
$contentTypeHeader->setType('multipart/related'); | |
return $transport; | |
} | |
/** | |
* Create a Laminas mime part that is an attachment to attach to the email. | |
* | |
* | |
* | |
* @param array $file Sanitized index from $_FILES | |
* | |
* @return Part | |
*/ | |
protected function createAttachment(array $file): Part | |
{ | |
$attachment = new Part($this->file->read($file['tmp_name'])); | |
$attachment->disposition = Mime::DISPOSITION_ATTACHMENT; | |
$attachment->encoding = Mime::ENCODING_BASE64; | |
$attachment->filename = $file['name']; | |
$attachment->type = 'text/csv'; | |
return $attachment; | |
} | |
private function getConfigReportRecipients() | |
{ | |
$adminEmails = explode(',', $this->scopeConfig->getValue(self::XML_REPORT_EMAIL_RECIPIENTS, \Magento\Store\Model\ScopeInterface::SCOPE_STORE)); | |
$adminEmails = array_map('trim', $adminEmails); | |
$resultEmails = []; | |
foreach ($adminEmails as $adminEmail){ | |
$parts = explode("@", $adminEmail); | |
$resultEmails[] = [ | |
'email' => $adminEmail, | |
'name' => $parts[0] | |
]; | |
} | |
return $resultEmails; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment