YiiMail发送附件

Jai*_*tus 4 yii

在我的项目中,我使用YiiMail扩展程序向用户发送邮件.我附加一个文件.但问题是无法使用附件发送邮件.我的邮件代码如下.

$this->email->setBody('<p>'.$email.'-'.$name.'-'.$details.'</p>', 'text/html');
$this->email->from = "test@test.com";
$this->email->setSubject('Direct Request');
$this->email->attach(CUploadedFile::getInstanceByName('fileupload'));
$this->email->setTo(array($emailId => 'test@test.com'));
Run Code Online (Sandbox Code Playgroud)

使用此代码,邮件不会发送并显示错误消息. 传递给Swift_Mime_SimpleMessage :: attach()的参数1必须实现接口Swift_Mime_MimeEntity,给出CUploadedFile的实例

这个错误的原因是什么,以及任何解决方案.提前致谢

tha*_*smt 8

您需要将文件附件转换为SwiftMailer Swift_Mime_MimeEntity类型.CUploadedFile::getInstanceByName('fileupload')返回一个CUploadedFile类,SwiftMailer不知道如何处理.更多关于Swift附件的信息.

我没有测试过这个,但你需要做这样的事情:

$uploadedFile = CUploadedFile::getInstanceByName('fileupload'); // get the CUploadedFile
$uploadedFileName = $uploadedFile->tempName; // will be something like 'myfile.jpg'
$swiftAttachment = Swift_Attachment::fromPath($uploadedFileName); // create a Swift Attachment
$this->email->attach($swiftAttachment); // now attach the correct type
Run Code Online (Sandbox Code Playgroud)

祝好运!