Prompt in phpstorm: @property-read
1. In phpstorm prompt: @property-read . as shown in Figure 1
$setting = ConventionEmailSetting::findSettingByConventionId($params['convention_id']);
if($setting && $setting->enable_custom_server == ConventionEmailSetting::ENABLE_CUSTOM_SERVER_YES){
Yii::$app->mailer->transport = [
'class' => 'Swift_SmtpTransport',
'host' => $setting->host,
'username' => $setting->username,
'password' => $setting->password,
'port' => $setting->port ?: '25',
'encryption' => $setting->encryption ?: 'tls',
];
}
2. In common/config/main-local.php
[
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
'viewPath' => '@common/mail',
// send all mails to a file by default. You have to set
// 'useFileTransport' to false and configure a transport
// for the mailer to send real emails.
'useFileTransport' => false,
'transport' => [
'class' => 'Swift_SmtpTransport',
'host' => 'smtpdm.aliyun.com',
'username' => '',
'password' => '',
'port' => '25',
'encryption' => 'tls',
]
],
],
];
3. In yii2, the yii\swiftmailer\mailer component is not set through public properties, but through the setTransport() method. And Yii::$App->Mailer->Transport =[…]It belongs to the direct setting of properties, and phpstorm cannot statically analyze that you are actually calling the setter method indirectly through magic setter. This is why it hints @property-read $transport . The adjustment is as follows, no more errors are reported. as shown in Figure 2
Yii::$app->mailer->setTransport([
'class' => 'Swift_SmtpTransport',
'host' => $setting->host,
'username' => $setting->username,
'password' => $setting->password,
'port' => $setting->port ?: '25',
'encryption' => $setting->encryption ?: 'tls',
]);

![在 Yii2 中,yii\swiftmailer\Mailer 组件中对 transport 并不是通过公开属性设置的,而是通过 setTransport() 方法设置的。而 Yii::$app->mailer->transport = [...] 属于 直接设置属性方式,PHPStorm 无法静态分析出你其实是通过 magic setter 间接调用了 setter 方法。这就是它提示 @property-read $transport 的原因。调整如下,不再报错](https://www.shuijingwanwq.com/wp-content/uploads/2025/08/2-3.jpg)