I am working on a tekton task to share test results as an attachment to the management team, when I went through tekton dev hub I could find sendMail task but I don't see an option to attach file to the mail.
https://hub.tekton.dev/tekton/task/sendmail
Can someone help me with the task to send email with attachments.
Also I have a java code that does this( send email along with attachments) using Microsoft EWS API but I am not sure how to convert that piece of code to shell script and use it in my pipleine, if someone can help me here it would be greatly appreciated. Below is the java code I am using
try {
String accessToken = StringUtils.EMPTY;
RestAssured.baseURI = "https://login.microsoftonline.com/c990bb7a-51f4-439b-bd36-9c07fb1041c0/oauth2/v2.0/token";
RequestSpecification request = RestAssured.given();
request.header(new Header("Content-Type", "application/x-www-form-urlencoded"));
ProxySpecification proxySpecification = new ProxySpecification("proxy.com", 83, "http");
request.proxy(proxySpecification);
request.accept(ContentType.URLENC);
Response response = request.contentType(ContentType.URLENC.withCharset("UTF-8"))
.formParam("grant_type", "client_credentials")
.formParam("client_id", "client_id")
.formParam("client_secret", "client_secret")
.formParam("scope", "https://outlook.office365.com/.default").post();
JSONObject subRespObject = new JSONObject(response.getBody().asString());
if (null != subRespObject && subRespObject.length() > 0 && !subRespObject.isNull("access_token")) {
accessToken = subRespObject.getString("access_token");
}
service = new ExchangeService();
service.setTraceEnabled(true);
URI url = new URI(EWS_HOST);
service.setUrl(url);
ImpersonatedUserId impersonatedUserId = new ImpersonatedUserId(ConnectingIdType.SmtpAddress, SENDER_CDSID);
service.setImpersonatedUserId(impersonatedUserId);
Map<String, String> headers = new HashMap<String, String>();
headers.put("X-AnchorMailbox", SENDER_CDSID);
headers.put("Authorization", "Bearer " + accessToken);
service.getHttpHeaders().putAll(headers);
WebProxy value = new WebProxy("proxy.com", 83);
String subject = emailConfigPropsMap.get("emailSubject");
String pullUrl = System.getProperty("pullUrl");
String pullNumber = StringUtils.substring(pullUrl, pullUrl.lastIndexOf("/")+1);
String messageBody = MessageFormat.format(emailConfigPropsMap.get("bodyMessage"), pullUrl,pullNumber);
String messageToRecipients = System.getProperty("approvers");
EmailMessage message = new EmailMessage(service);
MessageBody msgBody = new MessageBody();
msgBody.setText(messageBody);
message.setBody(msgBody);
msgBody.setBodyType(BodyType.HTML);
File folder = new File(FilenameUtils.separatorsToSystem(System.getProperty("user.dir") + "\\Results"));
List<File> attachments = new ArrayList<File>();
File[] files = folder.listFiles();
if (files != null) {
List<File> fileList = Arrays.asList(files);
for (File inputFile : fileList) {
attachments.add(inputFile);
}
}
if (CollectionUtils.isNotEmpty(attachments)) {
for (File inputFile : attachments) {
String fileName = inputFile.getName();
FileInputStream fileInputStream = new FileInputStream(inputFile);
message.getAttachments().addFileAttachment(fileName, fileInputStream);
}
}
EmailAddress senderEmail = new EmailAddress(SENDER_CDSID);
message.setFrom(senderEmail);
message.setSubject(subject);
if (StringUtils.isNotEmpty(messageToRecipients)) {
List<String> mailToRecipients = Arrays.asList(messageToRecipients.split(","));
if (CollectionUtils.isNotEmpty(mailToRecipients)) {
for (String mailToRecipient : mailToRecipients) {
EmailAddress toRecipients = new EmailAddress();
toRecipients.setAddress(mailToRecipient);
message.getToRecipients().add(toRecipients);
}
}
}
message.send();
} catch (Exception ex) {
ex.printStackTrace();
}