Good day,

I need some documentation to send a mail with attachments from Rules, cause i understood that i should provide the [mime-type]:[path] in the area under the attachments label in Rules, but i only see this information [candidature:field_cand_cv] in the Replacement patterns for my content-type candidature. so i dont have the [mime-type] apparently.

Could anyone help me or provide me some documentations.

CommentFileSizeAuthor
#61 6MhKI.jpg22.81 KBvpa24
#16 Untitled3.png52.82 KBnarcisgirona
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

sgabe’s picture

Where do you see that you should provide the MIME-type? That is no longer needed in the 7.x-1.x branch, see #1305824: Leave MIME type and use only path to specify an attachment.

sletis’s picture

thx for answering,

i just tried to send an attachment directly with [candidature:field_cand_cv] which is a File field.
I didnt get any attachment in the mail i received, i tried it like that "[candidature:field_cand_cv]" and '[candidature:field_cand_cv]'
i also print the [candidature:field_cand_cv] in a system message it s providing me the link to file: http://www.mywebsite.fr/sites/default/files/cvs/mocv_2.docx
Did i miss doing something?

ps: sorry for my bad english.

sgabe’s picture

Priority: Major » Normal

You need a relative path, not an absolute URL.

sletis’s picture

ah ok thx! last question how could i make this path beeing relative?
it s the only replacement pattern to my file i have access to!

lavisrap’s picture

I have similar problem. For testing I used the relative path files/1.pdf and there is such a file in /sites/default/files, but still I receive the email without attachment.

SilviuChingaru’s picture

I use a code like this in "Attachments" value field:

$language = field_language('node', $node, 'field_document_pdf'); 
foreach($node->field_document_pdf[$language] as $file) {
  echo drupal_realpath(file_load($file['fid'])->uri);
}

and is working fine.

s1work’s picture

For the life of me I cannot figure out how to get Rules and mime mail to send out the attachment from my file field on my content type. I've tried everything, and nothing works. fiftyz I've tried your solution to no avail. I'd really love to get this working. I appreciate anyone's help in resolving this.

SilviuChingaru’s picture

@s1work plase dpm node and export your rule and post them here.

narcisgirona’s picture

Same here!! Anyone managed to get file attachments working?

narcisgirona’s picture

getting closer.. i manage to sent via absolute path and email with attachments.

on the attachment value field of rules: (make sure to put "/" on front of the path) /sites/default/files/file_attached.jpg (then you just have to have a copy of that file on this path). It works for me this way.

The problem is that if i wanna attach "dynamic" files via field_image where the user uploaded that file is when gets tricky.. i guess i should do something like:

/sites/default/files/[node:field_image] or /sites/default/files/[node:field_image:path]..

but still can't find the token or way which provides me the path or name of the user uploaded file throu field_image.

Anyone can help?

Thanks again guys

narcisgirona’s picture

did you managed to make it work?

SilviuChingaru’s picture

#6 is working fine on a production site. Did you try that code? If so and is not working post it here and I'll try to help you.

narcisgirona’s picture

To be honest have no idea where i have to implement that:

$language = field_language('node', $node, 'field_document_pdf'); 
foreach($node->field_document_pdf[$language] as $file) {
  echo drupal_realpath(file_load($file['fid'])->uri);
}

as well do i have to adapt it somehow?

narcisgirona’s picture

I found out that [node:field_image] is "printing" the url of the images: http://www.mysite.com/sites/default/files/intercanvis/logotype5.png

and if i just cut away the "domain" it works: /sites/default/files/intercanvis/logotype5.png

so i guess now i have to figure out how to adapt it somehow.

Any suggestions?

SilviuChingaru’s picture

You have to put that in the Attachment field.
Change 'field_document_pdf' with your field name and also $node ->field_document_pdf.

Your code should look like this:

$language = field_language('node', $node, 'YOUR _FIELD_NAME'); 
foreach($node->YOUR_FIELD_NAME[$language] as $file) {
  echo drupal_realpath(file_load($file['fid'])->uri);
}
narcisgirona’s picture

FileSize
52.82 KB

i did that but still not working

$language = field_language('node', $node, 'field_image');
foreach($node->field_image[$language] as $file) {  
   echo drupal_realpath(file_load($file['fid'])->uri);
}
narcisgirona’s picture

It works!

Had php module off.. my bad!!

THANKS fiftyz and everyone!!

SilviuChingaru’s picture

You're welcome. Don't forget to give back to community and help others with your knowledge ;-)

SilviuChingaru’s picture

Status: Active » Closed (fixed)
botris’s picture

You can just use the URI, no need for drupal_realpath & file_load.
Also according to the drupal_realpath API page

The use of drupal_realpath() is discouraged

This should work:

$language = field_language('node', $node, 'field_image');
foreach($node->field_image[$language] as $file) { 
   echo $file['uri']);
}

I have a field with one file max and no languages so I use:

echo $node->field_file_for_mail['und'][0]['uri'];
rhrueda’s picture

As the Attachments field says:

The mail's attachments, one file per line e.g. "files/images/mypic.png".

don´t forget to add a new line if you want to send more than one attachment.

In my case I have nodes with several pdf documents each. This is what worked for me:

$car = node_load($node->field_favourite_car['und'][0]['nid']);
foreach($car->field_car_docs['und'] as $file) {
echo ($file['uri']);
echo("\n");
}
bsandor’s picture

What php module is this guys please? ( #17 )

raamsri14’s picture

i didn't see any attachment field in rules. how to get the attachment field?

jday’s picture

#21 works for me, Thanks rhrueda!!

Valdars’s picture

Had similar problem and even code samples here didn't help me. So looked around in Mimemail module code and discovered that it can actually use Drupal uris for attachments, eg public://path/file.ext.
So what worked for me was [node:field-filefield:file:path].
PS: Rules direct input thought that file field "file" property does not have "path" property altough it really does.

spire’s picture

<?php
$language = field_language('node', $node, 'YOUR _FIELD_NAME');
foreach($node->YOUR_FIELD_NAME[$language] as $file) {
  echo drupal_realpath(file_load($file['fid'])->uri);
}
?>

Where abouts do you place this code? is it a custom field? Someone said that it was in the attachment field, where abouts is that?

sorry for so many questions, im just unsure about what with the code as there is no clear answer of where it goes.

gthing’s picture

Where is the attachment field? Would love an answer for #23 and #26. Apparently some people have been able to find such a field. Help??

Nevermind, I found it. You have to create a new html email with attachment instead of a regular email to users. It's in the menu when you create the action in rules.

Sam Moore’s picture

What worked for me was putting the following in the Attachments field of the "Send HTML Email" :

<?php

foreach($node->field_attach_file['und'] as $file) {
  echo  drupal_realpath(file_load($file['fid'])->uri);
echo ("\n");}
?>

My field is called "field_attach_file" (that's its machine name).
I allow multiple file uploads, so I have to walk the array field_attach_file['und'] in order to visit each attachment. (They're referenced as field_attach_file['und'][0], field_attach_file['und'][1], and so forth - but the foreach takes care of that).
These arrays don't have an nid key, nor a uri that I can dereference, but they do have fid's - each one is just a number (corresponding to an entry in the /media hierarchy).
I can get to the files via a browser using /media/[fid] - but that won't do for attachments.

So now I have to get the real path of the file from that fid, so as to attach it.
That's where the drupal_realpath(file_load($file['fid'])->uri) comes in - it gets the filesystem path of the asset and actually attaches it to the email.

andros’s picture

Status: Closed (fixed) » Needs work

#25 worked for me. I think this is the way to go, but the rules selector and tokens should be aware of the path property and it should be explained in the token area.
I don't see any option for "path" in the direct select mode. So the function is there but is it not exposed to the user anywhere, I think this should be fixed!

tinohuda’s picture

#26 and the similar only work for 1 file.

i attached some files and those code NOT WORKING.

anyone can help, plz. thanks before.

asimag’s picture

On Drupal 7 I am using Latest Dev of SMTP and Mime Mail. You will also need to configure SMTP and MIme Mail to work coherently. See the section Alternative Approach at: https://drupal.org/node/1200142

The following path in rules attachment input box worked perfectly:
sites/default/files/abc.png

Don't put / in front of sites folder.
Although it will send duplicate attachments. To fix that follow: https://drupal.org/node/1988482

michaelgiaimo’s picture

This worked for me. My field name is "field_white_paper." I hard-coded the directory the attachments live in, then followed it with this node token:

[node:field-white-paper:file:name]

Note the underscores have been changed to dashes. So the attachment field looks like:

/sites/default/files/white-papers/[node:field-white-paper:file:name]

Anybody’s picture

Have a look here, if it's a problem with a newer version of mimemail.module than alpha2: #1389504: Documentation for sending emails with attachments

bennett_pr’s picture

If, on the off chance, you're also trying to do this using EntityForms, this structure worked for me in the 'Attachments' textarea for the rule:

To attach files:

sites/default/files/[entityform:FORM_FIELD_NAME:file:name]

To attach images:

[entityform:FORM_FIELD_NAME]

lindsay.wils’s picture

Issue summary: View changes

Im trying to get this working with a private file and have tried every option here to no avail! Has anyone managed to get this working with private files? Or im just doing something else wrong.... please help anyone :)

defconjuan’s picture

I've tried the following formats in the attachment field and none work:

  • [registration:field-anon-resume:file:path]
  • [registration:field-anon-resume:file:name]
  • private://file.txt
  • /var/www/acme/sites/default/files_private/file.txt
  • http://acme.com/system/files/file.txt
  • /sites/default/files_private/file.txt
  • sites/default/files_private/file.txt
  • /system/files/file.txt
  • system/files/file.txt

I used the following code in the message body before adding it to the attachments field; so that I could see what would be echoed when the same code was used in the attachment field.

<?php
foreach($registration->field_anon_resume['und'] as $file) {
//  echo  drupal_realpath(file_load($file['fid'])->uri); // returns /var/www/acme/sites/default/files_private/file.txt
//  echo  file_load($file['fid'])->uri; // returns private://file.txt
  echo file_create_url(file_load($file['fid'])->uri); // returns http://website.com/system/files/file.txt
  echo ("\n");
  }
?>	

Nothing. Absolutely nothing and no error log messages. The email comes through fine, but there are no attachments. I even tried putting a sample file in the sites/default/files (eg. public file system) and used manual entries replicating all the file patterns above and no attachments went through.

Any current advice on this would be appreciated because not being able to send attachments with Rules and MimeMail HTMLMail is a blocker.

defconjuan’s picture

Title: Unfortunately we can not provide Wow Gold » Send attachments via Rules
defconjuan’s picture

Issue tags: -Wow Gold
sgabe’s picture

Currently by default you should be able to use files located in the public file system (e.g. sites/default/files).

  • public://example.jpg
  • sites/default/files/example.jpg

With the "Send arbitrary files" permission you should be able to send files located outside the public file system. Note that this means every other locations, your private file system (e.g. /var/private) and even your settings.php!

  • private://example.jpg
  • sites/default/settings.php
  • /var/private/example.jpg
sgabe’s picture

Status: Needs work » Active
defconjuan’s picture

We are not running Drupal in a subfolder, but the:

"Send arbitrary files"

permission may be the issue. But it sounds like this is a security issue. For example, let's say you have an anonymous job application page where someone can submit their resume; in this instance you'd have to give the "Send arbitrary files" permission to "Anonymous" users. Doesn't that open huge security holes?

my-family’s picture

Version: 7.x-1.x-dev » 7.x-1.0-beta3

@sgabe: according to my testing, the new version of Mime Mail is not able to send files outside the drupal root. However, the old version (7.x-1.0-alpha1+20-dev) was, without problems.

The path is the same:

home/domains/[DOMAIN]/[PRIVATE-FOLDER]/[SUBFOLDERS(S)]/test.doc

The permissions settings ("Send arbitrary files") don't make any difference.

It is absolutely necessary for us to send private attachments. What would you recommend? Is it possible to stay with the old version? I see "security update" in the modules updates list but no security issue for Mime Mail in that period...

Thank you in advance for your advice.

my-family’s picture

Version: 7.x-1.0-beta3 » 7.x-1.x-dev

... and the latest -dev version (7.x-1.0-beta3+13-dev) does at least something: it sends an attachment.dat file with the file address:

https://[SUBDOMAIN]/home/domains/[DOMAIN]/[PRIVATE FOLDER]/[SUBFOLDER(S)]/test.doc

which is wrong.

When the right address is hardcoded via PHP, it sends the attachment.dat file with the right address:

https://[SUBDOMAIN]/system/files/[SUBFOLDER(S)]/test.doc

However, it does not send the attachment.

defconjuan’s picture

As a work-around, we ended up creating a function that works with the "Send HTML e-mail" rule action, attachments field:

<?php
function vtl_render_file_attachment($resume) {
  // $registration->field_resume['und'][0]['fid']
  $return = "";
  if(!empty($resume)) {
    $fid = $resume;
    $fileobj  = file_load($fid);
    $filename = $fileobj->filename;
    $filesize = $fileobj->filesize;
    $uri      = $fileobj->uri;
    $url_array = explode("://", $uri);

    // $return = "sites/default/files_private/".$url_array[1];
    $return = variable_get('file_private_path')."/".$url_array[1];
  } 
  return $return; 
}
?>

And in the "attachments" field of the "Send HTML e-mail" rule action:

<?= vtl_render_file_attachment($registration->field_resume['und'][0]['fid']); ?>

In the 2nd code block, you would replace $registration->field_resume['und'][0]['fid'] with the token for your entity and file field.

my-family’s picture

@vtldevelops: do you have the private file path outside (above) the drupal root?

defconjuan’s picture

Nope, it's /sites/default/files_private.

skorzh’s picture

#25 works for me, thanks a lot!

Nick Bell’s picture

You can enable multiple attachments simply by appending a newline after each file:

<?php
$language = field_language('node', $node, 'field_attachments'); 
foreach($node->field_attachments[$language] as $file) {
  echo drupal_realpath(file_load($file['fid'])->uri) . "\n";
}
?>
Cyclonecode’s picture

#45 work fine for me using Drupal 7-36 and Mime Mail 7.x-1.0-beta3 in order to attach private files.

presleyd’s picture

If your private files are under the web root they aren't private. /sites/default/whatever is NOT a private file.

Anonymous’s picture

The following code should work properly. The file uri is enough however, "\n" needs to be replaced with "\r\n".

This worked for me with an Acquia Drupal installation running Drupal 7.34 along with Mail System 7.x-2.34 and Mime Mail 7.x-1.0-beta3.

<?php
$language = field_language('node', $node, 'field_attachments');
foreach($node->field_attachments[$language] as $file) {
  echo file_load($file['fid'])->uri . "\r\n";
}
?>
idea_vasil’s picture

#26 worked for me. Tnx @spire

penone’s picture

Am trying to use #25 [node:field-filefield:file:path] from a field in a webform that is nested in a fieldset but can't figure out what the correct machine name would be.

For my example the input html is:

<input class="form-control form-file" type="file" id="edit-submitted-machinery-list-upload-file-0-upload" name="files[submitted_machinery_list_upload_file_0]" size="22">

mlodymicky’s picture

For testing I used the path: sites/default/webform/files/1.jpg and it works. But I need recieve uploaded file by users. I have file field to upload different file types in my webform module. I'm neewbe so tell me how to get it work.

delxpez’s picture

#25 worked for me.

nimek’s picture

[node:field-filefield:0:file:path]
[node:field-filefield:1:file:path]
[node:field-filefield:2:file:path]

Worked for me in multivalue field case

nerdcore’s picture

Priority: Normal » Major

I'm using Mime Mail 7.x-1.0-beta3 with Drupal 7.57 and unable to attach private files.

Marking this Major, as using the public files directory is not an option in my case; The attachment required is a Commerce Invoice and should never be seen by anyone other than the intended recipient.

Using public://filename.pdf works fine, but using private://filename.pdf generates the PDF and Mime Mail fails to attach it.

I'll try working through reply #45 but would be great to see this fixed in the module itself.

nerdcore’s picture

Priority: Major » Normal

For my purposes the patch provided by #2624516: Attach non-public files resolves this issue in a way which addresses my security concerns (anonymous should probably not be granted broad access to the entire private files directory) ad allows the Rule firing as Anonymous to attach from locations specified within the MIME Mail module configuration for the site.

If that patch is committed, then perhaps this issue can be marked Closed (Duplicate).

TR’s picture

Status: Active » Closed (outdated)

Closing this again ... the original poster's support question was answered 6 years ago and the issue was closed at that point.

Subsequent posts are all over the place talking about different problems with different versions of Rules.

Some of the more recent posts are about attaching private files - that is a feature request which is being addressed in #2624516: Attach non-public files.

If you have problems sending file attachments via the Rules action provided by Mime Mail, then please do not re-open this issue. First, READ this issue to see if any of the above applies to you, and if nothing here helps then open a NEW support request with details about what you are trying to do and what is happening.

vpa24’s picture

FileSize
22.81 KB

How to send multiple images file in Rule module?

lubwn’s picture

#57 worked for me.

alpha-1’s picture

Hi,

I am using rules Version: 7.x-2.13 when I send a file as attachment greater than 8MB it fails. max php upload and post_max_size is 20MB

what am I missing?

Drupal:7.87

Thanks

jaspm2004’s picture

#28 working for me
#31 thanks! I was having the duplicate issue too