/
smtp.go
66 lines (58 loc) · 1.53 KB
/
smtp.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// Go MySQL Batch
//
// Copyright 2015 Guillaume Barre. All rights reserved.
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// You can obtain one at http://mozilla.org/MPL/2.0/.package main
package main
import (
// "bytes"
"fmt"
"log"
"net/smtp"
)
// SMTP configuration associated to a batch
type SmtpConfig struct {
Server string `xml:"server,attr"`
Port string `xml:"port,attr"`
User string `xml:"user,attr"`
Password string `xml:"password,attr"`
Sender string `xml:"sender,attr"`
}
// Returns the sender of the generated email
func (s *SmtpConfig) getSender() string {
if s.Sender != "" {
return s.Sender
} else {
return s.User
}
}
// Sends the content passed as parameter to the recipient list
// of the given batch result
func (s *SmtpConfig) sendEmailHtml(r *BatchResult, content string) {
mime := "MIME-version: 1.0;\nContent-Type: text/html; charset=\"UTF-8\";\n\n"
subject := fmt.Sprintf("Subject: MysqlBatch Results for the batch \"%s\"\n", r.BatchName)
TraceActivity.Printf("sending the email : %s \n", subject)
dest := make([]string, 0)
for _, d := range r.Recipients.RecipientList {
dest = append(dest, d.Address)
}
auth := smtp.PlainAuth(
"",
s.User,
s.Password,
s.Server,
)
err := smtp.SendMail(
s.Server+":"+s.Port,
auth,
s.getSender(),
dest,
[]byte(subject+mime+content),
)
if err != nil {
TraceActivity.Printf("error sending the email : %s \n", err.Error())
log.Fatal(err)
}
}