send custom email (like jira email format ) using listener

Sneha Gole July 6, 2021

hello can anyone please give me solution to this ,

i have below information which send in mail using listener but ,

image.png

i want to send mail in below format,

image.png

 

 

1 answer

1 accepted

2 votes
Answer accepted
Ram Kumar Aravindakshan _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.
July 7, 2021

Hi @Sneha Gole,

For your requirement, if you are using the Send custom email listener, you will need to send the email using the HTML format.

To do so in the listener,  you will need to set the Email format to HTML and format the Email template using the HTML parameters as shown in the image below:-

email_listener.png

 

If possible, could you please share your listener configuration?

Thank you and Kind Regards,

Ram

Sneha Gole July 7, 2021

Hii @Ram Kumar Aravindakshan _Adaptavist_  thank you for your response .

i am using below listener 

image.png

 

and sending mail using ,

MailServerManager mailServerManager = ComponentAccessor.getMailServerManager()
SMTPMailServer mailServer = mailServerManager.getDefaultSMTPMailServer()

Email email = new Email(issue.getReporter().getEmailAddress())
email.setMimeType("text/html")
email.setTo(to)

def toCC = ""
cc.each
{
toCC = it.toString() + "," + toCC
}
log.warn toCC

// email.setCc(toCC)

// email.setSubject(issue.summary)


// email.setBody(html)
// mailServer.send(email)

Ram Kumar Aravindakshan _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.
July 7, 2021

Hi @Sneha Gole

If you are using the Custom Listener, you can try something like this:-

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.mail.Email

def issue = event.issue
def mailServerManager = ComponentAccessor.mailServerManager
def mailServer = mailServerManager.defaultSMTPMailServer
def reporter = issue.reporter
def emailAddress = reporter.emailAddress
def subject = "This is a test mail"
body = """
<html>
<body>
<h1>Subject:</h1>
<h3>Sample Content For Testing Purposes</h3>
</body>
</html>
"""

def email = new Email(emailAddress.toString())
email.setSubject(subject)
email.setBody(body)
email.setMimeType("text/html")
def threadClassLoader = Thread.currentThread().contextClassLoader
Thread.currentThread().contextClassLoader = mailServer.class.classLoader
mailServer.send(email)
Thread.currentThread().contextClassLoader = threadClassLoader

Please note, this sample code is not 100% exact to your environment. Hence, you will need to make the required modifications.

Basically, the text that you want to format should be added to the HTML body, i.e. 

body = """
<html>
<body>
<h1>Subject:</h1>
<h3>Sample Content For Testing Purposes</h3>
</body>
</html>
"""

You will need to trim the text if you want to remove the ending portion of the text. You can use the replace option in the String.

I hope this helps to solve your question. :) 

Thank you and Kind Regards,

Ram

Suggest an answer

Log in or Sign up to answer