How to send mail notifications using smtp Server through FlexDeploy

Objective

You have a working FlexDeploy Release/Pipeline. The goal of the tutorial is to send mail notification using Gmail as smtp server.  Similar way any other smtp server can be configured. The tutorial will include:

  • Configure gmail credentials

  • Configure FlexDeploy Email Setting properties

  • Configure email content

Configure gmail credentials

  • This step is only applicable when using gmail as smtp server.

  • We can’t directly use gmail id and password as smtp credentials. It will fail with below error during execution.

  • We have to use App password instead of actual password. To generate the same first access the page with your Gmail/Google Account -> https://myaccount.google.com/security

  • Navigate to How you sign in to Google section, enable the 2-Step Verification.

  • After enabling 2-Step Verification, search for App passwords security feature as shown below and select the same

  • Once you are inside App passwords, we need to generate an app passwords. Select app as Mail and select device as Other - choose a name, then click on the GENERATE button.

  • Once the App password is generated, the same can be used as smtp password.

Few important notes

  • SMTP stands for Simple Mail Transfer Protocol and it’s the industry standard protocol for email sending. A sender will use an SMTP server to carry out the process of transmitting an email message.

  •  IMAP (Internet Access Message Protocol) is an email protocol that deals with managing and retrieving email messages from the receiving server.

  • Since IMAP deals with message retrieval, you will not be able to use the IMAP protocol to send email. Instead, IMAP will be used for receiving messages.

  •  StartTLS is a protocol command used to inform the email server that the email client wants to upgrade from an insecure connection to a secure one using TLS or SSL. StartTLS is used with SMTP and IMAP.

Configure FlexDeploy Email Setting properties

  • To configure the smtp details navigate to System Settings.

  • Navigate to Email Settings and update the details.

  • Gmail smtp and imap server configuration are easily available over internet

  • Once setup is complete, you can test the configuration by clicking on the Test Email Configuration button.

  • It may happen that the Test Email Configuration failed with below error popup after around 10secs.

  • Important thing to understand here is even though its thrown as AuthenticationFailedException, there may be nothing wrong with userId/password. If we check closely, we can find, it’s getting timeout while connecting to smtp server. Could be Firewall related issue. Check the FlexDeploy Server log as well.

  • Tips :You can try telnet to smtp server host/port from the FlexDeploy server to check once.

  • If everything is successfully configured, Test Email Configuration will reflect success response.

  • Test email shall also be sent to user who initiated

Configure email content

  • Add a Notification Step in pipeline. The Pipeline should have a Role defined(any name) with the mail recipients as part of the same Role.

  • The Role should be mapped to Pipeline Roles of Notification step as shown below.

  • Provide correct Subject and Message details. In case the Message has dynamic values make sure to use f(x) option so that it can parse the dynamic values accordingly.

  • Sample groovy script to send mail notification body with dynamic content.

def builder = new StringBuilder(); // Creating variables and capturing relevant details def envCode= stgexec.getEnvironmentCode() def submittedUser= snapshot.getCreatedBy() def releaseName= stgexec.getReleaseName() // Setting the Header of the mail builder.append("<h1 style=\"padding: 10px 0\">Pipeline stage execution completed.</h1>"); // Sending the Stage execution details in Table format // Below section is to form the HTML table format builder.append("<table border=1>"); builder.append("<tr>"+ "<th style=\"padding: 0 10px 5px 0\">Environment Name</th>"+ "<th style=\"padding: 0 10px 5px 0\">Release Name</th>"+ "<th style=\"padding: 0 10px 5px 0\">Pipeline Name</th>"+ "<th style=\"padding: 0 10px 5px 0\">Submitted User</th>"+ "</tr>"); builder.append("<tr>"+ "<td style=\"padding-right: 10px\">${envCode}</td>"+ "<td style=\"padding-right: 10px\">${releaseName}</td>"+ "<td style=\"padding-right: 10px\">${PipelineName}</td>"+ "<td style=\"padding-right: 10px\">${submittedUser}</td>"+ "</tr>"); builder.append("</table>");
  • Apparently it looks fairly simple only, indeed it is. Lets go through it.

    • It’ simply retrieving some information using FlexDeploy API and assigning it to 3 variables.

    • Then creating a simple HTML table(basically String) with above details for better clarity (not mandatory, clear text also acceptable ).

  • That’s it, FlexDeploy will use the same content to send mail notification.

  • In case you are wondering from where we derived stgexec operations or we directly used PipelineName variable, please refer to below page to have a detailed understanding of FlexDeploy Groovy variable and methods:

Pipeline Groovy Variables and Methods

  • Below given is the result notification mail. It will contain some pre-configured information along with the content we added as part of Notification message.

  • In the Notification step itself FlexDeploy provides an excellent feature to verify/retrieve the operation/method names as shown below using Variable Lookup.

  • Next we will try something slightly more complex. Lets assume our Release has multiple projects associated to it, we want to add Project and Package details as well as part of the mail.

  • If we refer to Pipeline Groovy Variables and Methods , we can get the Project objects through stgexec.getSnapshotProjects() method. This method will basically return ReleaseProjectVersion objects.

  • ReleaseProjectVersion object has multiple methods to retrieve relavant details we are looking for. Above link contains all details.

  • So if we refactor our previous code a bit, below code will give us our desired result.

def builder = new StringBuilder(); def envCode=stgexec.getEnvironmentCode() def releaseName=stgexec.getReleaseName() //stgexec.getSnapshotProjects() --returns list of ReleaseProjectVersion objects. def releaseProjects=stgexec.getSnapshotProjects() // Setting the Header of the mail builder.append("<h1 style=\"padding: 10px 0\">Pipeline stage execution completed.</h1>"); // Sending the Stage execution details in Table format // Below section is to form the HTML table format builder.append("<table border=1>"); builder.append("<tr>"+ "<th style=\"padding: 0 10px 5px 0\">Environment Name</th>"+ "<th style=\"padding: 0 10px 5px 0\">Release Name</th>"+ "<th style=\"padding: 0 10px 5px 0\">Project Name</th>"+ "<th style=\"padding: 0 10px 5px 0\">Project Version</th>"+ "<th style=\"padding: 0 10px 5px 0\">Package Name</th>"+ "</tr>"); //Looping through all ReleaseProjectVersion objects(Projects) for (project in releaseProjects) { def projectName = project.getProjectName(); def projectVersion = project.getProjectVersionName(); def packageName = project.getPackageName(); builder.append("<tr>"+ "<td style=\"padding-right: 10px\">${envCode}</td>"+ "<td style=\"padding-right: 10px\">${releaseName}</td>"+ "<td style=\"padding-right: 10px\">${projectName}</td>"+ "<td style=\"padding-right: 10px\">${projectVersion}</td>"+ "<td style=\"padding-right: 10px\">${packageName}</td>"+ "</tr>"); } builder.append("</table>");
  • Now output mail will appear as given below.

Congratulations! You have successfully completed the email Notification Integration from FlexDeploy Pipeline.

Now that you have configured email setting successfully in FlexDeploy, the same can be reused for all other mail notifications as well. Simply refer to Pipeline Groovy Variables and Methods and configure your mail content similar way as mentioned above.

 

The following macros are not currently supported in the footer:
  • style