带有消息'fopen(/ tmp / ...)的SwiftMailer'ErrorException'无法打开流:权限被拒绝'

时间:2014-07-25 11:37:08

标签: php laravel shared-hosting swiftmailer

我在laravel cpanel上部署了shared host个应用。

发送电子邮件时,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)app/start/global.php以及使用App::before函数的php putenv回调函数

3)在lib/preferences.php之前使用php putenv函数设置$ temp的行

但是它们不会更改打开的文件的路径并导致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);
}

1 个答案:

答案 0 :(得分:1)

您需要检查尝试写入目录的进程是否可写入目录。您可以通过执行以下步骤来验证进程使用的用户和组:

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

这会给你user:group之类的东西。然后,您需要chown要写入的目录:

chown -R user:group writable_directory/