Custom script to send email as HTML? (Not built in send custom email)

Jesse Ruth
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
July 19, 2017

Hi, sorry if I've missed something obvious. But basically I want to use a custom script instead of the built in custom email for more flexibility. I have used basics I've found in other posts and plan to build this up for various post functions.

My problem though is that I'm not sure how to actually get the email to send as html? I set mimetype to text/html but the email body i receive reads like

 <p>bleh</p> 

Here is the script which I'm testing in script console (also if you feel like pointing out better ways of setting up the sendEmail please do so)

import com.atlassian.mail.Email;
import com.atlassian.mail.server.MailServerManager;
import com.atlassian.mail.server.SMTPMailServer;
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.component.ComponentAccessor

subject = "test"
emailAddr = "something@something.com"
body = "&lt;p&gt;bleh&lt;/p&gt;"

def sendEmail(String emailAddr, String subject, String body) {
SMTPMailServer mailServer = ComponentManager.getInstance().getMailServerManager().getDefaultSMTPMailServer();
if (mailServer) {
Email email = new Email(emailAddr);
email.setMimeType("text/html");
email.setSubject(subject);
email.setBody(body);
mailServer.send(email);
} else {
// Problem getting the mail server from JIRA configuration, log this error
}
}

sendEmail (emailAddr, subject, body)

 

1 answer

1 accepted

2 votes
Answer accepted
Jenna Davis
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.
July 19, 2017

Hello,

I don't believe you need to use "&lt;p&gt;" and such in order to write your HTML. I was able to send an email correctly formatted as HTML using just the expected HTML code inside of the body like this:

"<p>bleh</p>" 

Sending the email with the body formatted as: 

"&lt;p&gt;bleh&lt;/p&gt;" 

caused me to recieved "<p>bleh</p>" as you had. 

Hopefully this helps you out! Let me know if this doesn't fix the problem or if you have any other questions. :)

Jenna

Jesse Ruth
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
July 20, 2017

thanks!

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events