我需要将欢迎电子邮件的副本发送到特定邮件。
我所做的是我试图创建模块来以编程方式完成,我创建了一个覆盖页面,该句柄可以执行CreatePost.php,但之前已发送过。
<?php
/**
* Copyright © Cinemanext, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Cinemanext\ForceLogin\Controller\Account;
use \Magento\Framework\App\ObjectManager;
/**
* @SuppressWarnings(PHPMD.TooManyFields)
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class CreatePost extends \Magento\Customer\Controller\Account\CreatePost
{
}
此选项存在于订单邮件中,您可以将副本发送到特定的电子邮件。
我如何添加相同的功能来欢迎邮件。
答案 0 :(得分:0)
尝试下面的代码
<?php
namespace Cinemanext\ForceLogin\Controller\Account;
use Magento\Framework\App\Action\Context;
use Magento\Framework\View\Result\PageFactory;
use Magento\Store\Model\StoreManagerInterface;
use Magento\Customer\Model\CustomerFactory;
use Magento\Customer\Model\AddressFactory;
use Magento\Framework\Message\ManagerInterface;
use Magento\Framework\Escaper;
use Magento\Framework\UrlFactory;
use Magento\Customer\Model\Session;
use Magento\Customer\Model\Registration;
use Magento\Framework\Data\Form\FormKey\Validator;
use Magento\Framework\App\ObjectManager;
class CreatePost extends \Magento\Framework\App\Action\Action
{
/**
* @var \Magento\Store\Model\StoreManagerInterface
*/
protected $storeManager;
/**
* @var \Magento\Customer\Model\CustomerFactory
*/
protected $customerFactory;
/**
* @var \Magento\Customer\Model\AddressFactory
*/
protected $addressFactory;
/**
* @var \Magento\Framework\Message\ManagerInterface
*/
protected $messageManager;
/**
* @var \Magento\Framework\Escaper
*/
protected $escaper;
/**
* @var \Magento\Framework\UrlFactory
*/
protected $urlFactory;
/**
* @var \Magento\Customer\Model\Session
*/
protected $session;
/**
* @var Magento\Framework\Data\Form\FormKey\Validator
*/
private $formKeyValidator;
/**
* @param Context $context
* @param StoreManagerInterface $storeManager
* @param CustomerFactory $customerFactory
* @param AddressFactory $addressFactory
* @param ManagerInterface $messageManager
* @param Escaper $escaper
* @param UrlFactory $urlFactory
* @param Session $session
* @param Validator $formKeyValidator
*/
public function __construct(
Context $context,
StoreManagerInterface $storeManager,
CustomerFactory $customerFactory,
AddressFactory $addressFactory,
ManagerInterface $messageManager,
Escaper $escaper,
UrlFactory $urlFactory,
Session $session,
Validator $formKeyValidator = null
)
{
$this->storeManager = $storeManager;
$this->customerFactory = $customerFactory;
$this->addressFactory = $addressFactory;
$this->messageManager = $messageManager;
$this->escaper = $escaper;
$this->urlModel = $urlFactory->create();
$this->session = $session;
$this->formKeyValidator = $formKeyValidator ?: ObjectManager::getInstance()->get(Validator::class);
// messageManager can also be set via $context
// $this->messageManager = $context->getMessageManager();
parent::__construct($context);
}
/**
* Default customer account page
*
* @return void
*/
public function execute()
{
/** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */
$resultRedirect = $this->resultRedirectFactory->create();
// if ($this->session->isLoggedIn() || !$this->registration->isAllowed()) {
// $resultRedirect->setPath('*/*/');
// return $resultRedirect;
// }
// check if the form is actually posted and has the proper form key
if (!$this->getRequest()->isPost() || !$this->formKeyValidator->validate($this->getRequest())) {
$url = $this->urlModel->getUrl('*/*/create', ['_secure' => true]);
$resultRedirect->setUrl($this->_redirect->error($url));
return $resultRedirect;
}
$websiteId = $this->storeManager->getWebsite()->getWebsiteId();
$firstName = 'John';
$lastName = 'Doe';
$email = 'johndoe4@example.com';
$password = 'Test1234';
// instantiate customer object
$customer = $this->customerFactory->create();
$customer->setWebsiteId($websiteId);
// check if customer is already present
// if customer is already present, then show error message
// else create new customer
if ($customer->loadByEmail($email)->getId()) {
//echo 'Customer with the email ' . $email . ' is already registered.';
$message = __(
'There is already an account with this email address "%1".',
$email
);
// @codingStandardsIgnoreEnd
$this->messageManager->addError($message);
} else {
try {
// prepare customer data
$customer->setEmail($email);
$customer->setFirstname($firstName);
$customer->setLastname($lastName);
// set null to auto-generate password
$customer->setPassword($password);
// set the customer as confirmed
// this is optional
// comment out this line if you want to send confirmation email
// to customer before finalizing his/her account creation
$customer->setForceConfirmed(true);
// save data
$customer->save();
// save customer address
// this is optional
// you can skip saving customer address while creating the customer
$customerAddress = $this->addressFactory->create();
$customerAddress->setCustomerId($customer->getId())
->setFirstname($firstName)
->setLastname($lastName)
->setCountryId('US')
->setRegionId('12') // optional, depends upon Country, e.g. USA
->setRegion('California') // optional, depends upon Country, e.g. USA
->setPostcode('90232')
->setCity('Culver City')
->setTelephone('888-888-8888')
->setFax('999')
->setCompany('XYZ')
->setStreet(array(
'0' => 'Your Customer Address 1', // compulsory
'1' => 'Your Customer Address 2' // optional
))
->setIsDefaultBilling('1')
->setIsDefaultShipping('1')
->setSaveInAddressBook('1');
try {
// save customer address
$customerAddress->save();
} catch (Exception $e) {
$this->messageManager->addException($e, __('We can\'t save the customer address.'));
}
// send welcome email to the customer
$customer->sendNewAccountEmail();
//echo 'Customer with the email ' . $email . ' is successfully created.';
$this->messageManager->addSuccess(
__(
'Customer account with email %1 created successfully.',
$email
)
);
$url = $this->urlModel->getUrl('*/*/create', ['_secure' => true]);
$resultRedirect->setUrl($this->_redirect->success($url));
//$resultRedirect->setPath('*/*/');
return $resultRedirect;
} catch (StateException $e) {
$url = $this->urlModel->getUrl('customer/account/forgotpassword');
// @codingStandardsIgnoreStart
$message = __(
'There is already an account with this email address. If you are sure that it is your email address, <a href="%1">click here</a> to get your password and access your account.',
$url
);
// @codingStandardsIgnoreEnd
$this->messageManager->addError($message);
} catch (InputException $e) {
$this->messageManager->addError($this->escaper->escapeHtml($e->getMessage()));
foreach ($e->getErrors() as $error) {
$this->messageManager->addError($this->escaper->escapeHtml($error->getMessage()));
}
} catch (LocalizedException $e) {
$this->messageManager->addError($this->escaper->escapeHtml($e->getMessage()));
} catch (\Exception $e) {
$this->messageManager->addException($e, __('We can\'t save the customer.'));
}
}
$this->session->setCustomerFormData($this->getRequest()->getPostValue());
$defaultUrl = $this->urlModel->getUrl('*/*/create', ['_secure' => true]);
$resultRedirect->setUrl($this->_redirect->error($defaultUrl));
return $resultRedirect;
}
}