Compare commits

...

2 Commits

Author SHA1 Message Date
Sameer Rahmani 22d1737a8d
first release 2023-05-13 18:16:33 +01:00
Sameer Rahmani c01f050e15
Initial commit 2023-05-13 16:09:50 +01:00
5 changed files with 120 additions and 1 deletions

14
Dockerfile Normal file
View File

@ -0,0 +1,14 @@
FROM alpine
RUN apk --no-cache add go ca-certificates
WORKDIR /app
ADD go.mod .
ADD main.go .
RUN go build main.go
RUN ls main
FROM alpine
RUN apk --no-cache add ca-certificates
WORKDIR /app
COPY --from=0 /app/main /app/main
ENTRYPOINT /app/main

7
Makefile Normal file
View File

@ -0,0 +1,7 @@
TAG=4
build:
docker build -t lxsameer/woodpecker_mailer:$(TAG) .
push:
docker push lxsameer/woodpecker_mailer:$(TAG)

View File

@ -1,3 +1,22 @@
# woodpecker_mailer
This is a very basic mailer plugin for woodpecker
A mailer plugin for the Woodpecker CI.
Example configuration:
```yaml
pipeline:
Mail:
image: lxsameer/woodpecker_mailer:4
settings:
from: ci@serene-lang.org
user:
from_secret: mail_user
password:
from_secret: mail_pass
to: ${CI_COMMIT_AUTHOR_EMAIL}
subject: "[${CI_REPO}] JOB #${CI_BUILD_NUMBER} - ${CI_PREV_STEP_STATUS}"
text: |
BUILD: ${CI_BUILD_LINK}
COMMIT: ${CI_COMMIT_LINK}
```

3
go.mod Normal file
View File

@ -0,0 +1,3 @@
module lxsameer.com/woodpecker_mailer
go 1.20

76
main.go Normal file
View File

@ -0,0 +1,76 @@
package main
import (
"crypto/tls"
"fmt"
"html/template"
"net/mail"
"net/smtp"
"os"
"strings"
)
// "Send As" information
func main() {
var (
username = os.Getenv("PLUGIN_USER")
password = os.Getenv("PLUGIN_PASSWORD")
to = os.Getenv("PLUGIN_TO")
subject = os.Getenv("PLUGIN_SUBJECT")
body = os.Getenv("PLUGIN_TEXT")
from_addr = os.Getenv("PLUGIN_FROM")
from_name = os.Getenv("PLUGIN_FROM_NAME")
)
if username == "" || password == "" || to == "" || subject == "" || body == "" {
panic("'user', 'password', 'to', 'subject', and 'text' are mandatory!")
}
if username == "" || password == "" || to == "" || subject == "" || body == "" {
panic("'user', 'password', 'to', 'subject', and 'text' are mandatory!")
}
const gmail = "smtp.gmail.com:587"
sanitizedBody := template.HTMLEscapeString(body)
fmt.Println("Authenticateing...")
auth := smtp.PlainAuth("", username, password, "smtp.gmail.com")
smtpConnection, err := smtp.Dial(gmail)
if err != nil {
panic(err)
}
if err := smtpConnection.StartTLS(&tls.Config{ServerName: "smtp.gmail.com"}); err != nil {
panic(err)
}
if err := smtpConnection.Auth(auth); err != nil {
panic(err)
}
from := mail.Address{Name: from_name, Address: from_addr}
header := make(map[string]string)
header["From"] = from.String()
header["To"] = to
header["Subject"] = subject
// Convert the header map to a string and attach the body to it
var message string
for key, value := range header {
message += fmt.Sprintf("%s: %s\r\n", key, value)
}
message += "\r\n" + sanitizedBody + "\r\n"
fmt.Println("Senting an email to user")
rcpts := strings.Split(to, ",")
err = smtp.SendMail(gmail, auth, from_addr, rcpts, []byte(message))
if err != nil {
panic(err)
}
smtpConnection.Quit()
fmt.Println("Done")
}