douyingmou1389 2014-07-25 11:37
浏览 60
已采纳

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

I've deployed a laravel app on a cpanel shared host.

When sending email usign Mail class, the following error occurs randomly. (sometimes the mail is sent but sometimes the error occurs)

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

I contacted the host support they said I need to change the default temporary directory.

In SwiftMailer package lib/preferences.php a variable called $tmp is set to getenv('TMPDIR') and a comment in the file says that:

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

I tried to set the TMPDIR in different ways

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

2) in app/start/global.php and also in App::before callback function using php putenv function

3) in lib/preferences.php before the line that $temp is set using php putenv function

but non of them changes the path to the file that is opened and causes failed to open stream: Permission denied error

the following is the 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条回答 默认 最新

  • dptrmt4366 2014-07-25 13:24
    关注

    You need to check whether the directory is writable by the process trying to write to the directory. You can verify which user and group is used by your process by executing:

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

    This will give you something like user:group. Then you need to chown the directory to write to with:

    chown -R user:group writable_directory/
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建