Finalize mail transfer
This commit is contained in:
parent
aee503148d
commit
91d5de8da9
4 changed files with 117 additions and 71 deletions
|
@ -1,39 +1,72 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
// "errors"
|
"strings"
|
||||||
// "fmt"
|
// "errors"
|
||||||
// "log"
|
"bytes"
|
||||||
// "os"
|
"fmt"
|
||||||
// "strconv"
|
"github.com/davecgh/go-spew/spew"
|
||||||
// "net"
|
// "log"
|
||||||
// "net/mail"
|
// "os"
|
||||||
// "net/smtp"
|
// "strconv"
|
||||||
|
// "net"
|
||||||
|
"net/mail"
|
||||||
|
// "net/smtp"
|
||||||
)
|
)
|
||||||
|
|
||||||
type MailHeaders map[string]string
|
type EmailHeaders map[string]string
|
||||||
type MailBody []string
|
type EmailBody string
|
||||||
|
|
||||||
type EmailCtx struct {
|
type EmailCtx struct {
|
||||||
Headers MailHeaders
|
Headers EmailHeaders
|
||||||
Body MailBody
|
Body EmailBody
|
||||||
|
}
|
||||||
|
|
||||||
|
func (headers EmailHeaders) String() string {
|
||||||
|
var buffer bytes.Buffer
|
||||||
|
for k, v := range headers {
|
||||||
|
buffer.WriteString(fmt.Sprintf("%s: %s\r\n", k, v))
|
||||||
|
}
|
||||||
|
return buffer.String()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (body EmailBody) String() string {
|
||||||
|
res := string(body)
|
||||||
|
if false {
|
||||||
|
spew.Dump(res)
|
||||||
|
}
|
||||||
|
return res
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewEmail() *EmailCtx {
|
func NewEmail() *EmailCtx {
|
||||||
return &EmailCtx{}
|
email := EmailCtx{}
|
||||||
|
email.Headers = make(EmailHeaders)
|
||||||
|
return &email
|
||||||
}
|
}
|
||||||
|
|
||||||
func (email *EmailCtx) MakeHeaders(config EmailConfig) (int, error) {
|
func encodeRFC2047(text string) string {
|
||||||
|
// use mail's rfc2047 to encode any string
|
||||||
|
addr := mail.Address{text, ""}
|
||||||
|
return strings.Trim(addr.String(), " \"<@>")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (email *EmailCtx) MakeHeaders(config EmailConfig) {
|
||||||
|
email.Headers["Return-Path"] = config.From
|
||||||
email.Headers["From"] = config.From
|
email.Headers["From"] = config.From
|
||||||
email.Headers["To"] = config.To
|
email.Headers["To"] = config.To
|
||||||
email.Headers["Subject"] = config.Subject
|
email.Headers["Subject"] = encodeRFC2047(config.Subject)
|
||||||
return 0, nil
|
// email.Headers["Content-Type"] = "text/plain; charset=\"us-ascii\";"
|
||||||
|
email.Headers["Content-Type"] = "text/plain; charset=\"utf-8\";"
|
||||||
|
email.Headers["Content-Transfer-Encoding"] = "base64"
|
||||||
|
email.Headers["MIME-Version"] = "1.0"
|
||||||
|
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (email *EmailCtx) MakeBody(content []string) (int, error) {
|
func (email *EmailCtx) MakeBody(content string) {
|
||||||
email.Body = content
|
email.Body = EmailBody(content)
|
||||||
return 0, nil
|
if false {
|
||||||
}
|
spew.Dump(email.Body)
|
||||||
|
}
|
||||||
func (email *EmailCtx) Send() {
|
return
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,28 +1,10 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
// Examples
|
|
||||||
// - Sending emails with SSL : https://gist.github.com/chrisgillis/10888032
|
|
||||||
// - Project layout https://github.com/golang-standards/project-layout
|
|
||||||
// - Markdown rendering https://github.com/russross/blackfriday
|
|
||||||
|
|
||||||
import (
|
import (
|
||||||
// "gopkg.in/russross/blackfriday.v2"
|
// "gopkg.in/russross/blackfriday.v2"
|
||||||
// "github.com/davecgh/go-spew/spew"
|
// "github.com/davecgh/go-spew/spew"
|
||||||
)
|
)
|
||||||
|
|
||||||
func BuildContent(config Config) []string {
|
|
||||||
// run taskell (download tasks from trello and export markdown)
|
|
||||||
// read file as an array
|
|
||||||
// insert trello board url
|
|
||||||
// convert to HTML
|
|
||||||
|
|
||||||
// output := blackfriday.Run(input, blackfriday.WithNoExtensions())
|
|
||||||
return []string{}
|
|
||||||
}
|
|
||||||
|
|
||||||
func ImportFromTrello() {
|
|
||||||
}
|
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
// Setup config
|
// Setup config
|
||||||
config := NewConfig()
|
config := NewConfig()
|
||||||
|
@ -32,13 +14,11 @@ func main() {
|
||||||
trelloCtx := NewTrello(config.Trello.Token)
|
trelloCtx := NewTrello(config.Trello.Token)
|
||||||
trelloBoard := trelloCtx.GetBoard(config.Trello.Url)
|
trelloBoard := trelloCtx.GetBoard(config.Trello.Url)
|
||||||
trelloMarkdown := trelloBoard.ExportToMarkdown()
|
trelloMarkdown := trelloBoard.ExportToMarkdown()
|
||||||
panic("samere")
|
|
||||||
|
|
||||||
// Create email enveloppe
|
// Create email enveloppe
|
||||||
email := NewEmail()
|
email := NewEmail()
|
||||||
email.MakeHeaders(config.Email)
|
email.MakeHeaders(config.Email)
|
||||||
email.MakeBody(trelloMarkdown)
|
email.MakeBody(trelloMarkdown)
|
||||||
email.Send()
|
|
||||||
|
|
||||||
// Connect and send email
|
// Connect and send email
|
||||||
transport := NewTransport(config.Smtp)
|
transport := NewTransport(config.Smtp)
|
||||||
|
|
|
@ -1,7 +1,9 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"crypto/tls"
|
"crypto/tls"
|
||||||
|
"encoding/base64"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"net/smtp"
|
"net/smtp"
|
||||||
|
@ -54,7 +56,6 @@ func NewTransportTls(config SmtpConfig) *tls.Config {
|
||||||
|
|
||||||
func (ctx *TransportCtx) DialInsecure() {
|
func (ctx *TransportCtx) DialInsecure() {
|
||||||
// no SSL/TLS
|
// no SSL/TLS
|
||||||
fmt.Println("Creating SMTP client...")
|
|
||||||
c, err := smtp.Dial(ctx.Address)
|
c, err := smtp.Dial(ctx.Address)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Panic(err)
|
log.Panic(err)
|
||||||
|
@ -63,13 +64,11 @@ func (ctx *TransportCtx) DialInsecure() {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ctx *TransportCtx) DialTls() {
|
func (ctx *TransportCtx) DialTls() {
|
||||||
fmt.Printf("Creating TLS connection to %s...\n", ctx.Address)
|
|
||||||
conn, err := tls.Dial("tcp", ctx.Address, ctx.Tls)
|
conn, err := tls.Dial("tcp", ctx.Address, ctx.Tls)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Panic(err)
|
log.Panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Println("Creating SMTP client...")
|
|
||||||
c, err := smtp.NewClient(conn, ctx.Config.Hostname)
|
c, err := smtp.NewClient(conn, ctx.Config.Hostname)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Panic(err)
|
log.Panic(err)
|
||||||
|
@ -78,12 +77,10 @@ func (ctx *TransportCtx) DialTls() {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ctx *TransportCtx) DialStartTls() {
|
func (ctx *TransportCtx) DialStartTls() {
|
||||||
fmt.Println("Creating SMTP client...")
|
|
||||||
c, err := smtp.Dial(ctx.Address)
|
c, err := smtp.Dial(ctx.Address)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Panic(err)
|
log.Panic(err)
|
||||||
}
|
}
|
||||||
fmt.Printf("Creating StartTLS connection to %s...\n", ctx.Address)
|
|
||||||
c.StartTLS(ctx.Tls)
|
c.StartTLS(ctx.Tls)
|
||||||
|
|
||||||
ctx.Client = c
|
ctx.Client = c
|
||||||
|
@ -114,5 +111,25 @@ func (ctx *TransportCtx) Quit() {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ctx *TransportCtx) Send(email *EmailCtx) {
|
func (ctx *TransportCtx) Send(email *EmailCtx) {
|
||||||
|
// Set email header
|
||||||
|
ctx.Client.Mail(email.Headers["From"])
|
||||||
|
ctx.Client.Rcpt(email.Headers["To"])
|
||||||
|
|
||||||
|
// Set email body
|
||||||
|
wc, err := ctx.Client.Data()
|
||||||
|
if err != nil {
|
||||||
|
log.Panic(err)
|
||||||
|
}
|
||||||
|
defer wc.Close()
|
||||||
|
|
||||||
|
var buffer bytes.Buffer
|
||||||
|
buffer.WriteString(email.Headers.String())
|
||||||
|
buffer.WriteString("\r\n")
|
||||||
|
buffer.WriteString(base64.StdEncoding.EncodeToString([]byte(email.Body.String())))
|
||||||
|
|
||||||
|
if _, err = buffer.WriteTo(wc); err != nil {
|
||||||
|
log.Panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,6 +2,7 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
// "errors"
|
// "errors"
|
||||||
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/adlio/trello"
|
"github.com/adlio/trello"
|
||||||
// "github.com/davecgh/go-spew/spew"
|
// "github.com/davecgh/go-spew/spew"
|
||||||
|
@ -38,10 +39,7 @@ func runcmd(command string) string {
|
||||||
return string(out)
|
return string(out)
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewTrello(token string) *TrelloCtx {
|
func GetTokenProcessMessage() string {
|
||||||
client := trello.NewClient(APP_KEY, token)
|
|
||||||
/*
|
|
||||||
if client == nil {
|
|
||||||
url := strings.Join([]string{
|
url := strings.Join([]string{
|
||||||
"https://trello.com/1/authorize?expiration=never",
|
"https://trello.com/1/authorize?expiration=never",
|
||||||
"name=taskell",
|
"name=taskell",
|
||||||
|
@ -56,9 +54,12 @@ func NewTrello(token string) *TrelloCtx {
|
||||||
"When you have your access token, set TRELLO_TOKEN=<your-token>",
|
"When you have your access token, set TRELLO_TOKEN=<your-token>",
|
||||||
}, "\n\n")
|
}, "\n\n")
|
||||||
|
|
||||||
log.Panic(errors.New(text))
|
return text
|
||||||
}
|
}
|
||||||
*/
|
|
||||||
|
func NewTrello(token string) *TrelloCtx {
|
||||||
|
client := trello.NewClient(APP_KEY, token)
|
||||||
|
|
||||||
ctx := TrelloCtx{}
|
ctx := TrelloCtx{}
|
||||||
ctx.Token = token
|
ctx.Token = token
|
||||||
ctx.Client = client
|
ctx.Client = client
|
||||||
|
@ -79,16 +80,31 @@ func (ctx *TrelloCtx) GetBoard(boardUrl string) TrelloBoard {
|
||||||
return TrelloBoard{Ctx: ctx, Ptr: board}
|
return TrelloBoard{Ctx: ctx, Ptr: board}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (board *TrelloBoard) ExportToMarkdown() []string {
|
func (board *TrelloBoard) ExportToMarkdown() string {
|
||||||
// var s []string
|
var markdown bytes.Buffer
|
||||||
|
var text string
|
||||||
|
|
||||||
lists, _ := board.Ptr.GetLists(trello.Defaults())
|
lists, _ := board.Ptr.GetLists(trello.Defaults())
|
||||||
// spew.Dump(lists)
|
// spew.Dump(lists)
|
||||||
// s = append(s, "# Trello board")
|
text = fmt.Sprintf("# Board %s\n\n", board.Ptr.Name)
|
||||||
for _, v := range lists {
|
markdown.WriteString(text)
|
||||||
fmt.Println(v.Name)
|
|
||||||
|
text = fmt.Sprintf("URL: %s\n", board.Ptr.ShortUrl)
|
||||||
|
markdown.WriteString(text)
|
||||||
|
|
||||||
|
for listIdx := len(lists) - 1; listIdx >= 0; listIdx -= 1 {
|
||||||
|
list := lists[listIdx]
|
||||||
|
text := fmt.Sprintf("\n## %s\n\n", list.Name)
|
||||||
|
markdown.WriteString(text)
|
||||||
|
|
||||||
|
cards, _ := list.GetCards(trello.Defaults())
|
||||||
|
for _, card := range cards {
|
||||||
|
text := fmt.Sprintf("* %s\n", card.Name)
|
||||||
|
markdown.WriteString(text)
|
||||||
|
// spew.Dump(card)
|
||||||
}
|
}
|
||||||
return []string{}
|
}
|
||||||
|
return markdown.String()
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
Loading…
Reference in a new issue