Skip to content

Instantly share code, notes, and snippets.

@kunal732
Created January 24, 2014 12:53
Show Gist options
  • Save kunal732/8596671 to your computer and use it in GitHub Desktop.
Save kunal732/8596671 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"net/http"
"log"
"github.com/sendgrid/sendgrid-go"
"regexp"
)
//encode decode router
func CodeRouter (w http.ResponseWriter, req *http.Request) {
//Get Email Values
to := req.FormValue("from")
subject := req.FormValue("subject")
spam_score := req.FormValue("spam_score")
spam_report := req.FormValue("spam_report")
//use regex to get relevant info from spam report
r, _ := regexp.Compile("([1-9].[0-9]\\s([A-Z]*\\w)*(((\\D)*[a-zA-Z]\\w)*\\s)*)")
match := r.FindAllString(spam_report, -1)
//Construct Body from spam report text taken from the Regular Expression
startbody := "Your email has a Spam Score of <b>"+spam_score+"</b>. Whenever this score is above 5 it will most likely be rejected or sent to the spam folder by most email providers. You should always try to maintain a score less than 5 and as close to 0 as possible. Below is a list of of the spam rules your email has triggered.<br><br><b>Scores + Rules Triggered:</b><br>"
for x:=2; x<len(match); x++ {
fmt.Println(match[x])
startbody=startbody +match[x]
startbody=startbody+"<br>"
}
//Setup Values to email
subject = "Re: "+subject
body := "<br><br>Raw Spam Report:<br>"+req.FormValue("spam_report")
body = startbody+body
emailBack(subject, to, body)
}
func emailBack(subject string, to string, body string) {
sg := sendgrid.NewSendGridClient("username", "password")
message := sendgrid.NewMail()
message.AddTo(to)
message.AddSubject(subject)
message.AddHTML(body)
message.AddFrom("check@spamtest.me")
if r := sg.Send(message); r == nil {
fmt.Println("Email sent!")
} else {
fmt.Println(r)
}
}
func main() {
http.HandleFunc("/spamcheck", CodeRouter)
err := http.ListenAndServe(":3030", nil)
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment