2015-10-14 63 views
1

我建立我的電子郵件標題是這樣的:具體5.7.5.2 - 在哪裏放置表單文件附件標題?

$txt_message .= $this->txt_message; 
$html_message .= $this->html_message; 
$mh = Core::make('helper/mail'); 
$mh->to($this->email_to, $this->site_name); 
$mh->from($this->email, $this->name); 
$mh->replyto($this->email, $this->name); 
$mh->setSubject($this->subject); 
$mh->setBody($txt_message); 
$mh->setBodyHtml($html_message); 
@$mh->sendMail(); 

有些帖子說可以用

$mh->addAttachment($file); 

添加附件,但$文件必須是一個文件對象。我怎樣才能使上傳的文件成爲一個文件對象?

我也發現了這個帖子:http://www.adrikodde.nl/blog/2012/mail-attachments-concrete5/

,但我得到了所有的Zend東西錯誤。 Zend Mail在C5.7中仍然可用嗎?

我在哪裏爲文件附件添加標題?我在哪裏可以找到更多關於真正發送消息的東西(它是Zend郵件嗎?)以及可用的方法?

謝謝。

[解決]
感謝尼古拉,這裏有可供附加檔案工作的例子:

$file = $_FILES['image']['tmp_name']; 
$filename = $_FILES['image']['name']; 
$importer = new \Concrete\Core\File\Importer(); 
$file_version = $importer->import($file, $filename); 
$attachment = $file_version->getFile(); 
$mh->addAttachment($attachment); 

//Delete the file if not wanted on server 
$attachment->delete(); 

PS。在嘗試發送文件之前,請不要忘記檢查真正選擇/存在/上傳的文件!

if (!empty($this->image)) { 
    $importer = new \Concrete\Core\File\Importer(); 
    $image_version = $importer->import($this->image, $file_name); 
    if ($image_version instanceof \Concrete\Core\File\Version) { 
     $attachment = $image_version->getFile(); 
     $mh->addAttachment($attachment); 
    } 
} 
@$mh->sendMail(); 

回答

1

將文件添加到您的文件系統,你應該看看這個

http://concrete5.org/api/class-Concrete.Core.File.Importer.html

在返回的對象(這是成功的一個FileVersion),你應該能夠調用getFile()得到實際Concrete5 File對象

+0

尼古拉,我試圖 $文件= $ _FILES [「照片」] [ 'tmp_name的值']; $ filename = $ _FILES ['photo'] ['name']; $ importer = new \ Concrete \ Core \ File \ Importer(); $ result = $ importer-> import($ file,$ filename); $ mh-> addAttachment($ result);但它給了我一個錯誤:「傳遞給Concrete \ Core \ Mail \ Service :: addAttachment()的參數1必須是Concrete \ Core \ File \ File的一個實例,Concrete \ Core \ File \ Version實例給出了」 這個例子也在這裏:https://www.concrete5.org/documentation/developers/5.7/working-with-files-and-the-file-manager/importing-new-files/ – linuxoid

+1

明白了!得到它了!得到它了!我忘了 $ file_version = $ importer-> import($ file,$ filename); $ attachment = $ file_version-> getFile(); 現在它工作!非常感謝Nicolai對我剛剛意識到的getFile() – linuxoid

+0

的建議,即使這樣做有效,但會產生不必要的影響 - 它將所有上傳的文件保存在文件管理器中。這不是我需要的。我只需要通過電子郵件發送附件,它不應該將它保存在服務器上的任何地方。有沒有其他的方式來附加文件,沒有文件導入器?謝謝。 – linuxoid