1
0
mirror of https://github.com/tektoncd/catalog.git synced 2024-11-21 05:55:35 +00:00
Fix for issue-1285 Send mail task was failing to complete
This commit is contained in:
Sudhish Nair 2024-06-30 22:10:39 +05:30 committed by tekton-robot
parent 08d0a66ed8
commit a8c2c4d394
2 changed files with 172 additions and 0 deletions

View File

@ -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: "<me@myserver.com>"
- name: recipients
value: "<him@hisserver.com> <her@herserver.com>"
taskRef:
name: sendmail
```

View File

@ -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