Sending e-mail from Confluence with Groovy, ScriptRunner

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 vote
Alexey Matveev Community Champion Nov 08, 2017

Hello, 

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

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 Community Champion Nov 08, 2017

Change the wrong line to

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

and add import

import com.atlassian.confluence.mail.ConfluenceMailServerManager 

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 Community Champion Feb 13, 2018

Check if the outgoing mail is enabled. 

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

@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 Community Champion Mar 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

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

Alexey Matveev Community Champion Mar 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.

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 Community Champion Nov 09, 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? 

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.

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

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

Suggest an answer

Log in or Sign up to answer
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published May 30, 2018 in Marketplace Apps

Three tips for boosting your board's efficiency with Story Maps

Trello is one of the most effective tools for driving your sprints. It's customizable for every Agile team and product owners and Scrum masters (SM) love it. However, Agile teams often struggle with:...

840 views 2 9
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you