In my module I have a feature that adds custom tab to product and there is a form that accepts name, mail and question
I need to send mail both to admin and customer.
This is my mail helper
<?php
namespace AlexAskQuestionHelper;
use MagentoFrameworkAppArea;
use MagentoFrameworkAppConfigScopeConfigInterface;
use MagentoFrameworkAppHelperAbstractHelper;
use MagentoFrameworkAppHelperContext;
use MagentoFrameworkExceptionLocalizedException;
use MagentoFrameworkExceptionMailException;
use MagentoFrameworkExceptionNoSuchEntityException;
use MagentoFrameworkMailTemplateTransportBuilder;
use MagentoFrameworkTranslateInlineStateInterface;
use MagentoStoreModelScopeInterface;
use MagentoStoreModelStoreManagerInterface;
use MagentoUserModelUserFactory;
class Mail extends AbstractHelper
{
/**
* @var StoreManagerInterface
*/
private $storeManager;
/**
* @var TransportBuilder
*/
private $transportBuilder;
/**
* @var StateInterface
*/
private $inlineTranslation;
/**
* @var ScopeConfigInterface
*/
protected $scopeConfig;
/**
* @var UserFactory
*/
private $userFactory;
/**
* Mail constructor
*
* @param Context $context
* @param StoreManagerInterface $storeManager
* @param TransportBuilder $transportBuilder
* @param StateInterface $inlineTranslation
* @param ScopeConfigInterface $scopeConfig
* @param UserFactory $userFactory
*/
public function __construct(
Context $context,
StoreManagerInterface $storeManager,
TransportBuilder $transportBuilder,
StateInterface $inlineTranslation,
ScopeConfigInterface $scopeConfig,
UserFactory $userFactory
) {
$this->storeManager = $storeManager;
$this->transportBuilder = $transportBuilder;
$this->inlineTranslation = $inlineTranslation;
$this->scopeConfig = $scopeConfig;
$this->userFactory = $userFactory;
parent::__construct($context);
}
/**
* @param $customerEmail
* @param string $customerName
* @param string $message
* @return void
* @throws LocalizedException
* @throws MailException
* @throws NoSuchEntityException
*/
public function sendMail($customerEmail, string $customerName, string $message)
{
$templateOptions = [
'area' => Area::AREA_FRONTEND,
'store' => $this->storeManager->getStore()->getId()
];
$templateVars = [
'store' => $this->storeManager->getStore(),
'customer_name' => $customerName,
'message' => $message
];
$from = ['email' => $customerEmail, 'name' => $customerName];
$this->inlineTranslation->suspend();
// Send email to admin
$toAdmin = [$this->getAdminEmail()];
$transportAdmin = $this->transportBuilder
->setTemplateIdentifier('askquestion_options_email_settings_template_settings_admin')
->setTemplateOptions($templateOptions)
->setTemplateVars($templateVars)
->setFromByScope($from)
->addTo($toAdmin)
->getTransport();
$transportAdmin->sendMessage();
$this->inlineTranslation->resume();
}
/**
* @return mixed|string
*/
private function getAdminEmail()
{
$transEmailSeller = $this->scopeConfig->getValue(
'trans_email/ident_sales/email',
ScopeInterface::SCOPE_STORE
);
if ($transEmailSeller) {
return $transEmailSeller;
}
return '';
}
}
Code from submit index
/**
* Send Email
*/
if ($request->getParam('email')) {
$email = $request->getParam('email');
$customerName = $request->getParam('name');
$message = $request->getParam('question');
$this->mailHelper->sendMail($email, $customerName, $message);
}