Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

How to send email using the email address that is in the custom field?

Hello All,

I have a custom field(RequerterEmail) in the issue.
I created this field because many of these users are external, they don't have access to my company's jira. They are external customers. That's why I didn't use the userpicker field.
Therefore, I would like to send email to them whenever the request for this was created and closed.
I can't get the field value (RequerterEmail) and send the email.
My code is this:
Can anyone help me?

 

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.mail.Email
import com.atlassian.jira.mail.settings.MailSettings
import com.atlassian.mail.MailException
import com.atlassian.mail.server.SMTPMailServer
import com.atlassian.plugin.util.ContextClassLoaderSwitchingUtil
import org.apache.log4j.Level
import org.apache.log4j.Logger
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.fields.CustomField

String sendEmail(String emailAddr, String subject, String body) {
def logger = Logger.getLogger(getClass())
logger.setLevel(Level.DEBUG)

// Stop emails being sent if the outgoing mail server gets disabled (useful if you start a script sending emails and need to stop it)
def mailSettings = ComponentAccessor.getComponent(MailSettings)
if (mailSettings?.send()?.disabled) {
return 'Your outgoing mail server has been disabled'
}

def mailServer = ComponentAccessor.mailServerManager.defaultSMTPMailServer
if (!mailServer) {
logger.debug('Your mail server Object is Null, make sure to set the SMTP Mail Server Settings Correctly on your Server')
return 'Failed to Send Mail. No SMTP Mail Server Defined'
}

def email = new Email(emailAddr)
email.setMimeType('text/html')
email.setSubject(subject)
email.setBody(body)
try {
// This is needed to avoid the exception about IMAPProvider
ContextClassLoaderSwitchingUtil.runInContext(SMTPMailServer.classLoader) {
mailServer.send(email)
}
logger.debug('Mail sent')
'Success'
} catch (MailException e) {
logger.debug("Send mail failed with error: ${e.message}")
'Failed to Send Mail, Check Logs for error'
}
}
//sendEmail('user@email.com', '<Your subject here>', '<Your body here>')
def RequesterEmail = 12601 as Long
//def issue = issue as MutableIssue


def customFieldManager = ComponentAccessor.getCustomFieldManager()
def UserEmailCustomField = CustomFieldManager.getCustomFieldObject(RequesterEmail)
def UserEmailValue = issue.getCustomFieldValue(UserEmailCustomField)
//def user_semail = com.atlassian.jira.component.ComponentAccessor.getCustomFieldValue(12601)

//return(UserEmailValue)

2 answers

Hello Ravi,

In fact, the example you sent me works. But as I understand very little code, I'm having difficulty implementing this code for my case.
Can you help me?

Thank you in advance

BR

0 votes

Hi @Emanuel Paquetelima 

Were you able to send an email when you hard code the email address? It would be a good idea to try that first.

What is the type of your custom field?

Ravi

Hi Ravi,

How are you?

I usually see your videos
No, I couldn't.
I'm using a Text Field(single line).
In reality my code is not doing anything.

Can you help me with this?

Like Ravi Sagar _Sparxsys_ likes this

Hello @Emanuel Paquetelima 

Thanks.

So what you can do is to make sure that email is being sent. You can find a simple working example here.

Once that works then fetch the custom field value (if it is filled in) and use that in your code.

Ravi

@Ravi Sagar _Sparxsys_ 

Hello Ravi,

In fact, the example you sent me works. But as I understand very little code, I'm having difficulty implementing this code for my case.
Can you help me?

Thank you in advance

BR

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Confluence Cloud

🏠 Say hello to the new Confluence Home!

Hi Atlassian Community, My name is DJ Chung, and I’m a Product Manager on the Confluence Cloud team. Today, I’m excited to share a new and improved version of Home. The new Home helps you ...

32,749 views 26 115
Read article

Community Events

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

Find an event

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

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you