yiisoft / yii2

Yii 2: The Fast, Secure and Professional PHP Framework
http://www.yiiframework.com
BSD 3-Clause "New" or "Revised" License
14.24k stars 6.91k forks source link

Variable with name $message can not be passed to email view #4147

Closed alexweb-zz closed 10 years ago

alexweb-zz commented 10 years ago

When you try to send email for example in your MessageForm:

\Yii::$app->mail->compose('message', ['message' => $this->message])
                ->setFrom($emailFrom)
                ->setTo($emailTo)
                ->setSubject('You Have Received a New Message on ' . \Yii::$app->name)
                ->send();

Then in \mail\message.php view $message variable is empty.

If you change name to something else like:

\Yii::$app->mail->compose('message', ['msg' => $this->message])
                ->setFrom($emailFrom)
                ->setTo($emailTo)
                ->setSubject('You Have Received a New Message on ' . \Yii::$app->name)
                ->send();

Then $msg contains data.

klimov-paul commented 10 years ago

Parameter 'message' in the mail view reserved for the mail message instance. Perhaps we should change the way message instance is passed to the view to avoid collisitions.

klimov-paul commented 10 years ago

Perhaps related #3992

qiangxue commented 10 years ago

I don't think we need to make change here.

It doesn't make sense that you pass in a message parameter and you still expect message to be the old message object.

alexweb-zz commented 10 years ago

Ok. But it's a little bit confusing, because it's not clear where $message value were missed. You passing value to variable and then it just disappears

\Yii::$app->mail->compose('message', ['message' => 'Hello world'])
                ->setFrom($emailFrom)
                ->setTo($emailTo)
                ->setSubject('You Have Received a New Message on ' . \Yii::$app->name)
                ->send();
klimov-paul commented 10 years ago

Try var_dump($message)

alexweb-zz commented 10 years ago
object(yii\swiftmailer\Message)[135]
  private '_swiftMessage' => null
  public 'mailer' => 
    object(yii\swiftmailer\Mailer)[133]
      public 'messageClass' => string 'yii\swiftmailer\Message' (length=23)
      private '_swiftMailer' => null
      private '_transport' => 
        array (size=0)
          empty
      public 'htmlLayout' => string 'layouts/html' (length=12)
      public 'textLayout' => string 'layouts/text' (length=12)
      public 'messageConfig' => 
        array (size=0)
          empty
      public 'useFileTransport' => boolean false
      public 'fileTransportPath' => string '@runtime/mail' (length=13)
      public 'fileTransportCallback' => null
      private '_view' (yii\mail\BaseMailer) => 
        object(yii\web\View)[134]
          public 'assetBundles' => 
            array (size=0)
              ...
          public 'title' => null
          public 'metaTags' => null
          public 'linkTags' => null
          public 'css' => null
          public 'cssFiles' => null
          public 'js' => null
          public 'jsFiles' => null
          private '_assetManager' => null
          public 'context' => 
            &object(yii\swiftmailer\Mailer)[133]
          public 'params' => 
            array (size=0)
              ...
          public 'renderers' => null
          public 'defaultExtension' => string 'php' (length=3)
          public 'theme' => null
          public 'blocks' => null
          public 'cacheStack' => 
            array (size=0)
              ...
          public 'dynamicPlaceholders' => 
            array (size=0)
              ...
          private '_viewFiles' (yii\base\View) => 
            array (size=1)
              ...
          private '_events' (yii\base\Component) => 
            array (size=0)
              ...
          private '_behaviors' (yii\base\Component) => 
            array (size=0)
              ...
      private '_viewPath' (yii\mail\BaseMailer) => string 'path/to/app/mail' (length=26)
      private '_events' (yii\base\Component) => 
        array (size=0)
          empty
      private '_behaviors' (yii\base\Component) => null
klimov-paul commented 10 years ago

See it is object

alexweb-zz commented 10 years ago

I know. But expected to be 'Hello world' :)

klimov-paul commented 10 years ago

Message supports magic method __toString(), while message is empty it outputs empty string.

alexweb-zz commented 10 years ago

It's clear. But I passed string 'Hello world'. Why in view I got yii\swiftmailer\Message ? Ok, actually it's not a big problem I think.

qiangxue commented 10 years ago

@klimov-paul I think at this line https://github.com/yiisoft/yii2/blob/master/framework/mail/BaseMailer.php#L171 we probably should check if $params['message'] already exists. If so, we do not overwrite it.

klimov-paul commented 10 years ago

But how we can pass message object then? Skipping it does not seems right. Perhaps we should create special View class for the mail, which can hold message object as its field during the rendering. This will solve #3992 as well.

samdark commented 10 years ago

I think it's a good idea.

qiangxue commented 10 years ago

Since the user overwrites it, it means they don't need it in the view. So why do we care if it's available ?

On Tuesday, July 1, 2014, Alexander Makarov notifications@github.com wrote:

I think it's a good idea.

— Reply to this email directly or view it on GitHub https://github.com/yiisoft/yii2/issues/4147#issuecomment-47673279.

samdark commented 10 years ago

They may need it in a view. message is just common enough to create such conflict.

qiangxue commented 10 years ago

For #3992, I think we can pass $message to the layout. Unlike web layout, here $message is a known common variable, it's fine we pass it to layout.

On Tuesday, July 1, 2014, Qiang Xue qiang.xue@gmail.com wrote:

Since the user overwrites it, it means they don't need it in the view. So why do we care if it's available ?

On Tuesday, July 1, 2014, Alexander Makarov <notifications@github.com javascript:_e(%7B%7D,'cvml','notifications@github.com');> wrote:

I think it's a good idea.

— Reply to this email directly or view it on GitHub https://github.com/yiisoft/yii2/issues/4147#issuecomment-47673279.

qiangxue commented 10 years ago

The conflict doesn't matter. If a user explicitly passes in a $message parameter, what will he expect to get when accessing $message? And if he wants to access the message object, he knows he should not overwrite $message.

Michael-Holland-1 commented 4 years ago

In the original post he has passed an email variable to setFrom but I get an error saying the email that that variable represents is not a Signature Sender on my account. How do I make sure it is?