2014-07-25 49 views
0

我已經在cpanelshared host上部署了laravel應用程序。帶有'fopen(/ tmp/...)消息的SwiftMailer'ErrorException'無法打開流:權限被拒絕'

當發送電子郵件使用Mail類時,隨機發生以下錯誤。 (有時郵件被髮送,但有時會出現錯誤)

production.ERROR: exception 'ErrorException' with message 'fopen(/tmp/e19839f1a2d67e4ab7c83a5951c31bfd/body): failed to open stream: Permission denied' in /home/ekbatana/laravel4/vendor/swiftmailer/swiftmailer/lib/classes/Swift/KeyCache/DiskKeyCache.php:300 

我聯繫了主機支持,他們說我需要更改默認的臨時目錄。

在SwiftMailer包lib/preferences.php一個名爲$tmp變量設置爲getenv('TMPDIR'),並在文件中評論說:

// You can override the default temporary directory by setting the TMPDIR environment variable. 

我試圖設置TMPDIR以不同的方式

1)的.htaccess: SetEnv TMPDIR /home/.../laravel4/app/storage/my_temp

2)in app/start/global.php and還in App::before回調函數使用php putenv函數

3)lib/preferences.php之前使用php putenv功能

但其中不改變到被打開,導致failed to open stream: Permission denied錯誤

以下文件的路徑是swiftmailer/lib/preferences.php

設置$臨時線路
<?php 

/****************************************************************************/ 
/*                   */ 
/* YOU MAY WISH TO MODIFY OR REMOVE THE FOLLOWING LINES WHICH SET DEFAULTS */ 
/*                   */ 
/****************************************************************************/ 

$preferences = Swift_Preferences::getInstance(); 

// Sets the default charset so that setCharset() is not needed elsewhere 
$preferences->setCharset('utf-8'); 

// Without these lines the default caching mechanism is "array" but this uses a lot of memory. 
// If possible, use a disk cache to enable attaching large attachments etc. 
// You can override the default temporary directory by setting the TMPDIR environment variable. 

// The @ operator in front of is_writable calls is to avoid PHP warnings 
// when using open_basedir 
$tmp = getenv('TMPDIR'); 
if ($tmp && @is_writable($tmp)) { 
    $preferences 
     ->setTempDir($tmp) 
     ->setCacheType('disk'); 
} elseif (function_exists('sys_get_temp_dir') && @is_writable(sys_get_temp_dir())) { 
    $preferences 
     ->setTempDir(sys_get_temp_dir()) 
     ->setCacheType('disk'); 
} 

// this should only be done when Swiftmailer won't use the native QP content encoder 
// see mime_deps.php 
if (version_compare(phpversion(), '5.4.7', '<')) { 
    $preferences->setQPDotEscape(false); 
} 
+0

難道託管服務提供商告訴你,你需要改變你的臨時目錄中哪個目錄? – lowerends

+0

@lowerends no,但新的臨時目錄的權限是755 – MTVS

+0

你可以調試當前設置的TMPDIR嗎?你可以使用'dd($ _ ENV ['TMPDIR'])'。 – lowerends

回答

1

您需要檢查目錄是否可以通過嘗試寫入目錄的進程寫入。您可以驗證其用戶和組是由您的過程執行:

<?php 
    echo getmyuid().':'.getmygid(); 
?> 

這會給你像user:group。然後,你需要chown目錄寫入用:

chown -R user:group writable_directory/ 
相關問題