Hi all,
I have come to a bit of a dead end with a script I'm working on. The purpose is to automatically create a pdf export file from a specific page and e-mail it. I would configure and save the script as a scheduled job, but first I need to get the script itself working. I have added my current version of it below, and would appreciate any help in regards to understanding what is wrong here.
import java.io.File;
import java.io.FileOutputStream;
import java.net.URL;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;
import java.nio.file.Path;
import java.nio.file.Paths;
import com.atlassian.confluence.pages.PageManager
import com.atlassian.sal.api.component.ComponentLocator
import com.atlassian.mail.Email;
import com.atlassian.mail.server.SMTPMailServer;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Multipart;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMultipart;
URL website =
new URL("https://<confluence URL>/spaces/flyingpdf/pdfpageexport.action?pageId=<page ID number>");
ReadableByteChannel rbc = Channels.newChannel(website.openStream());
FileOutputStream fos = new FileOutputStream("export.pdf");
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
Path p1 = Paths.get("export.pdf");
File attachment = p1.toFile();
SMTPMailServer mailServer = PageManager.getInstance().getMailServerManager().getDefaultSMTPMailServer();
if (mailServer != null) {
Email email = new Email("<e-mail address goes here>");
email.setSubject("test");
email.setBody("test");
Multipart multipart = new MimeMultipart();
//set attachment
MimeBodyPart attachmentBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(attachment);
attachmentBodyPart.setDataHandler(new DataHandler(source));
attachmentBodyPart.setFileName(attachment.getName());
attachmentBodyPart.setContent("hello world", "text/plain;charset=utf-8");
multipart.addBodyPart(attachmentBodyPart);
email.setMultipart(multipart);
mailServer.send(email);
} else {
throw new RuntimeException("no mail server!!!");
}
Thank you in advance for any input :)
Best regards,
Margaret
Hi,
Thanks - I changed/added the lines as you described and received no errors :)
However, the result didn't exactly come out as planned, added the screenshot of it:
I think the problem is with the following line
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE)
Jira can not access the resource. Are you sure that the resource https://<confluence URL>/spaces/flyingpdf/pdfpageexport.action?pageId=<page ID number> is reachable from the Jira server?. Does the page allow anonymous access?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi,
Apologies for not answering sooner! I have been testing the code both ways (when the page is accessible to anonymous users and not), but the result is still the same for both - receiving the no route to host error. Since page export is also included, I provided the anonymous with permissions to see and export the space, re-indexed just to be sure, but still no result, unfortunately.
I have been working with Script Runner for Confluence, the script is also run/tested from Confluence - if it's possible/better/easier to obtain this functionality by using ScriptRunner for Jira, I could look into that.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
"no route to host" is a network error, it means your machine that the code is running on cannot reach the server over the network.
I'd want to check a call to http://<confluence URL> on the command line with curl or wget to ensure the machine can see it, assuming it is that (the error might be "can't see the mail server" as well, but the line number was around the opening of the confluence url)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi,
Thank you for input and information :) I'll check with the sysadmin.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hello,
What is the error? Or no errors and it is just not working?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi,
Attached a screenshot of the error on line 29 (that's the only error I get), this seems to be the faulty line:
SMTPMailServer mailServer = PageManager.getInstance().getMailServerManager().getDefaultSMTPMailServer();
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Change the wrong line to
def confluenceMailServerManager = ComponentLocator.getComponent(ConfluenceMailServerManager)
SMTPMailServer mailServer = confluenceMailServerManager.getDefaultSMTPMailServer();
and add import
import com.atlassian.confluence.mail.ConfluenceMailServerManager
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @Alexey Matveev I took the code for sending email only and the email is not sent when I run the code from scriptrunner console and from the result it is null.
What might be the problem?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Check if the outgoing mail is enabled.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@Alexey Matveev I want to take the email functionality further so that when a user has signed up he/she receives a welcome email.
How can I get the email of the user who has signed up so that I can place it in the 'signedupuseremail' as below:
Email email = new Email("<signedupuseremail>");
I am trying to use script runner and I have selected the user create event now the challenge is getting the email of the user who has signed up and setting it in the 'signedupuseremail'
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I saw your queston. I could not find this event in Jira, that is why I did not answer.
You could do it by the answer in the thread below, but you would need to develop a plugin with a servletfileterpluginmodule
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
It does not matter. The way to solve is the same. You should find the event. If it does not exist, then you should write your own serverfilterpluginmodule to generate the event.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.