/
sendToEmailScheduling.groovy
83 lines (68 loc) · 2.45 KB
/
sendToEmailScheduling.groovy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import net.datenwerke.rs.core.service.sendto.hooks.SendToTargetProviderHook
import net.datenwerke.rs.core.service.sendto.hooks.adapter.SendToTargetProviderHookAdapter
import net.datenwerke.rs.core.client.sendto.SendToClientConfig
import net.datenwerke.rs.core.service.mail.MailService
import net.datenwerke.rs.core.service.reportmanager.ReportExecutorService
import net.datenwerke.rs.core.service.mail.SimpleAttachment
/**
* sendToEmailScheduling.groovy
* Version: 1.0.0
* Type: Hook
* Last tested with: ReportServer 4.6.1-6096
* Allows to add an entry to the send-to menu, sending the report via email.
* Includes scheduling support as well.
* https://reportserver.net/en/guides/script/chapters/Send-To/
*
* You can place this script in onstartup.d to hook up automatically.
*/
def HOOK_NAME = 'MY_SEND_TO_EMAIL'
reportExec = GLOBALS.getInstance(ReportExecutorService)
mailService = GLOBALS.getInstance(MailService)
def doSendEmail(pdf, values) {
// prepare for sending mail
def mail = mailService.newSimpleMail()
mail.subject = 'The Report'
mail.toRecipients = values['email']
mail.from = 'from@reportserver.net'
def attachment = new SimpleAttachment(pdf.report, pdf.mimeType, 'filename.pdf')
mail.setContent('Some Message', attachment)
// send mail
mailService.sendMail mail
}
def callback = [
consumes : { report ->
def config = new SendToClientConfig()
config.title = 'Send via Custom Mail'
config.icon = 'send'
config.form = """
{
"width": 400,
"height": 180,
"form" : {
"labelAlign": "top",
"fields": [{
"id": "email",
"type": "string",
"label": "Email Address",
"value": "name@example.com"
}]
}
}
"""
return config
},
getId : {
->
return 'someUniqueId'
},
sendTo : { report, values, execConfig ->
def pdf = reportExec.execute(report, ReportExecutorService.OUTPUT_FORMAT_PDF, execConfig)
doSendEmail(pdf, values)
return "Send the report via mail. Config $values" as String
},
scheduledSendTo: { compiledReport, report, reportJob, format, values ->
def pdf = reportExec.execute(report, ReportExecutorService.OUTPUT_FORMAT_PDF)
doSendEmail(pdf, values)
}
] as SendToTargetProviderHookAdapter
GLOBALS.services.callbackRegistry.attachHook(HOOK_NAME, SendToTargetProviderHook, callback)