Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for Issue 1285-: SendMail task was failing to execute successfully #1286

Merged
merged 1 commit into from
Aug 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 81 additions & 0 deletions task/sendmail/0.2/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
Send a message via SMTP server

This task sends a simple email to receivers via SMTP server

## Install the Task and create a secret

```
kubectl apply -f https://api.hub.tekton.dev/v1/resource/tekton/task/sendmail/0.2/raw
```

Create a secret that has the SMTP server information

* **url**: The IP address of the SMTP server

* **port**: The port number of the SMTP server

* **user**: User name for the SMTP server

* **password**: Password for the SMTP server

* **tls**: The tls enabled or not ("True" or "False")

Example server-secret.yaml
```
kind: Secret
apiVersion: v1
metadata:
name: server-secret
stringData:
url: "smtp.server.com"
port: "25"
user: "userid"
password: "password"
tls: "False"
```

Example kubectl command
```
kubectl apply -f server-secret.yaml
```

## Parameters

* **server**: The name of the secret that has the SMTP server information

* **subject**: Email subject (plain text)

* **body**: Email body (plain text)

* **sender**: Email sender email address

* **recipients**: Email recipients email addresses (space delimited)

## Platforms

The Task can be run on `linux/amd64`, `linux/s390x`, `linux/arm64` and `linux/ppc64le` platforms.

## Usage

This TaskRun runs the Task to send an email to the receivers via the SMTP server.

```
apiVersion: tekton.dev/v1
kind: TaskRun
metadata:
name: mail-taskrun
spec:
params:
- name: server
value: server-secret
- name: subject
value: Hi, again!
- name: body
value: "Tekton email"
- name: sender
value: "<[email protected]>"
- name: recipients
value: "<[email protected]> <[email protected]>"
taskRef:
name: sendmail
```
91 changes: 91 additions & 0 deletions task/sendmail/0.2/sendmail.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
apiVersion: tekton.dev/v1
kind: Task
metadata:
name: sendmail
labels:
app.kubernetes.io/version: "0.2"
annotations:
tekton.dev/pipelines.minVersion: "0.50.0"
tekton.dev/categories: Messaging
tekton.dev/tags: mail
tekton.dev/displayName: "send mail"
tekton.dev/platforms: "linux/amd64,linux/s390x,linux/ppc64le,linux/arm64"
spec:
description: >-
This task sends a simple email to receivers via SMTP server

params:
- name: server
type: string
description: secret name for SMTP server information (url, port, password)
- name: subject
type: string
description: plain text email subject
- name: body
type: string
description: plain text email body
- name: sender
type: string
description: sender email address
- name: recipients
type: string
description: recipient email addresses (space delimited list)
steps:
- name: send
image: docker.io/library/python:3.12-alpine@sha256:c2f41e6a5a67bc39b95be3988dd19fbd05d1b82375c46d9826c592cca014d4de #tag: 3.12-alpine
script: |
#!/usr/bin/env python3
import smtplib, ssl, os
port = os.getenv('PORT')
smtp_server = os.getenv('SERVER')
sender_email = "$(params.sender)"
receiver_emails = "$(params.recipients)"
user = os.getenv('USER')
password = os.getenv('PASSWORD')
tls = os.getenv('TLS')
message = f"""\
Subject: $(params.subject)
To: {receiver_emails}
From: {sender_email}

$(params.body)"""
print(message)
if tls == 'True':
context = ssl.create_default_context()
server = smtplib.SMTP_SSL(smtp_server, port, context=context)
server.ehlo()
else:
server = smtplib.SMTP(smtp_server, port)
server.ehlo()
server.starttls()
if password != '':
server.login(user, password)
for receiver in [item for item in receiver_emails.split(' ') if item]:
server.sendmail(sender_email, receiver, message.encode('utf-8'))
server.quit()
env:
- name: USER
valueFrom:
secretKeyRef:
name: $(params.server)
key: user
- name: PASSWORD
valueFrom:
secretKeyRef:
name: $(params.server)
key: password
- name: TLS
valueFrom:
secretKeyRef:
name: $(params.server)
key: tls
- name: SERVER
valueFrom:
secretKeyRef:
name: $(params.server)
key: url
- name: PORT
valueFrom:
secretKeyRef:
name: $(params.server)
key: port