Skip to main content
Example scripts
arrow icon
To homepage
Confluence
Data centre icon
Data Center

Send Custom Email for Confluence

Created 1 year ago, Updated 1 month(s) ago
App in script
ScriptRunner For Confluence
ScriptRunner For Confluence
by Adaptavist
Compatibility
compatibility bullet
Confluence (7.15 - 8.6)
compatibility bullet
ScriptRunner For Confluence (7.10.0)
Language |
groovy
import com.atlassian.confluence.mail.ConfluenceMailServerManager
import com.atlassian.mail.Email
import com.atlassian.mail.MailException
import com.atlassian.mail.server.SMTPMailServer
import com.atlassian.plugin.util.ContextClassLoaderSwitchingUtil
import com.atlassian.sal.api.component.ComponentLocator

String sendEmail(String emailAddr, String subject, String body) {
    def mailServerManager = ComponentLocator.getComponent(ConfluenceMailServerManager)
    def mailServer = mailServerManager.defaultSMTPMailServer

    if (!mailServer) {
        log.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)
        }
        log.debug('Mail sent')
        'Success'
    } catch (MailException e) {
        log.error("Send mail failed with error: ${e.message}")
        'Failed to Send Mail, Check Logs for error'
    }
}

sendEmail('<YOUR_EMAIL_ADDRESS>', '<MAIL_SUBJECT>', '<MAIL_BODY>')
Having an issue with this script?
Report it here