2017-10-25 6 views
1

ハローみんな、Magentoの2 - バージョン2.2 - Magentoの2.2 Zend_Mail_Transport_Sendmailはもう機能していない古いオーバーライドのリリース以来Zend_Mail_Transport_Sendmail

をオーバーライドします。

以前は、etc/di.xmlとmodel/Transport.phpの書き換えが成功しました。

<preference for="Magento\Framework\Mail\Transport" type="MyModul\MyPlugin\Model\Transport"/> 

namespace MyModul\MyPlugin\Model; 

class Transport extends \Zend_Mail_Transport_Sendmail implements \Magento\Framework\Mail\TransportInterface 
{ 

    protected $_message; 

    /** 
    * @param \Magento\Framework\Mail\MessageInterface $message 
    * @throws \Zend_Mail_Exception 
    */ 
    public function __construct(
     \Magento\Framework\Mail\MessageInterface $message 
    ) 
    { 
     if (!$message instanceof \Zend_Mail) { 
      throw new \InvalidArgumentException('The message should be an instance of \Zend_Mail'); 
     } 

     parent::__construct('-f ' . '[email protected]'); 
     $this->_message = $message; 
    } 



    /** 
    * Send a mail using this transport 
    * 
    * @return void 
    * @throws \Magento\Framework\Exception\MailException 
    */ 
    public function sendMessage() 
    { 
     try { 
      parent::send($this->_message); 
     } catch (\Exception $e) { 
      throw new \Magento\Framework\Exception\MailException(new \Magento\Framework\Phrase($e->getMessage()), $e); 
     } 
    } 

    /** 
    * @inheritdoc 
    */ 
    public function getMessage() 
    { 
     return $this->_message; 
    } 

    /** 
    * Send mail using PHP native mail() 
    * 
    * @access public 
    * @return void 
    * @throws Zend_Mail_Transport_Exception if parameters is set 
    *   but not a string 
    * @throws Zend_Mail_Transport_Exception on mail() failure 
    */ 
    public function _sendMail() 
    { 

     /** 
     * revert changes from update to 2.1.7 
     * 
     * mailing was not working with Envelope >= 2.1.7 
     */ 
     if ($this->parameters === null) { 
      set_error_handler(array($this, '_handleMailErrors')); 
      $result = mail(
       $this->recipients, 
       $this->_mail->getSubject(), 
       $this->body, 
       $this->header); 
      restore_error_handler(); 
     } else { 
      if(!is_string($this->parameters)) { 
       /** 
       * @see Zend_Mail_Transport_Exception 
       * 
       * Exception is thrown here because 
       * $parameters is a public property 
       */ 
       #require_once 'Zend/Mail/Transport/Exception.php'; 
       throw new Zend_Mail_Transport_Exception(
        'Parameters were set but are not a string' 
       ); 
      } 

      set_error_handler(array($this, '_handleMailErrors')); 
      $result = mail(
       $this->recipients, 
       $this->_mail->getSubject(), 
       $this->body, 
       $this->header, 
       $this->parameters); 
      restore_error_handler(); 
     } 

     if ($this->_errstr !== null || !$result) { 
      /** 
      * @see Zend_Mail_Transport_Exception 
      */ 
      #require_once 'Zend/Mail/Transport/Exception.php'; 
      throw new Zend_Mail_Transport_Exception('Unable to send mail. ' . $this->_errstr); 
     } 
    } 
} 

しかし、今、私は一種の私は今行われることになっている方法であると思われ、プラグインを作成しようと試み

を失っています。

<type name="\Magento\Framework\Mail\Transport"> 
    <plugin sortOrder="50" name="SendMailPlugin" type="MyModul\MyPlugin\Plugin\SendMailPlugin"/> 
</type> 

「TransportInterface」のオーバーライドについても読んでいますが、動作させることはできません。私はプラグインでコンストラクタを呼び出す必要があります。事前に

よろしくと感謝

UPDATE:

それが今再び取り組んでいます。問題は実際にはTransportInterfaceだけでした。私はクラスをオーバーライドするときに好みがインターフェイスを指す必要があり

<preference for="Magento\Framework\Mail\TransportInterface" type="MyModul\MyPlugin\Model\Transport" /> 

...私はすでにまたは多分それは「世代」キャッシュとは何かを持っていたことをしようと試みたと思いました。

私はまだ動作するようにプラグインのバージョンを取得しませんでした。私の最初の試みでは、プラグイン "after__construct"または "afterConstruct"でメソッドを呼び出す方法がわかりませんでした。また、parent :: contructを呼び出しています。

私はそれが最終的に動作するようになるなら私はそれを投稿します。

ご協力いただきありがとうございます。

UPDATE 2:contructのmethodesプラグインに設定することができないので

プラグイン溶液は、この場合には不可能イスト。 [OK]を

https://www.mageplaza.com/magento-2-module-development/magento-2-plugin-interceptor.html

+0

これはmagento.stackexchange.comで行ってください。 – jmargolisvt

答えて

0

は、まず第一に、あなたはあなたが別のクラスをオーバーライドするクラスとしてそれを使用しようとMagentoの2でプラグインの役割を誤解が、これは間違ったアプローチです。プラグインを使用すると、入力、出力、またはその両方を変更することによって、クラス内のパブリックメソッドをラップすることができます。あなたが探しているものdocumentation

でプラグインについての詳細を読むことができますが、依存性注入とpreferences

あなたの最初のアプローチが正しいことに慣れる必要があるので、M2方式でオーバーライドです。 Magento_Emailの後にモジュールがロードされていることを確認するためにモジュールでシーケンスを正しく設定しましたか? etc/configのシーケンスを確認します。あなたが試みることができる別のことは、トランスポートモデルではなく、特にオーバーライドがグローバルである場合に、TransportInterfaceの優先度を設定することです。あなたがモデル アプリ/コード/ Magentoの/ Eメール/モデル/ Transport.php

のためのプラグインを使用する必要が

0

はあなたにあなたのモジュールのフォルダ

<config> 
<type name="Magento\Email\Model\Transport"> 
    <plugin name="yourpluginforemail" type="\My\Module\Model\MyPlugin" sortOrder="1" /> 
</type> 

di.xmlを作成し、あなたのプラグインファイルはlookeのようになります

<?php 

namespace My\Module\Model; 

class MyPlugin 
{ 
    public function afterSendEmail($subject, $result) 
    { 
     return 'some thing else' 
    } 
} 
?> 

それともたい場合は、あなたが作成することで、このファイルを上書きすることができます自身と設定されたシーケンス

あなたのモジュールでdie.xml

module.xml

<module name="My_Module" setup_version="2.0.1"> 
      <sequence> 
       <module name="Magento_Email"/> 

      </sequence> 
     </module> 

それからでしょうそれを上書きする。

だから二つの方法 - オーバーライドクラスまたは「後」を経由して1つのメソッド「周り」プラグイン

0

バルトシュハーバ

「前」ソリューションは、シングルショットで働いていました。ありがとうございました。

関連する問題