Application ‘A’ is running:
- minimum-stability": “dev”
- yiisoft/yii2": ~2.0.45
- yiisoft/yii2-symfonymailer": "3.0.x-dev
Application ‘B’ is running:
- minimum-stability": “stable”
- yiisoft/yii2: “~2.0.45”
- yiisoft/yii2-symfonymailer": “^3.0”
Both applications are running on freshly installed yii2-app-basic, using composer to install and update. Both applications are using the same configuration, shown below:
'mailer' => [
'class' => \yii\symfonymailer\Mailer::class,
'transportFactory' => app\utils\CustomSmtpFactory::class,// causes the type error.
'viewPath' => '@app/mail',
// send all mails to a file by default.
'useFileTransport' => true,
'transport' => [
'scheme' => 'smtp',
//other settings
'dsn' => 'native://default',
],
],
The error:
TypeError
Cannot assign string to property yii\symfonymailer\Mailer::$transportFactory of type ?Symfony\Component\Mailer\Transport
Both yii2-SynfonyMailer work until I try the transportFactory, and only use the code given as a example from the readme for the timeout option. The CustomSmtpFactory.php code that is saved in ‘app/utils’
<?php
namespace app\utils; //file is in utils folder of your application
use Symfony\Component\Messenger\Transport\TransportFactoryInterface;
class CustomSmtpFactory implements TransportFactoryInterface {
public function __construct(private TransportFactoryInterface $factory, private float $timeout)
{
$this->timeout = 120;
}
public function create(Dsn $dsn): TransportInterface
{
$result = $this->factory->create($dsn);
if ($result instanceof SmtpTransport) {
//Setup timeout to this or
$result->getStream()->setTimeout($this->timeout);
}
return $result;
}
public function supports(Dsn $dsn): bool {
return $this->factory->supports($dsn);
}
}
SiteController.php
public function actionContact()
{
$model = new ContactForm();
if ($model->load(Yii::$app->request->post()) && $model->contact(Yii::$app->params['adminEmail'])) {
Yii::$app->session->setFlash('contactFormSubmitted');
return $this->refresh();
}
return $this->render('contact', [
'model' => $model,
]);
}
ContactFoam.php
<?php
namespace app\models;
use Yii;
use yii\base\Model;
/**
* ContactForm is the model behind the contact form.
*/
class ContactForm extends Model
{
public $name;
public $email;
public $subject;
public $body;
public $verifyCode;
/**
* @return array the validation rules.
*/
public function rules()
{
return [
// name, email, subject and body are required
[['name', 'email', 'subject', 'body'], 'required'],
// email has to be a valid email address
['email', 'email'],
// verifyCode needs to be entered correctly
['verifyCode', 'captcha'],
];
}
/**
* @return array customized attribute labels
*/
public function attributeLabels()
{
return [
'verifyCode' => 'Verification Code',
];
}
/**
* Sends an email to the specified email address using the information collected by this model.
* @param string $email the target email address
* @return bool whether the model passes validation
*/
public function contact($email)
{
if ($this->validate()) {
Yii::$app->mailer->compose()
->setTo($email)
->setFrom([Yii::$app->params['senderEmail'] => Yii::$app->params['senderName']])
->setReplyTo([$this->email => $this->name])
->setSubject($this->subject)
->setTextBody($this->body)
->send();
return true;
}
return false;
}
}