/** * @param \Magento\Framework\Mail\MessageInterface $message * @param \MagePal\GmailSmtpApp\Helper\Data $dataHelper * @throws \Zend_Mail_Exception */ public function __construct(\Magento\Framework\Mail\MessageInterface $message, \MagePal\GmailSmtpApp\Helper\Data $dataHelper) { if (!$message instanceof \Zend_Mail) { throw new \InvalidArgumentException('The message should be an instance of \\Zend_Mail'); } //Set reply-to path $setReturnPath = $dataHelper->getConfigSetReturnPath(); switch ($setReturnPath) { case 1: $returnPathEmail = $message->getFrom(); break; case 2: $returnPathEmail = $dataHelper->getConfigReturnPathEmail(); break; default: $returnPathEmail = null; break; } if ($returnPathEmail !== null && $dataHelper->getConfigSetReturnPath()) { $message->setReturnPath($returnPathEmail); } if ($message->getReplyTo() === NULL && $dataHelper->getConfigSetReplyTo()) { $message->setReplyTo($returnPathEmail); } //set config $smtpConf = ['auth' => strtolower($dataHelper->getConfigAuth()), 'ssl' => $dataHelper->getConfigSsl(), 'username' => $dataHelper->getConfigUsername(), 'password' => $dataHelper->getConfigPassword(), 'port' => $dataHelper->getConfigSmtpPort()]; $smtpHost = $dataHelper->getConfigSmtpHost(); parent::__construct($smtpHost, $smtpConf); $this->_message = $message; }
/** * Index action * * @return \Magento\Backend\Model\View\Result\Page */ public function execute() { $request = $this->getRequest(); $store_id = $request->getParam('store', null); $name = 'MagePal Gmail Smtp App Test'; $username = $request->getPost('username'); $password = $request->getPost('password'); //if default view //see https://github.com/magento/magento2/issues/3019 if (!$request->getParam('store', false)) { if (empty($username) || empty($password)) { $this->getResponse()->setBody(__('Please enter a valid username/password')); return; } } //if password mask (6 stars) $password = $password == '******' ? $this->_dataHelper->getConfigPassword($store_id) : $password; $to = $request->getPost('email') ? $request->getPost('email') : $username; //SMTP server configuration $smtpHost = $request->getPost('smtphost'); $smtpConf = array('auth' => strtolower($request->getPost('auth')), 'ssl' => $request->getPost('ssl'), 'username' => $username, 'password' => $password, 'port' => $request->getPost('smtpport')); $transport = new \Zend_Mail_Transport_Smtp($smtpHost, $smtpConf); $from = trim($request->getPost('from_email')); $from = \Zend_Validate::is($from, 'EmailAddress') ? $from : $username; //Create email $mail = new \Zend_Mail(); $mail->setFrom($from, $name); $mail->addTo($to, $to); $mail->setSubject('Hello from MagePal'); $mail->setBodyText('Thank you for choosing MagePal extension.'); $result = __('Sent... Please check your email') . ' ' . $to; try { //only way to prevent zend from giving a error if (!$mail->send($transport) instanceof \Zend_Mail) { } } catch (\Exception $e) { $result = __($e->getMessage()); } $this->getResponse()->setBody($this->makeClickableLinks($result)); }