MTVS
MTVS

Reputation: 2096

SwiftMailer 'ErrorException' with message 'fopen(/tmp/...) failed to open stream: Permission denied'

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);
}

Upvotes: 0

Views: 1979

Answers (1)

lowerends
lowerends

Reputation: 5267

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/

Upvotes: 1

Related Questions