I am using PHP Version 5.4.6 on 1ubuntu1.1 and I keep getting this error when using mimemail with no file set. In the code, if no file is set then $file is given the value of the content, which in this case is a string, line 180 of mimemail.inc.

  // We have the actual content.
  elseif ($content) {
    $file = $content;
  }

Later on this value for 'file' is tested using is_file(), line 311

      if (isset($part['file'])) {
        $file = (is_file($part['file'])) ? file_get_contents($part['file']) : $part['file'];
        $part_body = chunk_split(base64_encode($file), 76, variable_get('mimemail_crlf', "\n"));
      }

Clearly my version of php does not like this when 'file' is a string such as contents of pdf. I'm not sure what the solution is but clearly a test is needed to determine whether or not the string passed is a valid path. But then, what is a "valid path"?

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

NewZeal’s picture

As an immediate fix I have done the following:

Added another value to the array called file_content which is a boolean so that in _mimemail_file() it is set to true when there is no file. Then the offending error can be removed thus:

      if (isset($part['file'])) {
        if ($part['file_content']) $file = $part['file'];
        else $file = (is_file($part['file'])) ? file_get_contents($part['file']) : $part['file'];
        $part_body = chunk_split(base64_encode($file), 76, variable_get('mimemail_crlf', "\n"));        
      }
dscutaru’s picture

Something similar in beta-1

Warning: realpath() expects parameter 1 to be a valid path, string given in drupal_realpath() (line 2269 of /var/www/drupal-7/includes/file.inc).
Warning: is_file() expects parameter 1 to be a valid path, string given in mimemail_multipart_body() (line 319 of /var/www/drupal-7/sites/all/modules/mimemail/mimemail.inc).

made small changes to fix, patch:

diff --git a/mimemail.inc b/mimemail.inc
index 730d58b..a54d3f1 100644
--- a/mimemail.inc
+++ b/mimemail.inc
@@ -187,7 +187,7 @@ function _mimemail_file($url = NULL, $content = NULL, $name = '', $type = '', $d
   if (isset($file) && (@is_file($file) || $content)) {
     $public_path = file_default_scheme() . '://';
     $no_access = !user_access('send arbitrary files');
-    $not_in_public_path = strpos(drupal_realpath($file), drupal_realpath($public_path)) === FALSE;
+    $not_in_public_path = empty($content) && strpos(drupal_realpath($file), drupal_realpath($public_path)) === FALSE;
     if (@is_file($file) && $not_in_public_path && $no_access) {
       return $url;
     }
@@ -316,7 +316,7 @@ function mimemail_multipart_body($parts, $content_type = 'multipart/mixed; chars
       }
 
       if (isset($part['file'])) {
-        $file = (is_file($part['file'])) ? file_get_contents($part['file']) : $part['file'];
+        $file = (@is_file($part['file'])) ? file_get_contents($part['file']) : $part['file'];
         $part_body = chunk_split(base64_encode($file), 76, variable_get('mimemail_crlf', "\n"));
 
       }

sgabe’s picture

Issue summary: View changes
Status: Active » Needs review
FileSize
1.1 KB

Patch file attached based on #2, changing status.

Status: Needs review » Needs work

The last submitted patch, 3: mimemail-2057703-03.patch, failed testing.

pontoffeltier’s picture

Warning: realpath() expects parameter 1 to be a valid path, string given in drupal_realpath() (line 2269 of /var/www/drupal-7/includes/file.inc).
Warning: is_file() expects parameter 1 to be a valid path, string given in mimemail_multipart_body() (line 319 of /var/www/drupal-7/sites/all/modules/mimemail/mimemail.inc).

Got the same errors :-/
Anyone know a way around this? I am trying to send a PDF with this:

$file = file_create_url(file_build_uri('rrv.pdf'));
$message['params']['attachments'][] = array(
	'filecontent' => file_get_contents($file),
	'filename' => 'rrv.pdf',
	'filemime' => 'application/pdf',
	'filepath' => $file
);
pasive’s picture

@pontoffeltier your solition should be working if you apply a full path to file rrv.pdf instead of just a file name, like this

<?php
$file = file_create_url(file_build_uri('sites/default/files/custom_folder_if_you_have/rrv.pdf'));
?>

Anyway thanks for a hint helped me to get file attachments working

pontoffeltier’s picture

Isn't the file_create_url and file_build_uri taking care of the path? I checked the output and the url links correctly to the file...

pasive’s picture

Yes - true they do.

I had to spend another hour to figure out how it finally works.

At the endI wrote a blog post on this - how works correctly http://passivemanagement.net/blog/2014/01/let-drupal-send-email-attachme... . If you follow my example code then it should be working.

My example works with mimemail-7.x-1.0-beta1.

sgabe’s picture

@pasive the filecontent key is for dynamically generated files, while the filepath key is for existing files stored on the server. You should only use (the proper) one of the two.

As a comment on your blog post I would like to note that the hook_mail() implementation in the module considers file attachments as a multiline string because it handles messages sent via Rules actions provided by MimeMail itself. For more information see drupal_mail().

pasive’s picture

Thanks @sgabe, a very useful comment.
As I was really struggling to find the right way of getting this working. Now I get more clearer understanding of filecontent and filepath keys.

SpadXIII’s picture

FileSize
1.09 KB

I ran into this issue myself as well. I'm using encrypted_files module. Because mimemail changes the file-uri (with ef://.. stream wrapper) into a full file path, the files weren't decrypted anymore. I added a mail_alter in which I looped the attachments, decrypting them myself and unsetting the uri and filepath. This gave an error in the exact same locations as the above patch.

I just re-rolled it against latest dev.

SpadXIII’s picture

Version: 7.x-1.0-alpha2 » 7.x-1.0-beta3
Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, 11: mimemail-2057703-11.patch, failed testing.

g.oechsler’s picture

Status: Needs work » Needs review

11: mimemail-2057703-11.patch queued for re-testing.

Status: Needs review » Needs work

The last submitted patch, 11: mimemail-2057703-11.patch, failed testing.

SpadXIII’s picture

Seems like there's a whole lot going wrong in the tests unrelated to the patch!?

sgabe’s picture

Yep, no idea why the tests are failing.

bhavikshah9’s picture

I was facing exactly similar problem to what is mentioned in Issue, comment#2 as well as in comment#5.
I applied patch specified in comment#2 manually and everything worked fine for me. Problem is resolved now.
I checked the log to find out the reason why that patch has failed but i really didn't understood the exact reason of failure.
I am in mixed feeling right now, happy that problem is resolved but worried about that mysterious test-case in which patch has failed.

flocondetoile’s picture

Same issue et patch #11 solved it.
Thanks

martichka5’s picture

Hi,
I have experienced the same problem.
After some debugging i found out that mpdf library does not return string for file_content if we want it as string.
Doeas anybody know somethig about this?

Thanks in advance.

  • sgabe committed fb2c10a on 7.x-1.x
    Issue #2057703 by New Zeal, sgabe: Warning: is_file() expects a valid...
sgabe’s picture

Title: Warning: is_file() expects parameter 1 to be a valid path, string given in mimemail_multipart_body() line 311 » Warning: is_file() expects a valid path
Status: Needs work » Fixed
FileSize
1.75 KB

I think a little refactoring here would be a nicer solution. The attached patch has been committed.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.