Uploaded image for project: 'Jenkins'
  1. Jenkins
  2. JENKINS-30854

Support AWS SES Message IDs for In-Reply-To header

    • Icon: Improvement Improvement
    • Resolution: Fixed
    • Icon: Minor Minor
    • email-ext-plugin
    • None

      The Amazon Simple Email Service (AWS SES) rewrites the Message-ID header of outgoing emails. That means subsequent failure/success notifications will not be in the same thread, because they reference a non-existing message id in the In-Reply-To header.

      The rewritten message id is returned as last message of the SMTP transaction, e.g.

      250 Ok <00000123456abcde-1234abcd-abcd-1234-1234-1234abcd1234-000000@eu-west-1.amazonses.com>
      

      http://docs.aws.amazon.com/ses/latest/DeveloperGuide/smtp-response-codes.html

      I have written a proof-of-concept patch that updates the rewritten message id in case AWS SES is used for sending mails.

      Would you be willing to accept such a patch? It is kind of ugly to include provider-specific code like this, but it has no downside for people not using AWS SES and can probably be encapsulated so that it doesn't get in the way.

          [JENKINS-30854] Support AWS SES Message IDs for In-Reply-To header

          Alex Earl added a comment -

          Please submit a pull request so I can review there, it's much easier.

          Alex Earl added a comment - Please submit a pull request so I can review there, it's much easier.

          Alex Earl added a comment -

          I looked at your changes and I think you could do it via a pre-send script instead of changing the plugin itself. See https://wiki.jenkins-ci.org/display/JENKINS/Email-ext+Recipes#Email-extRecipes-PresendScripts for some examples of pre-send scripts. You have access to the msg object.

          Alex Earl added a comment - I looked at your changes and I think you could do it via a pre-send script instead of changing the plugin itself. See https://wiki.jenkins-ci.org/display/JENKINS/Email-ext+Recipes#Email-extRecipes-PresendScripts for some examples of pre-send scripts. You have access to the msg object.

          Johannes W added a comment -

          Thanks for your answer, I created the pull request.

          However, I don't see how this can be done via a pre-send script: The Message-ID is only known after the email is sent, and can only be obtained by the SMTPTransport object.

          Johannes W added a comment - Thanks for your answer, I created the pull request . However, I don't see how this can be done via a pre-send script: The Message-ID is only known after the email is sent, and can only be obtained by the SMTPTransport object.

          Code changed in jenkins
          User: Johannes Weißl
          Path:
          src/main/java/hudson/plugins/emailext/ExtendedEmailPublisher.java
          src/main/java/hudson/plugins/emailext/ExtendedEmailPublisherDescriptor.java
          src/main/java/hudson/plugins/emailext/plugins/ContentBuilder.java
          src/main/resources/hudson/plugins/emailext/ExtendedEmailPublisher/config.groovy
          src/main/resources/hudson/plugins/emailext/ExtendedEmailPublisher/global.groovy
          src/main/resources/hudson/plugins/emailext/ExtendedEmailPublisher/help-tokens.groovy
          src/main/resources/hudson/plugins/emailext/ExtendedEmailPublisher/help-tokens.properties
          src/main/resources/hudson/plugins/emailext/templates/Jive-Formatter-README.md
          src/main/resources/hudson/plugins/emailext/templates/jive-formatter.groovy
          src/main/webapp/help/globalConfig/defaultClasspath.html
          src/main/webapp/help/globalConfig/defaultPostsendScript.html
          src/main/webapp/help/globalConfig/security.html
          src/main/webapp/help/projectConfig/defaultClasspath.html
          src/main/webapp/help/projectConfig/postsendScript.html
          src/test/java/hudson/plugins/emailext/ExtendedEmailPublisherTest.java
          src/test/java/hudson/plugins/emailext/plugins/ContentBuilderTest.java
          src/test/java/hudson/plugins/emailext/plugins/content/TriggerNameContentTest.java
          src/test/postsend/hudson/plugins/emailext/ExtendedEmailPublisherTestHelper.groovy
          src/test/resources/recipient-provider-upgrade.xml
          src/test/resources/recipient-provider-upgrade2.xml
          http://jenkins-ci.org/commit/email-ext-plugin/8c0c532a524f14730ad221a9d15f98c6341bbd37
          Log:
          Add post-send script feature

          For some tasks pre-send scripts are not sufficient, like rewriting the
          Message-ID based on the SMTP response for later In-Reply-To headers.
          This is needed when using Amazon Simple Email Service (AWS SES) for
          sending emails.

          Other possible use cases are scripts that act upon failed sending
          attempts.

          Here is a example post-send script that does the message id rewriting
          for AWS SES:

          ```groovy
          import com.sun.mail.smtp.SMTPTransport;
          import java.util.regex.Matcher;
          import java.util.regex.Pattern;
          import hudson.plugins.emailext.ExtendedEmailPublisherDescriptor;

          String smtpHost = props.getProperty("mail.smtp.host", "");
          String awsRegex = "^email-smtp\\.([a-z0-9-]+)\\.amazonaws\\.com\$";
          Pattern p = Pattern.compile(awsRegex);
          Matcher m = p.matcher(smtpHost);
          if (m.matches()) {
          String region = m.group(1);
          if (transport instanceof SMTPTransport) {
          String response = ((SMTPTransport)transport).getLastServerResponse();
          String[] parts = response.trim().split(" +");
          if (parts.length == 3 && parts[0].equals("250") && parts[1].equals("Ok"))

          { String MessageID = "<" + parts[2] + "@" + region + ".amazonses.com>"; msg.setHeader("Message-ID", MessageID); }

          }
          }
          ```

          References:

          SCM/JIRA link daemon added a comment - Code changed in jenkins User: Johannes Weißl Path: src/main/java/hudson/plugins/emailext/ExtendedEmailPublisher.java src/main/java/hudson/plugins/emailext/ExtendedEmailPublisherDescriptor.java src/main/java/hudson/plugins/emailext/plugins/ContentBuilder.java src/main/resources/hudson/plugins/emailext/ExtendedEmailPublisher/config.groovy src/main/resources/hudson/plugins/emailext/ExtendedEmailPublisher/global.groovy src/main/resources/hudson/plugins/emailext/ExtendedEmailPublisher/help-tokens.groovy src/main/resources/hudson/plugins/emailext/ExtendedEmailPublisher/help-tokens.properties src/main/resources/hudson/plugins/emailext/templates/Jive-Formatter-README.md src/main/resources/hudson/plugins/emailext/templates/jive-formatter.groovy src/main/webapp/help/globalConfig/defaultClasspath.html src/main/webapp/help/globalConfig/defaultPostsendScript.html src/main/webapp/help/globalConfig/security.html src/main/webapp/help/projectConfig/defaultClasspath.html src/main/webapp/help/projectConfig/postsendScript.html src/test/java/hudson/plugins/emailext/ExtendedEmailPublisherTest.java src/test/java/hudson/plugins/emailext/plugins/ContentBuilderTest.java src/test/java/hudson/plugins/emailext/plugins/content/TriggerNameContentTest.java src/test/postsend/hudson/plugins/emailext/ExtendedEmailPublisherTestHelper.groovy src/test/resources/recipient-provider-upgrade.xml src/test/resources/recipient-provider-upgrade2.xml http://jenkins-ci.org/commit/email-ext-plugin/8c0c532a524f14730ad221a9d15f98c6341bbd37 Log: Add post-send script feature For some tasks pre-send scripts are not sufficient, like rewriting the Message-ID based on the SMTP response for later In-Reply-To headers. This is needed when using Amazon Simple Email Service (AWS SES) for sending emails. Other possible use cases are scripts that act upon failed sending attempts. Here is a example post-send script that does the message id rewriting for AWS SES: ```groovy import com.sun.mail.smtp.SMTPTransport; import java.util.regex.Matcher; import java.util.regex.Pattern; import hudson.plugins.emailext.ExtendedEmailPublisherDescriptor; String smtpHost = props.getProperty("mail.smtp.host", ""); String awsRegex = "^email-smtp\\.( [a-z0-9-] +)\\.amazonaws\\.com\$"; Pattern p = Pattern.compile(awsRegex); Matcher m = p.matcher(smtpHost); if (m.matches()) { String region = m.group(1); if (transport instanceof SMTPTransport) { String response = ((SMTPTransport)transport).getLastServerResponse(); String[] parts = response.trim().split(" +"); if (parts.length == 3 && parts [0] .equals("250") && parts [1] .equals("Ok")) { String MessageID = "<" + parts[2] + "@" + region + ".amazonses.com>"; msg.setHeader("Message-ID", MessageID); } } } ``` References: https://issues.jenkins-ci.org/browse/JENKINS-30854 https://github.com/jenkinsci/email-ext-plugin/pull/108 http://docs.aws.amazon.com/ses/latest/DeveloperGuide/smtp-response-codes.html

          Alex Earl added a comment -

          Will be in next release 2.41

          Alex Earl added a comment - Will be in next release 2.41

          Johannes W added a comment -

          Thanks a lot!

          Johannes W added a comment - Thanks a lot!

          Johannes W added a comment -

          Johannes W added a comment - I added a paragraph to https://wiki.jenkins-ci.org/display/JENKINS/Email-ext+Recipes#Email-extRecipes-PostsendScripts

          Johannes W added a comment -

          Bug with the post-send script feature in JENKINS-33205.

          Johannes W added a comment - Bug with the post-send script feature in JENKINS-33205 .

            slide_o_mix Alex Earl
            jmuc Johannes W
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: