Workflow send mail with attachment [SOLVED]

Questions, comments, discussions. Over time certain topics might be moved to their own category.
Post Reply
merlin81
Posts: 5
Joined: Thu Jan 14, 2021 5:46 pm

Workflow send mail with attachment [SOLVED]

Post by merlin81 »

I'm looking for a way to attach the document in workflow and send it by mail. What I've found is the way to send document from view, within a workflow it does not seem to be possible. In mailer/workflow_actions.py only send method is implemented.
Can someone her give me a hint how to get the document from context within the workflow_actions.py so I can use UserMailers send_document method to attach document to mail ? Figure this out I'd try to add a checkbox to Workflow Send Mail to have it for all.
Or maybe there is another way to embed the document I have not found ?
Thanks in advance,
Christoph
merlin81
Posts: 5
Joined: Thu Jan 14, 2021 5:46 pm

Re: Workflow send mail with attachment

Post by merlin81 »

Here is a patch to get an attachment. You need to set attachment in Mail Action settings.

Code: Select all

--- apps/mailer/workflow_actions.py.bak 2021-01-17 19:50:40.128729704 +0000
+++ apps/mailer/workflow_actions.py     2021-01-17 21:21:41.903839960 +0000
@@ -50,8 +50,15 @@
                 'required': True
             }
         },
+        'attachment': {
+            'label': _('Attachment'),
+            'class': 'django.forms.BooleanField', 'default': False,
+            'help_text': _(
+                 'Set true if the document should be attached to mail.'
+            ),'required': False
+        },
     }
-    field_order = ('mailing_profile', 'recipient', 'subject', 'body')
+    field_order = ('mailing_profile', 'recipient', 'subject', 'body','attachment')
     label = _('Send email')
     widgets = {
         'body': {
@@ -72,11 +79,18 @@
         body = self.render_field(
             field_name='body', context=context
         )
-
+        attachment = self.render_field(field_name='attachment', context=context) == 'True'
         user_mailer = self.get_user_mailer()
-        user_mailer.send(
-            to=recipient, subject=subject, body=body,
-        )
+        if attachment == True:
+           document=context['document']
+           user_mailer.send_document(
+              as_attachment=True, body=body, document=document,
+              subject=subject, to=recipient
+           )
+        else:
+           user_mailer.send(
+               to=recipient, subject=subject, body=body,
+           )

     def get_form_schema(self, **kwargs):
         result = super().get_form_schema(**kwargs)
User avatar
rosarior
Developer
Developer
Posts: 630
Joined: Tue Aug 21, 2018 3:28 am
Location: Puerto Rico
Contact:

Re: Workflow send mail with attachment

Post by rosarior »

Patch added here: https://gitlab.com/mayan-edms/mayan-edm ... 5077d8788b

And will be available in the next version.

Thanks!
Post Reply