Skip to content

Instantly share code, notes, and snippets.

@tyrostone
Created February 11, 2016 19:53
Show Gist options
  • Save tyrostone/d7a2e03f091cb50eb2ac to your computer and use it in GitHub Desktop.
Save tyrostone/d7a2e03f091cb50eb2ac to your computer and use it in GitHub Desktop.
package main
import (
"crypto/tls"
"flag"
"fmt"
"golang.org/x/net/html"
"io"
"log"
"net"
"net/http"
"net/mail"
"net/smtp"
"strconv"
"strings"
)
var destinationEmail string
var password string
type SMTPServer struct {
Address string
Username string
Password string
Port int
TLS bool
}
type Email struct {
From mail.Address
To mail.Address
Subject string
Body string
}
func getHeaders(body io.Reader, headers []string) []string {
tokenizer := html.NewTokenizer(body)
counter := 0
for {
tokenType := tokenizer.Next()
token := tokenizer.Token()
switch tokenType {
case html.ErrorToken:
return headers
case html.StartTagToken:
if token.Data == "h2" {
counter++
}
case html.TextToken:
if counter > 0 {
counter--
headers = append(headers, token.Data)
}
case html.EndTagToken:
continue
}
}
}
func getTitleWords(headers []string) []string {
bookTitleHeader := headers[0]
titleWords := strings.Fields(bookTitleHeader)
return titleWords
}
func determineDownload(bookWords []string, desiredKeywords []string, download bool) bool {
if download {
return true
} else {
for download == false {
for _, i := range bookWords {
for _, k := range desiredKeywords {
len := len(i)
if i[len-1:len] == ":" && len > 2 {
i = i[:len-1]
}
if i == k {
download = true
break
}
}
}
break
}
}
return download
}
func sendEmail(email Email, server SMTPServer) (success bool) {
fullAddr := server.Address + ":" + strconv.Itoa(server.Port)
host, _, _ := net.SplitHostPort(fullAddr)
headers := make(map[string]string)
headers["From"] = email.From.String()
headers["To"] = email.To.String()
headers["Subject"] = email.Subject
message := ""
for k, v := range headers {
message += fmt.Sprintf("%s: %s\r\n", k, v)
}
message += "\r\n" + email.Body
fromString := email.From.String()
parsedFrom := fromString[1 : len(fromString)-1]
auth := smtp.PlainAuth("", parsedFrom, server.Password, host)
tlsconfig := &tls.Config{
InsecureSkipVerify: true,
ServerName: host,
}
conn, err := tls.Dial("tcp", fullAddr, tlsconfig)
checkError(err)
c, err := smtp.NewClient(conn, host)
checkError(err)
if err = c.Auth(auth); err != nil {
log.Panic(err)
}
if err = c.Mail(email.From.Address); err != nil {
log.Panic(err)
}
if err = c.Rcpt(email.To.Address); err != nil {
log.Panic(err)
}
w, err := c.Data()
checkError(err)
_, err = w.Write([]byte(message))
checkError(err)
err = w.Close()
checkError(err)
c.Quit()
return true
}
func checkError(err error) {
if err != nil {
log.Panic(err)
}
}
func main() {
flag.StringVar(&destinationEmail, "email", "", "for gmail")
flag.StringVar(&password, "password", "", "for gmail")
flag.Parse()
emailAddr := mail.Address{"", destinationEmail}
keywords := []string{"DevOps", "Go", "Golang", "Python",
"JavaScript", "AngularJS", "Ember.js", "Cloud", "Continuous Integration", "Puppet"}
freeBooksURL := "https://www.packtpub.com/packt/offers/free-learning"
resp, err := http.Get(freeBooksURL)
checkError(err)
defer resp.Body.Close()
headers := getHeaders(resp.Body, []string{})
resp.Body.Close()
titleKeywords := getTitleWords(headers)
download := determineDownload(titleKeywords, keywords, false)
if download {
email := Email{emailAddr, emailAddr,
"Download Today's Packt Free Learning Book",
"The book is: " + fmt.Sprintf("%v", titleKeywords) +
" and the download URL is: " + fmt.Sprintf("%v", freeBooksURL)}
server := SMTPServer{"smtp.gmail.com",
emailAddr.String(), password, 465, true}
sendEmail(email, server)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment