github.com/elliott5/community@v0.14.1-0.20160709191136-823126fb026a/documize/api/mail/mailer.go (about) 1 // Copyright 2016 Documize Inc. <legal@documize.com>. All rights reserved. 2 // 3 // This software (Documize Community Edition) is licensed under 4 // GNU AGPL v3 http://www.gnu.org/licenses/agpl-3.0.en.html 5 // 6 // You can operate outside the AGPL restrictions by purchasing 7 // Documize Enterprise Edition and obtaining a commercial license 8 // by contacting <sales@documize.com>. 9 // 10 // https://documize.com 11 12 // jshint ignore:start 13 14 package mail 15 16 import ( 17 "bytes" 18 "fmt" 19 "html/template" 20 "net/smtp" 21 22 "github.com/documize/community/documize/api/request" 23 "github.com/documize/community/documize/web" 24 "github.com/documize/community/wordsmith/log" 25 ) 26 27 // InviteNewUser invites someone new providing credentials, explaining the product and stating who is inviting them. 28 func InviteNewUser(recipient, inviter, url, username, password string) { 29 method := "InviteNewUser" 30 31 file, err := web.ReadFile("mail/invite-new-user.html") 32 33 if err != nil { 34 log.Error(fmt.Sprintf("%s - unable to load email template", method), err) 35 return 36 } 37 38 emailTemplate := string(file) 39 40 // check inviter name 41 if inviter == "Hello You" || len(inviter) == 0 { 42 inviter = "Your colleague" 43 } 44 45 subject := fmt.Sprintf("%s has invited you to Documize", inviter) 46 47 e := newEmail() 48 e.From = creds.SMTPsender() 49 e.To = []string{recipient} 50 e.Subject = subject 51 52 parameters := struct { 53 Subject string 54 Inviter string 55 Url string 56 Username string 57 Password string 58 }{ 59 subject, 60 inviter, 61 url, 62 recipient, 63 password, 64 } 65 66 buffer := new(bytes.Buffer) 67 t := template.Must(template.New("emailTemplate").Parse(emailTemplate)) 68 log.IfErr(t.Execute(buffer, ¶meters)) 69 e.HTML = buffer.Bytes() 70 71 err = e.Send(getHost(), getAuth()) 72 73 if err != nil { 74 log.Error(fmt.Sprintf("%s - unable to send email", method), err) 75 } 76 } 77 78 // InviteExistingUser invites a known user to an organization. 79 func InviteExistingUser(recipient, inviter, url string) { 80 method := "InviteExistingUser" 81 82 file, err := web.ReadFile("mail/invite-existing-user.html") 83 84 if err != nil { 85 log.Error(fmt.Sprintf("%s - unable to load email template", method), err) 86 return 87 } 88 89 emailTemplate := string(file) 90 91 // check inviter name 92 if inviter == "Hello You" || len(inviter) == 0 { 93 inviter = "Your colleague" 94 } 95 96 subject := fmt.Sprintf("%s has invited you to their Documize account", inviter) 97 98 e := newEmail() 99 e.From = creds.SMTPsender() 100 e.To = []string{recipient} 101 e.Subject = subject 102 103 parameters := struct { 104 Subject string 105 Inviter string 106 Url string 107 }{ 108 subject, 109 inviter, 110 url, 111 } 112 113 buffer := new(bytes.Buffer) 114 t := template.Must(template.New("emailTemplate").Parse(emailTemplate)) 115 log.IfErr(t.Execute(buffer, ¶meters)) 116 e.HTML = buffer.Bytes() 117 118 err = e.Send(getHost(), getAuth()) 119 120 if err != nil { 121 log.Error(fmt.Sprintf("%s - unable to send email", method), err) 122 } 123 } 124 125 // PasswordReset sends a reset email with an embedded token. 126 func PasswordReset(recipient, url string) { 127 method := "PasswordReset" 128 129 file, err := web.ReadFile("mail/password-reset.html") 130 131 if err != nil { 132 log.Error(fmt.Sprintf("%s - unable to load email template", method), err) 133 return 134 } 135 136 emailTemplate := string(file) 137 138 subject := "Documize password reset request" 139 140 e := newEmail() 141 e.From = creds.SMTPsender() //e.g. "Documize <hello@documize.com>" 142 e.To = []string{recipient} 143 e.Subject = subject 144 145 parameters := struct { 146 Subject string 147 Url string 148 }{ 149 subject, 150 url, 151 } 152 153 buffer := new(bytes.Buffer) 154 t := template.Must(template.New("emailTemplate").Parse(emailTemplate)) 155 log.IfErr(t.Execute(buffer, ¶meters)) 156 e.HTML = buffer.Bytes() 157 158 err = e.Send(getHost(), getAuth()) 159 160 if err != nil { 161 log.Error(fmt.Sprintf("%s - unable to send email", method), err) 162 } 163 } 164 165 // ShareFolderExistingUser provides an existing user with a link to a newly shared folder. 166 func ShareFolderExistingUser(recipient, inviter, url, folder, intro string) { 167 method := "ShareFolderExistingUser" 168 169 file, err := web.ReadFile("mail/share-folder-existing-user.html") 170 171 if err != nil { 172 log.Error(fmt.Sprintf("%s - unable to load email template", method), err) 173 return 174 } 175 176 emailTemplate := string(file) 177 178 // check inviter name 179 if inviter == "Hello You" || len(inviter) == 0 { 180 inviter = "Your colleague" 181 } 182 183 subject := fmt.Sprintf("%s has shared %s with you", inviter, folder) 184 185 e := newEmail() 186 e.From = creds.SMTPsender() 187 e.To = []string{recipient} 188 e.Subject = subject 189 190 parameters := struct { 191 Subject string 192 Inviter string 193 Url string 194 Folder string 195 Intro string 196 }{ 197 subject, 198 inviter, 199 url, 200 folder, 201 intro, 202 } 203 204 buffer := new(bytes.Buffer) 205 t := template.Must(template.New("emailTemplate").Parse(emailTemplate)) 206 log.IfErr(t.Execute(buffer, ¶meters)) 207 e.HTML = buffer.Bytes() 208 209 err = e.Send(getHost(), getAuth()) 210 211 if err != nil { 212 log.Error(fmt.Sprintf("%s - unable to send email", method), err) 213 } 214 } 215 216 // ShareFolderNewUser invites new user providing credentials, explaining the product and stating who is inviting them. 217 func ShareFolderNewUser(recipient, inviter, url, folder, invitationMessage string) { 218 method := "ShareFolderNewUser" 219 220 file, err := web.ReadFile("mail/share-folder-new-user.html") 221 222 if err != nil { 223 log.Error(fmt.Sprintf("%s - unable to load email template", method), err) 224 return 225 } 226 227 emailTemplate := string(file) 228 229 // check inviter name 230 if inviter == "Hello You" || len(inviter) == 0 { 231 inviter = "Your colleague" 232 } 233 234 subject := fmt.Sprintf("%s has shared %s with you on Documize", inviter, folder) 235 236 e := newEmail() 237 e.From = creds.SMTPsender() 238 e.To = []string{recipient} 239 e.Subject = subject 240 241 parameters := struct { 242 Subject string 243 Inviter string 244 Url string 245 Invitation string 246 Folder string 247 }{ 248 subject, 249 inviter, 250 url, 251 invitationMessage, 252 folder, 253 } 254 255 buffer := new(bytes.Buffer) 256 t := template.Must(template.New("emailTemplate").Parse(emailTemplate)) 257 log.IfErr(t.Execute(buffer, ¶meters)) 258 e.HTML = buffer.Bytes() 259 260 err = e.Send(getHost(), getAuth()) 261 262 if err != nil { 263 log.Error(fmt.Sprintf("%s - unable to send email", method), err) 264 } 265 } 266 267 var creds = struct{ SMTPuserid, SMTPpassword, SMTPhost, SMTPport, SMTPsender func() string }{ 268 func() string { return request.ConfigString("SMTP", "userid") }, 269 func() string { return request.ConfigString("SMTP", "password") }, 270 func() string { return request.ConfigString("SMTP", "host") }, 271 func() string { 272 r := request.ConfigString("SMTP", "port") 273 if r == "" { 274 return "587" // default port number 275 } 276 return r 277 }, 278 func() string { return request.ConfigString("SMTP", "sender") }, 279 } 280 281 // Helper to return SMTP credentials 282 func getAuth() smtp.Auth { 283 a := smtp.PlainAuth("", creds.SMTPuserid(), creds.SMTPpassword(), creds.SMTPhost()) 284 //fmt.Printf("DEBUG getAuth() = %#v\n", a) 285 return a 286 } 287 288 // Helper to return SMTP host details 289 func getHost() string { 290 h := creds.SMTPhost() + ":" + creds.SMTPport() 291 //fmt.Printf("DEBUG getHost() = %#v\n", h) 292 return h 293 }