-
Notifications
You must be signed in to change notification settings - Fork 0
/
email.go
38 lines (32 loc) · 991 Bytes
/
email.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package main
import (
"bufio"
"fmt"
"net/smtp"
"os"
"strconv"
"time"
)
func sendMail(message string, userEmail string) {
auth := smtp.PlainAuth("", os.Getenv("SMTP_FROM"), os.Getenv("SMTP_PASSWORD"), os.Getenv("SMTP_HOST"))
to := []string{userEmail}
currentTime := time.Now()
date := strconv.Itoa(currentTime.Year()) + "-" + strconv.Itoa(int(currentTime.Month())) + "-" + strconv.Itoa(currentTime.Day())
body := []byte("Subject: Your Todo List for " + date + "\n\r" + message)
err := smtp.SendMail(os.Getenv("SMTP_HOST") + ":" + os.Getenv("SMTP_PORT"), auth, os.Getenv("SMTP_FROM"), to, body)
if err != nil {
fmt.Println(err)
return
}
}
func emailCommand() {
scanner := bufio.NewScanner(os.Stdin)
if userEmail == "" {
fmt.Println("Looks like you haven't shared your email yet. Please type it in below:")
scanner.Scan()
userEmail = scanner.Text()
}
sendMail(todosToString(todos, (func(i int) string {
return strconv.Itoa(i + 1) + ") "
})), userEmail)
}