2011-10-29 67 views
4

在我的项目中,我使用YiiMail扩展将邮件发送给用户。我在附上一个文件。但问题是无法使用附件发送邮件。我的邮件代码如下。YiiMail发送附件

$this->email->setBody('<p>'.$email.'-'.$name.'-'.$details.'</p>', 'text/html'); 
$this->email->from = "[email protected]"; 
$this->email->setSubject('Direct Request'); 
$this->email->attach(CUploadedFile::getInstanceByName('fileupload')); 
$this->email->setTo(array($emailId => '[email protected]')); 

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

是什么原因,这个错误是展示和这方面的任何解决方案。 在此先感谢

回答

8

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

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

$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 

祝你好运!

+0

非常感谢你..你救了我......多谢哥们。 –