#1 2019-07-15 21:46:30

jheadlee
Member
Registered: 2019-07-15

Issue with Dynamic Script to send emails to users dynamically

If there is a better option for this, let me know. This was 90% of why we bought this platform. We have a lot of users that we pull from our ERP that need to get daily reports. So I pull from the DB of the ERP and then call URLs with CURL, or that is the plan.

However it's running and showing me the results, but no report is ran or emailed. I have tested email.cf, all scheduled or UI based reports work perfectly so I know the config is solid.

Here is my script:

import java.util.HashMap;

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.mail.SimpleAttachement;
import net.datenwerke.rs.core.service.mail.SimpleMail;
import net.datenwerke.rs.core.service.reportmanager.ReportExecutorService;
import net.datenwerke.rs.core.service.reportmanager.ReportService;
import net.datenwerke.rs.core.service.reportmanager.engine.CompiledReport;
import net.datenwerke.rs.core.service.reportmanager.engine.config.ReportExecutionConfig;
import net.datenwerke.rs.core.service.reportmanager.entities.reports.Report;
import net.datenwerke.rs.core.service.reportmanager.exceptions.ReportExecutorException;
import net.datenwerke.rs.core.service.sendto.hooks.SendToTargetProviderHook;
import net.datenwerke.rs.scripting.service.scripting.scriptservices.CallbackRegistry;
import net.datenwerke.scheduler.service.scheduler.entities.AbstractJob;
import net.datenwerke.security.service.authenticator.AuthenticatorService
import java.util.HashMap
import groovy.xml.*

 
// define outputFormat if not already in binding
//if(! binding.hasVariable('outputFormat'))
//outputFormat = null

def HOOK_NAME = "MY_SEND_TO"
def user = GLOBALS.getRsService(AuthenticatorService.class).getCurrentUser()

reportService = GLOBALS.getInstance(ReportService.class)
reportExec = GLOBALS.getInstance(ReportExecutorService.class)
mailService = GLOBALS.getInstance(MailService.class)

String doSendMail() {
 
  try {

        Report extraReport = reportService.getReportById(args[1]);
        CompiledReport extraReportCompiled = extraReportCompiled = reportExec.execute(extraReport, "PDF", ReportExecutionConfig.EMPTY_CONFIG);

        SimpleMail mail = mailService.newSimpleMail();
        mail.setSubject("Test report");
        mail.setToRecipients(args[0]);
        mail.setFrom("ouremail@foo.bar");

        SimpleAttachement extraReportAttachement = new SimpleAttachement(extraReportCompiled.getReport(),
                extraReportCompiled.getMimeType(), "extraReport.pdf");
        // Attach the files
        mail.setContent("Some text", extraReportAttachement);

        // send mail
        mailService.sendMail(mail);

        return "Sent the report via mail.";
   
    } catch (ReportExecutorException e) {
            e.printStackTrace();
    }
}

def writer = new StringWriter()

new MarkupBuilder(writer).html {
   head {
     title ( 'Hello World' )
   }
   body {
     h1(style: 'color: #f00', "Hello ${user.getFirstname()}")
     p("Isn't that an easy way to create a report?")
    p('Arguments: ' + args)

    p('Email: ' + args[0])
    p('Report: ' + args[1])
//    p('Extra: ' + args[2])
     
   }
}


return writer.toString()


doSendMail()
return writer.toString()

Here is the URL I'm trying to bulk send to ( I will handle race condition with pauses in the emails so don't worry about the server once it works :> )

http://ourserver.com/reportserver/repor … %201153557

Here is reportserver folder results :

reportserver$ ls -l
4619    lib    FileServerFolder
1178717    emailReport.rs    FileServerFile
^^^^^--- that is correct

Here is report folder:

reportserver$ ls -l /reportmanager/BOSS/Support/
1064045    Pending Tickets Report    JasperReport
1153557    DailyUserReport    JasperReport
^^^^--- this is what is passed in with args for email %20 ReportID so we can make this dynamic as we add reports...

Can haz help?

I get no error on exec or web page, but I also get no report, and netstat -an done repeatedly shows no attempt to email. Again if I do scheduled report or manual->Send_To->email, all works flawlessly...

Thanks in advance.

Offline

#2 2019-07-16 09:19:11

jalbrecht
Administrator
Registered: 2016-10-21

Re: Issue with Dynamic Script to send emails to users dynamically

Dear John,

We’ll have a look, just one question: does the script work when you run it in the terminal in commit mode (-c option) ?

wbr Jan

Offline

#3 2019-07-16 15:06:31

jheadlee
Member
Registered: 2019-07-15

Re: Issue with Dynamic Script to send emails to users dynamically

Restarted the app, same results. Manual report -> Email works, script doesn't error, spits back HTML code, seems to run even with try / catch, no results other than what seems like it's working. And no report shows up in the inbox.

This all worked before the upgrade, so not sure if that helps, what logs can I check to see what is going on?

Offline

#4 2019-07-16 15:25:15

jheadlee
Member
Registered: 2019-07-15

Re: Issue with Dynamic Script to send emails to users dynamically

I think I found it, let me test and I will respond, for now don't do anything with this... Seems like an o365 mail policy, will share if it works :>

Offline

#5 2019-07-16 16:03:10

jheadlee
Member
Registered: 2019-07-15

Re: Issue with Dynamic Script to send emails to users dynamically

No luck, I was able to get o365 to complain when I used a made up email, some policy requires the SMTP address to exist. So I change it to mine. Nada. It's like it goes to the ether. Where can I get logs for this? I see no errors, I am having my IT staff look at it, but it would be helpful to know RS at least got a 200 and thinks things are copasetic. With no logs I'm flying blind. Let me know how we can move this forward, it's critical to get these reports out as they were all working before going to 3.0.5 which fixed another issue and seemingly broke email.

Offline

#6 2019-07-17 14:46:46

Rodion
Member
Registered: 2017-05-17

Re: Issue with Dynamic Script to send emails to users dynamically

Dear John,

please look at this snippet of code :

 
return writer.toString()


doSendMail()
return writer.toString()

if you first return, than your rest of code wouldn't be executed.

If we assume that this is an paste error, then let's continue.

First of all, please, specify explicit your RS-version.
From your last comment i assume you are trying with RS 3.0.5.

i imitated youre issue, and, in fact, the exactly code you provided doesn't work.
I simplified it a bit and got it to work :

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.mail.SimpleAttachement;
import net.datenwerke.rs.core.service.mail.SimpleMail;
import net.datenwerke.rs.core.service.reportmanager.ReportExecutorService;
import net.datenwerke.rs.core.service.reportmanager.ReportService;
import net.datenwerke.rs.core.service.reportmanager.engine.CompiledReport;
import net.datenwerke.rs.core.service.reportmanager.engine.config.ReportExecutionConfig;
import net.datenwerke.rs.core.service.reportmanager.entities.reports.Report;
import net.datenwerke.rs.core.service.reportmanager.exceptions.ReportExecutorException;
import net.datenwerke.rs.core.service.sendto.hooks.SendToTargetProviderHook;
import net.datenwerke.rs.scripting.service.scripting.scriptservices.CallbackRegistry;
import net.datenwerke.scheduler.service.scheduler.entities.AbstractJob;
import java.util.HashMap
 
reportService = GLOBALS.getInstance(ReportService.class)
reportExec = GLOBALS.getInstance(ReportExecutorService.class)
mailService = GLOBALS.getInstance(MailService.class)

        Report extraReport = reportService.getReportById(Long.parseLong(args[1]));
        CompiledReport extraReportCompiled = extraReportCompiled = reportExec.execute(extraReport, "PDF", ReportExecutionConfig.EMPTY_CONFIG);

        SimpleMail mail = mailService.newSimpleMail();
        mail.setSubject("MySubject");
        mail.setToRecipients(args[0]);
        mail.setFrom("from@reportserver.net");

        SimpleAttachement extraReportAttachement = new SimpleAttachement(extraReportCompiled.getReport(),
                extraReportCompiled.getMimeType(), "extraReport.pdf");

        // Attach the files
        mail.setContent("Some text", extraReportAttachement);

        mailService.sendMail(mail);
 
        return "Send the report via mail"

One of the possibilities of tracing "what's going on" are the tomcat logs.
Depending on your installation they are in "TOMCAT_DIR/logs".

Generally, there are many options sending an email via script.

If you have further questions, please don't hesitate to ask.

Best regards,
Rodion

Offline

#7 2019-07-23 19:43:40

jheadlee
Member
Registered: 2019-07-15

Re: Issue with Dynamic Script to send emails to users dynamically

Wanted to share in case anyone else has o365 and runs into the same issue.

What it ACTUALLY turned out ( figured out by sending mail to postfix and then o365... ) is that they have a policy that they will not allow emails from a SMTP address that doesn't exist. So boss-reports@ourdomain.com didn't exist so it was dying there, no issues with the code itself.

SMDH, Microsoft SSRS was the reason I went to ReportServer, and they are still getting me :>

Offline

#8 2019-07-29 06:59:59

eduardo
Administrator
Registered: 2016-11-01
Website

Re: Issue with Dynamic Script to send emails to users dynamically

Hi jheadlee,

thanks for letting us know!

Regards,
Eduardo

Offline

Board footer

Powered by FluxBB