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.
| Comment | File | Size | Author |
|---|---|---|---|
| #17 | mimemail_760080.patch | 1.05 KB | svipsa |
| #10 | mimemail_760080.patch | 1.18 KB | louiswolf |
| #3 | mimemail_abs_file.patch | 503 bytes | Robbert |
Comments
Comment #1
sgabe commentedThe 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).
Comment #2
capulux commentedAs 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).
the next line is
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.txtMaybe 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.
Comment #3
Robbert commentedPrefixing 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.Comment #4
mathilde commentedHi,
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
Comment #5
mathilde commentedResolve:
$headers must be set to array() rather than NULL
Comment #6
aneuryzma commentedhi,
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);
Comment #7
kongoji commented@aneuryzma
I think you miss some info on your $attachments array.
It should work if you write it in this way:
Comment #8
aneuryzma commentedsolved. Thanks
Comment #9
sgabe commentedFirst, do not close any issue, please. Second, it would be nice if you can share the solution (about any issue not just this).
Comment #10
louiswolf commentedI've created a new patch which also adjusts the documentation in mimemail.module.
Comment #11
aneuryzma commented@sgabe: I haven't close any issue. The solution is given by the previous post, that's why I didn't add anything.
Comment #12
Robbert commented@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
listparameter you should look at #629038: Attachements dont respect ‘list’ setting.Comment #13
Robbert commentedIs it possible that louiswolf's patch (#10) could be committed? Or, alternatively, does anyone know a better solution for this problem?
Comment #14
jantoine commentedI 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
Comment #15
sgabe commentedI think this is covered in #907716: Allow non-web-accessible files as attachments, so I am marking this as a duplicate.
Comment #16
halaric commentedThe patch #10 doesn't work because "file:///" is matched by the previous test :
Comment #17
svipsa commentedThis added %20 for public/private files with spaces in filename,
and this is wrong, because later % will be replaced to %25
Attached fast fix.
Comment #18
anybodyThis may be important: #1389504: Documentation for sending emails with attachments (difference between "filepath" and "filecontent"!)
Comment #19
anybodyI 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.