2011-10-29 58 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

非常感謝你..你救了我......多謝哥們。 –