I use mailcatcher within docker magento under wsl2.
Overall the module works good, but I can’t get any mail on submit.
Code from Result.php in Controller/Index
<?php
/**
* @copyright Copyright (c) 2016 www.magebuzz.com
*/
namespace MagebuzzTestimonialControllerIndex;
use MagebuzzTestimonialModelTestimonialFactory;
use MagentoFrameworkAppActionAction;
use MagentoFrameworkAppActionContext;
use MagentoFrameworkFilesystem;
use MagentoFrameworkViewResultPageFactory;
class Result extends Action
{
/**
* @var MagentoFrameworkAppCacheTypeListInterface
*/
protected $cacheTypeList;
protected $resultPageFactory;
protected $testimonialFactory;
protected $forwordFactory;
/**
* @var MagentoFrameworkImageAdapterFactory
*/
protected $adapterFactory;
/**
* @var MagentoMediaStorageModelFileUploaderFactory
*/
protected $uploader;
/**
* @var MagentoFrameworkFilesystem
*/
protected $filesystem;
protected $_helper;
/**
* Recipient email config path
*/
const XML_PATH_EMAIL_RECIPIENT = 'contact/email/recipient_email';
/**
* @var MagentoFrameworkMailTemplateTransportBuilder
*/
protected $_transportBuilder;
/**
* @var MagentoFrameworkTranslateInlineStateInterface
*/
protected $inlineTranslation;
/**
* @var MagentoFrameworkAppConfigScopeConfigInterface
*/
protected $scopeConfig;
/**
* @var MagentoStoreModelStoreManagerInterface
*/
protected $storeManager;
/**
* @var MagentoFrameworkEscaper
*/
protected $_escaper;
protected $_filesystem;
protected $_storeManager;
protected $_directory;
protected $_imageFactory;
public function __construct(
Context $context,
PageFactory $pageFactory,
TestimonialFactory $testimonialFactory,
MagentoFrameworkAppCacheTypeListInterface $cacheTypeList,
MagentoFrameworkImageAdapterFactory $adapterFactory,
MagentoMediaStorageModelFileUploaderFactory $uploader,
MagentoFrameworkFilesystem $filesystem,
MagebuzzTestimonialHelperData $helper,
MagentoFrameworkMailTemplateTransportBuilder $transportBuilder,
MagentoFrameworkTranslateInlineStateInterface $inlineTranslation,
MagentoFrameworkAppConfigScopeConfigInterface $scopeConfig,
MagentoStoreModelStoreManagerInterface $storeManager,
MagentoFrameworkEscaper $escaper,
MagentoFrameworkImageAdapterFactory $imageFactory
)
{
$this->_filesystem = $filesystem;
$this->_storeManager = $storeManager;
$this->_directory = $filesystem->getDirectoryWrite(MagentoFrameworkAppFilesystemDirectoryList::MEDIA);
$this->_imageFactory = $imageFactory;
$this->cacheTypeList = $cacheTypeList;
$this->_helper = $helper;
$this->adapterFactory = $adapterFactory;
$this->uploader = $uploader;
$this->filesystem = $filesystem;
$this->testimonialFactory = $testimonialFactory;
$this->resultPageFactory = $pageFactory;
parent::__construct($context);
$this->_transportBuilder = $transportBuilder;
$this->inlineTranslation = $inlineTranslation;
$this->scopeConfig = $scopeConfig;
$this->storeManager = $storeManager;
$this->_escaper = $escaper;
}
public function execute()
{
$data = $this->getRequest()->getPostValue();
/** @var MagentoBackendModelViewResultRedirect $resultRedirect */
$resultRedirect = $this->resultRedirectFactory->create();
if ($data) {
//start block upload image
if (isset($_FILES['avatar']) && isset($_FILES['avatar']['name']) && strlen($_FILES['avatar']['name'])) {
/*
* Save image upload
*/
try {
$base_media_path = 'magebuzz/testimonial/images/';
$uploader = $this->uploader->create(
['fileId' => 'avatar']
);
$uploader->setAllowedExtensions(['jpg', 'jpeg', 'gif', 'png']);
$imageAdapter = $this->adapterFactory->create();
$uploader->addValidateCallback('image', $imageAdapter, 'validateUploadFile');
$uploader->setAllowRenameFiles(true);
$mediaDirectory = $this->filesystem->getDirectoryRead(MagentoFrameworkAppFilesystemDirectoryList::MEDIA);
$result = $uploader->save(
$mediaDirectory->getAbsolutePath($base_media_path)
);
// Resize and keep save new folder //
$this->imageResize($_FILES['avatar']['name']);
// Resize and keep save new folder //
$data['avatar'] = $result['name'];
} catch (Exception $e) {
if ($e->getCode() == 0) {
$this->messageManager->addError($e->getMessage());
}
}
}
//end block upload image
/** @var MagebuzzTestimonialModelTestimonial $model */
$model = $this->_objectManager->create('MagebuzzTestimonialModelTestimonial');
$id = $this->getRequest()->getParam('mb_testimonial_id');
if ($id) {
$model->load($id);
}
$model->setData($data);
// Check approved
if ($this->_helper->isApprove() == 1) {
$model->setIsActive(2);
if($this->_helper->isApproveEmail() ==1){
//$model->sendApprovedEmailToCustomer();
}
}
try {
$model->save();
// Start send email
if($this->_helper->isSubmitEmail() ==1){
//$model->sendSubmittedEmailToCustomer();
}
// End send email
$this->cacheTypeList->invalidate('full_page');
$this->messageManager->addSuccess(__($this->_helper->getMessageThankYou()));
$this->_objectManager->get('MagentoBackendModelSession')->setFormData(false);
if ($this->getRequest()->getParam('back')) {
return $resultRedirect->setPath('*/*/edit', ['mb_testimonial_id' => $model->getId(), '_current' => true]);
}
return $resultRedirect->setPath('*/*/');
} catch (MagentoFrameworkExceptionLocalizedException $e) {
$this->messageManager->addError($e->getMessage());
} catch (RuntimeException $e) {
$this->messageManager->addError($e->getMessage());
} catch (Exception $e) {
$this->messageManager->addException($e, __('Something went wrong while saving the Testimonial.'));
}
return $resultRedirect->setPath('*/*/index', ['mb_testimonial_id' => $this->getRequest()->getParam('mb_testimonial_id')]);
}
return $resultRedirect->setPath('*/*/');
}
public function imageResize($data)
{
$image = $data;
$absPath = $this->_filesystem->getDirectoryRead(MagentoFrameworkAppFilesystemDirectoryList::MEDIA)->getAbsolutePath('magebuzz/testimonial/images/') . $image;
$imageResized = $this->_filesystem->getDirectoryRead(MagentoFrameworkAppFilesystemDirectoryList::MEDIA)->getAbsolutePath('magebuzz/testimonial/images/resized/') . $image;
$imageResize = $this->_imageFactory->create();
$imageResize->open($absPath);
$resizeImage = 400;
// get origin width image
$widthOld = $imageResize->getOriginalWidth();
// get origin height image
$heightOld = $imageResize->getOriginalHeight();
$scaleWidth = ($widthOld/$heightOld);
$scaleHeight = ($heightOld/$widthOld);
if($widthOld > $heightOld)
{
$imageResize->resize(null, $resizeImage);
$newWidth = round($scaleWidth * $resizeImage);
$imageResize->crop(0, ($newWidth - $resizeImage) / 2, ($newWidth - $resizeImage) / 2, 0);
}else{
$imageResize->resize($resizeImage, null);
$newHeight = round($scaleHeight * $resizeImage);
$imageResize->crop(($newHeight - $resizeImage) / 2, 0, 0, ($newHeight - $resizeImage) / 2);
}
$dest = $imageResized;
$imageResize->save($dest);
$resizedURL = $this->_storeManager->getStore()->getBaseUrl(MagentoFrameworkUrlInterface::URL_TYPE_MEDIA) . 'magebuzz/testimonial/images/resized/' . $image;
}
}
Testimonial Model
<?php
/**
* @copyright Copyright (c) 2016 www.magebuzz.com
*/
namespace MagebuzzTestimonialModel;
use MagentoFrameworkModelAbstractModel;
class Testimonial extends AbstractModel
{
const XML_PATH_SUBMITTED_EMAIL_TEMPLATE = 'testimonial_config/group_testimonial_email_configuration/config_email_template_posting';
const XML_PATH_APPROVED_EMAIL_TEMPLATE = 'testimonial_config/group_testimonial_email_configuration/config_email_template_approving';
const STATUS_APPROVE = 2;
const STATUS_PENDING = 1;
const STATUS_DENY = 0;
/**
* Store manager
*
* @var MagentoStoreModelStoreManagerInterface
*/
protected $_storeManager;
protected $scopeConfig;
/**
* @var MagentoFrameworkMailTemplateTransportBuilder
*/
protected $_transportBuilder;
/**
* @var MagentoFrameworkTranslateInlineStateInterface
*/
protected $inlineTranslation;
protected $_testimonialHelper;
/**
* Testimonial constructor.
* @param MagentoFrameworkModelContext $context
* @param MagentoStoreModelStoreManagerInterface $storeManager
* @param MagentoFrameworkAppConfigScopeConfigInterface $scopeConfig
* @param MagentoFrameworkRegistry $registry
* @param MagentoFrameworkMailTemplateTransportBuilder $transportBuilder
* @param MagentoFrameworkTranslateInlineStateInterface $inlineTranslation
* @param MagebuzzTestimonialHelperData $testimonialHelper
* @param MagentoFrameworkModelResourceModelAbstractResource|null $resource
* @param MagentoFrameworkDataCollectionAbstractDb|null $resourceCollection
* @param array $data
*/
function __construct(
MagentoFrameworkModelContext $context,
MagentoStoreModelStoreManagerInterface $storeManager,
MagentoFrameworkAppConfigScopeConfigInterface $scopeConfig,
MagentoFrameworkRegistry $registry,
MagentoFrameworkMailTemplateTransportBuilder $transportBuilder,
MagentoFrameworkTranslateInlineStateInterface $inlineTranslation,
MagebuzzTestimonialHelperData $testimonialHelper,
MagentoFrameworkModelResourceModelAbstractResource $resource = null,
MagentoFrameworkDataCollectionAbstractDb $resourceCollection = null,
array $data = [])
{
$this->_storeManager = $storeManager;
$this->scopeConfig = $scopeConfig;
$this->_transportBuilder = $transportBuilder;
$this->inlineTranslation = $inlineTranslation;
$this->_testimonialHelper = $testimonialHelper;
parent::__construct($context, $registry, $resource, $resourceCollection, $data);
}
protected function _construct()
{
$this->_init('MagebuzzTestimonialModelResourceModelTestimonial');
}
public function getAvailableStatuses(){
return [
self::STATUS_APPROVE => __('Approve'),
self::STATUS_PENDING => __('Pending'),
self::STATUS_DENY => __('Deny')
];
}
public function sendSubmittedEmailToCustomer()
{
$storeId = $this->_storeManager->getStore()->getId();
$this->_testimonialHelper->setStoreId($storeId);
$this->inlineTranslation->suspend();
try {
//load store amdmin email
$sender = [
'email' => $this->scopeConfig->getValue(
'trans_email/ident_support/email',
MagentoStoreModelScopeInterface::SCOPE_STORE
),
'name' => $this->scopeConfig->getValue(
'trans_email/ident_support/name',
MagentoStoreModelScopeInterface::SCOPE_STORE
)
];
$replyTo = $this->scopeConfig->getValue(
'trans_email/ident_support/email',
MagentoStoreModelScopeInterface::SCOPE_STORE
);
$transport = $this->_transportBuilder
->setTemplateIdentifier($this->scopeConfig->getValue(
self::XML_PATH_SUBMITTED_EMAIL_TEMPLATE,
MagentoStoreModelScopeInterface::SCOPE_STORE,
$storeId))
->setTemplateOptions(['area' => MagentoFrameworkAppArea::AREA_FRONTEND, 'store' => $storeId])
->setTemplateVars([
'testimonial' => $this,
'subject' => __('About Your Testimonial')
])
->setFrom($sender)
->addTo($this->getMbTestimonialEmail(), $this->getMbTestimonialName())
->setReplyTo($replyTo)
->getTransport();
$transport->sendMessage();
$this->inlineTranslation->resume();
}
catch (Exception $e) {
//silence is gold
}
}
public function sendApprovedEmailToCustomer()
{
$storeId = $this->_storeManager->getStore()->getId();
$this->_testimonialHelper->setStoreId($storeId);
$this->inlineTranslation->suspend();
try {
//load store amdmin email
$sender = [
'email' => $this->scopeConfig->getValue(
'trans_email/ident_support/email',
MagentoStoreModelScopeInterface::SCOPE_STORE
),
'name' => $this->scopeConfig->getValue(
'trans_email/ident_support/name',
MagentoStoreModelScopeInterface::SCOPE_STORE
)
];
$replyTo = $this->scopeConfig->getValue(
'trans_email/ident_support/email',
MagentoStoreModelScopeInterface::SCOPE_STORE
);
$transport = $this->_transportBuilder
->setTemplateIdentifier($this->scopeConfig->getValue(
self::XML_PATH_APPROVED_EMAIL_TEMPLATE,
MagentoStoreModelScopeInterface::SCOPE_STORE,
$storeId))
->setTemplateOptions(['area' => MagentoFrameworkAppArea::AREA_FRONTEND, 'store' => $storeId])
->setTemplateVars([
'testimonial' => $this,
'subject' => __('About Your Testimonial')
])
->setFrom($sender)
->addTo($this->getMbTestimonialEmail(), $this->getMbTestimonialName())
->setReplyTo($replyTo)
->getTransport();
$transport->sendMessage();
$this->inlineTranslation->resume();
}
catch (Exception $e) {
//silence is gold
}
}
}