Sending e-mail from Confluence with Groovy, ScriptRunner

Margaret Till November 8, 2017

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

2 answers

0 votes
Margaret Till November 9, 2017

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:

error_result.png

Alexey Matveev
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 9, 2017

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? 

Margaret Till November 16, 2017

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.

Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 16, 2017

"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)

Margaret Till November 16, 2017

Hi,
Thank you for input and information :) I'll check with the sysadmin.

0 votes
Alexey Matveev
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 8, 2017

Hello, 

What is the error? Or no errors and it is just not working?

Margaret Till November 8, 2017

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();
error_line_29.png

Alexey Matveev
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 8, 2017

Change the wrong line to

def confluenceMailServerManager = ComponentLocator.getComponent(ConfluenceMailServerManager)
SMTPMailServer mailServer = confluenceMailServerManager.getDefaultSMTPMailServer();

and add import

import com.atlassian.confluence.mail.ConfluenceMailServerManager 
Like # people like this
Ian February 13, 2018

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? 

Alexey Matveev
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 13, 2018

Check if the outgoing mail is enabled. 

Ian February 13, 2018

Thanks @Alexey Matveev  I have correctly configured the email and now it is being sent.

Ian March 27, 2018

@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'

Alexey Matveev
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
March 27, 2018

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

Ian March 27, 2018

The new users are signing up on confluence and not jira.The event is on script runner for confluence.

Alexey Matveev
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
March 27, 2018

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.

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events