title: Jira Script Runner - Mail is not sent by Post Function of Create Transition
body: <p>we have a Project in Jira which we use as an inbox for Email. Not all people sending emails are users in JIRA (and they shall not be). Nevertheless, we would like to inform then on having received the Email. The emailaddress is part of the Issue description.</p>
  
  <p>I am aware of some plugins out there but instead of replacing the Mailhandlers, I am trying to write a groovy script for JIRA adapting <a href="https://jamieechlin.atlassian.net/wiki/display/GRV/Post+Functions#PostFunctions-SendEmailWhenBlockerReported">this code</a> which I want to post into a Post Function on the CREATE transition of a workflow.</p>
  
  <p>The following code works fine when I grab an existing Test-Issue and run the script in the console:</p>
  
  <pre><code>import com.atlassian.jira.ComponentManager
  import com.atlassian.jira.issue.Issue
  import com.atlassian.jira.issue.IssueManager
  import com.atlassian.mail.Email
  import com.atlassian.mail.server.MailServerManager
  import com.atlassian.mail.server.SMTPMailServer
  
  ComponentManager componentManager = ComponentManager.getInstance()
  MailServerManager mailServerManager = componentManager.getMailServerManager()
  SMTPMailServer mailServer = mailServerManager.getDefaultSMTPMailServer()
  
  if (mailServer) {
      if (true) {  
  
          IssueManager issueManager = componentManager.getIssueManager()    
          Issue issue = issueManager.getIssueObject("IN-376")
  
          def grabEmail = {
              (((it.split(   "\\[Created via e-mail received from:")[1]).split("&lt;")[1]).split("&gt;")[0])
          }
          String senderAddress = grabEmail("${issue.description}")
  
          Email email = new Email(senderAddress) 
          email.setSubject("JIRA Ticket erstellt: ${issue.summary}") 
          String content = "Content ----&gt; by Issue2 ${issue.description}"
          email.setBody(content)
          mailServer.send(email)
  
  
      }
  }
  </code></pre>
  
  <p>Alas, it will not run in the Post Function like this:</p>
  
  <pre><code>import com.atlassian.jira.ComponentManager
  import com.atlassian.jira.issue.Issue
  //import com.atlassian.jira.issue.IssueManager
  import com.atlassian.mail.Email
  import com.atlassian.mail.server.MailServerManager
  import com.atlassian.mail.server.SMTPMailServer
  
  ComponentManager componentManager = ComponentManager.getInstance()
  MailServerManager mailServerManager = componentManager.getMailServerManager()
  SMTPMailServer mailServer = mailServerManager.getDefaultSMTPMailServer()
  
  if (mailServer) {
      if (true) {  
  
          //IssueManager issueManager = componentManager.getIssueManager()    
          //Issue issue = issueManager.getIssueObject("IN-376")
  
          def grabEmail = {
              (((it.split(   "\\[Created via e-mail received from:")[1]).split("&lt;")[1]).split("&gt;")[0])
          }
          String senderAddress = grabEmail("${issue.description}")
  
          Email email = new Email(senderAddress) 
          email.setSubject("JIRA Ticket erstellt: ${issue.summary}") 
          String content = "Content ----&gt; by Issue2 ${issue.description}"
          email.setBody(content)
          mailServer.send(email)
  
  
      }
  }
  </code></pre>
  
  <p>I have no idea why the second code breaks since the code this is based on uses issue also as if it is implicitly defined. This Post function is the last to run.</p>
  
  <p>I would also find hints as to debugging this problem helpful.</p>
  
  <p>Thank you!</p>