forked from documize/community
-
Notifications
You must be signed in to change notification settings - Fork 0
/
send.go
127 lines (100 loc) · 2.89 KB
/
send.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// Copyright 2016 Documize Inc. <legal@documize.com>. All rights reserved.
//
// This software (Documize Community Edition) is licensed under
// GNU AGPL v3 http://www.gnu.org/licenses/agpl-3.0.en.html
//
// You can operate outside the AGPL restrictions by purchasing
// Documize Enterprise Edition and obtaining a commercial license
// by contacting <sales@documize.com>.
//
// https://documize.com
// Package smtp provides access to SMTP server for sending email.
package smtp
import (
"crypto/tls"
"encoding/base64"
"strings"
"github.com/documize/community/core/mail"
)
// Config is used to create SMTP server connection
type Config struct {
// IP/hostname of SMTP server
Host string
// Port number of SMTP server
Port int
// Username for authentication with SMTP server
Username string
// Password for authentication with SMTP server
Password string
// SenderEmail is FROM address
SenderEmail string
// SenderName is FROM display name
SenderName string
// AnonymousAuth does not send username/password to server
AnonymousAuth bool
// Base64EncodeCredentials encodes User and Password as base64 before sending to SMTP server
Base64EncodeCredentials bool
// UseSSL uses SMTP SSL connection with SMTP server
UseSSL bool
// SkipSSLVerify allows unverified certificates
SkipSSLVerify bool
}
// Connect returns open connection to server for sending email
func Connect(c Config) (d *mail.Dialer, err error) {
// prepare credentials
u := strings.TrimSpace(c.Username)
p := strings.TrimSpace(c.Password)
// anonymous, no credentials
if c.AnonymousAuth {
u = ""
p = ""
}
// base64 encode if required
if c.Base64EncodeCredentials {
u = base64.StdEncoding.EncodeToString([]byte(u))
p = base64.StdEncoding.EncodeToString([]byte(p))
}
// basic server
d = mail.NewDialer(c.Host, c.Port, u, p)
// use SSL
d.SSL = c.UseSSL
// verify SSL cert chain
d.TLSConfig = &tls.Config{InsecureSkipVerify: c.SkipSSLVerify}
// TLS mode
d.StartTLSPolicy = mail.OpportunisticStartTLS
return d, nil
}
// EmailMessage represents email to be sent.
type EmailMessage struct {
ToEmail string
ToName string
Subject string
BodyHTML string
ReplyTo string
ReplyName string
}
// SendMessage sends email using specified SMTP connection
func SendMessage(d *mail.Dialer, c Config, em EmailMessage) (b bool, err error) {
m := mail.NewMessage()
// participants
m.SetHeader("From", m.FormatAddress(c.SenderEmail, c.SenderName))
m.SetHeader("To", m.FormatAddress(em.ToEmail, em.ToName))
// Where do replies go?
reply := c.SenderEmail
replyName := c.SenderName
if len(em.ReplyTo) > 0 {
reply = em.ReplyTo
}
if len(em.ReplyName) > 0 {
replyName = em.ReplyName
}
m.SetAddressHeader("Reply-To", reply, replyName)
// content
m.SetHeader("Subject", em.Subject)
m.SetBody("text/html", em.BodyHTML)
// send email
if err = d.DialAndSend(m); err != nil {
return false, err
}
return true, nil
}