Swift_IoException无法打开文件以读取yii2 [英] Swift_IoException Unable to open file for reading yii2
本文介绍了Swift_IoException无法打开文件以读取yii2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在Yii2中开发一个Web应用程序。我在发送电子邮件时附加了一个文件。但是在将文件附加到电子邮件中后,我遇到了这个错误。
I am developing a web application in Yii2. I have attached a file while sending the email. But after attaching the file in email I am facing this error.
我发送带有附件的电子邮件的代码如下
My code for sending email with attachment goes like this
Yii::$app->mailer->compose()
->setFrom('sender email')
->setTo('reciever email')
->setSubject('test')
->setHtmlBody('test')
->attach('path of attachment file')
->send();
我真的遇到了一个大问题,请帮忙。
I am really facing a big problem please help.
推荐答案
根据此链接 http://www.yiiframework.com/doc-2.0/guide-tutorial-mailing.html#file-attachment ,attach()方法将文件名(字符串)作为参数。要修正您的代码,请执行以下操作:
According to this link http://www.yiiframework.com/doc-2.0/guide-tutorial-mailing.html#file-attachment , the attach() method expects the filename (string) as parameter. To fix your code:
$model->attachment = UploadedFile::getInstances($model, 'attachment');
if($model->attachment) {
$message = Yii::$app->mailer->compose()
->setFrom([ Yii::$app->user->identity->email => 'Sample Mail'])
->setTo($model->email)
->setSubject($model->subject)
->setHtmlBody($model->content);
foreach ($model->attachment as $file) {
$filename = 'emailattachments/' .$file->baseName. '.' . $file->extension; # i'd suggest adding an absolute path here, not a relative.
$file->saveAs($filename);
$message->attach($filename);
}
$message->send();
}
这篇关于Swift_IoException无法打开文件以读取yii2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文