Hi,

I tried to add attachment to my mails by filling out the attachments parameter as specified in the documentation, but this does not work. The documentation (line 64-102 in mimemail.module, why is this not explained in the README too?) states that this parameter should have the following shape.

   Array
   (
     [0] => Array
       (
         [filepath] => '/path/to/file.name'
         [filemime] => 'mime/type'
       )
   )

Unfortunately, when I use an absolute local path (that is a path referring to a file from the root of my file system, for example /var/mydata/file.jpg) it fails. So, I have been looking into the code to find out what is going wrong and stumbled upon the following, in the function mimemail_html_body the function _mimemail_file is called for each attachment. Moreover, _mimemail_file passes each path through _mimemail_url, which is supposed to do some sanitizing. Oddly, _mimemail_url strips the / from the given path because it thinks it is an absolute external path (that is path referring to a file from the root of my Drupal installation).

So, certainly there is something wrong, because the documentation does not specify whether it should be an external or local path. Either this should be made clear in the documentation, but then I am still wondering whether there is an alternative way to add an attachment given by an absolute local path, or removal of the infix / should be fixed.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

sgabe’s picture

Component: Code » Documentation

The path should be a relative Drupal path and this should be fixed in the documentation in the first place. I am changing the component, so this issue can be a reference for #614782: Update README.txt and additional documentation. As regards the attachments with absolute local path, I think it's more like a feature request, than a bug report. Specially in this status of the module (see the other issues).

capulux’s picture

As I need the absolute path for files, I played around and walked through the code. I came to the same conclusion as Robbert in the post #0:

In the function _mimemail_url the following call will remove the leading slash as long as the base_path() is '/' (which is the fact for my installation).

  $url = preg_replace( '!^'. base_path() .'!', '', $url, 1);

the next line is

  if ($embed_file) return $url;

which will return the url as is, if embed_file is set.

A quick workaround to embed files with absolute path would be to add an additional slash to the filename. For example:
//path/to/file.txt

Maybe you should check whether base_path() == '/' before adding the additional slash. This approach works fine for me.

But as this workaround is not very clean, it would be nice if we could use something like file://path/to/file.txt or just /path/to/file.txt.

Robbert’s picture

Component: Documentation » Code
Category: bug » feature
Status: Active » Needs review
FileSize
503 bytes

Prefixing paths with an extra slash sounds like something dirty on which I do not want to rely. :) Hence, I've created a patch such that it accepts absolute paths in the shape file:///path/to/file.ext. It uses three slashes so as to be consistent with http://nl2.php.net/manual/en/wrappers.file.php.

mathilde’s picture

Hi,

I succeed in sending emails, but I fail sending file attachments. I add your patch. After several try I have no more idea. Can anybody help me please?

Here's my code:

$subject = 'test email';
$body = 'Nouvelle station en attente de validation';
$plaintext = TRUE;
$headers = NULL;
$attachments = array(
'filepath' => '/data/www/drupal-6.17/sites/default/files/captures_exemple.xls',
'filemime' => 'application/vnd.ms-excel'
);

mimemail($sender, $recipient, $subject, $body, $plaintext,$headers,NULL, $attachments);

Here's the error message:

# warning: array_merge() [function.array-merge]: Argument #1 is not an array in /data/www/drupal-6.17/modules/mimemail/mimemail.module on line 137.
# warning: array_merge() [function.array-merge]: Argument #2 is not an array in /data/www/drupal-6.17/modules/mimemail/mimemail.inc on line 55.
# warning: Invalid argument supplied for foreach() in /data/www/drupal-6.17/modules/mimemail/mimemail.inc on line 59.
# warning: Invalid argument supplied for foreach() in /data/www/drupal-6.17/modules/mimemail/mimemail.inc on line 21.

Thank you

mathilde’s picture

Resolve:
$headers must be set to array() rather than NULL

aneuryzma’s picture

hi,

I've set headers to array(), but still the attachment is not there... this is my code, thanks.

header('Location: invoices/sample.pdf');

$sender = 'mycompany@company.com';
$recipient = 'myemail@email.com';
$subject = 'New order';
$body = 'Please, see the attachment.';
$plaintext = TRUE;
$headers = array();
$attachments[]=array(
'filepath' => 'invoices/sample.pdf',
'filemime' => 'mime/type',
);

mimemail($sender, $recipient, $subject, $body, $plaintext, $headers, $text = NULL, $attachments, $mailkey);

kongoji’s picture

@aneuryzma

I think you miss some info on your $attachments array.
It should work if you write it in this way:

$attachments[] = array(
  'filepath' => 'invoices/sample.pdf',
  'filemime' => 'mime/type',
  'filename' => 'sample.pdf',
  'list' => TRUE,
);
aneuryzma’s picture

Status: Needs review » Closed (fixed)

solved. Thanks

sgabe’s picture

Status: Closed (fixed) » Needs review

First, do not close any issue, please. Second, it would be nice if you can share the solution (about any issue not just this).

louiswolf’s picture

FileSize
1.18 KB

I've created a new patch which also adjusts the documentation in mimemail.module.

aneuryzma’s picture

@sgabe: I haven't close any issue. The solution is given by the previous post, that's why I didn't add anything.

Robbert’s picture

@aneuryzma: in comment #8, you have closed the issue. Also, the solution to your problem is not a solution to the original problem, namely that it is impossible to add attachments specified by absolute local paths. For the problems related to the list parameter you should look at #629038: Attachements dont respect ‘list’ setting.

Robbert’s picture

Status: Needs review » Reviewed & tested by the community

Is it possible that louiswolf's patch (#10) could be committed? Or, alternatively, does anyone know a better solution for this problem?

jantoine’s picture

Version: 6.x-1.0-alpha2 » 6.x-1.0-alpha6
Status: Reviewed & tested by the community » Needs work

I have tested the patch from #10 with 6.x-1.0-alpha6 and it does not work for me. I am creating files in the Drupal temporary directory which is set to the file systems temporary directory (/tmp) and attachments are not working. I have also tried for other directories outside the Drupal root using absolute paths and the attachments fail.

Cheers,

Antoine

sgabe’s picture

Status: Needs work » Closed (duplicate)

I think this is covered in #907716: Allow non-web-accessible files as attachments, so I am marking this as a duplicate.

halaric’s picture

The patch #10 doesn't work because "file:///" is matched by the previous test :

  // If the URL is absolute or a mailto, return it as-is.
  if (strpos($url, '://') !== FALSE || preg_match('!mailto:!', $url)) {
    $url = str_replace(' ', '%20', $url);
    return $url;
  }
svipsa’s picture

Version: 6.x-1.0-alpha6 » 7.x-1.x-dev
FileSize
1.05 KB
if (strpos($url, '://') !== FALSE || preg_match('!(mailto|callto|tel)\:!', $url)) {

This added %20 for public/private files with spaces in filename,
and this is wrong, because later % will be replaced to %25

Attached fast fix.

Anybody’s picture

This may be important: #1389504: Documentation for sending emails with attachments (difference between "filepath" and "filecontent"!)

Anybody’s picture

Issue summary: View changes
Status: Closed (duplicate) » Active

I have to re-open this issue, because it indeed still exists. I've installed the latest 7.x-1.x-dev and the file to attach is located in the temporary directory. If I provide the absolute path, no attachement is added.

By prepending a second "/" at the beginning I could quickfix this as described. (So the path is //tmp/xyz). Permissions are set correctly and this quickfix makes the difference between working and failing attachments.
Else how do I specify the file path correctly, if a relative path is impossibe?

Thanks a lot.