Skip to content
Browse files

SMTP Send: one domain per transaction. Fixes #88

  • Loading branch information...
1 parent 8d28945 commit d4992d12a2f65ab85c2a7fa51ddf22eb5c05e8df @dcposch committed Dec 14, 2013
Showing with 37 additions and 22 deletions.
  1. +17 −4 src/scramble/email.go
  2. +2 −2 src/scramble/handlers_rest.go
  3. +18 −16 src/scramble/smtp_client.go
View
21 src/scramble/email.go
@@ -161,29 +161,42 @@ func (addrs EmailAddresses) GroupByHost() map[string]EmailAddresses {
// Like GroupByHost, but resolves the hostname to Mx host.
// The second return value is an array of all addresses that couldn't be resolved.
-func (addrs EmailAddresses) GroupByMxHost() (map[string]EmailAddresses, EmailAddresses) {
+func (addrs EmailAddresses) GroupByMxHost() (map[string][]EmailAddresses, EmailAddresses) {
hostAddrs := addrs.GroupByHost()
- mxHostAddrs := map[string]EmailAddresses{}
+ mxHostAddrs := map[string][]EmailAddresses{}
failedAddrs := EmailAddresses{}
for host, addrs := range hostAddrs {
var mxHost string
// Skip lookup for self
// This helps with localhost testing
if host == GetConfig().SMTPMxHost {
- mxHostAddrs[host] = append(mxHostAddrs[host], addrs...)
+ mxHostAddrs[host] = append(mxHostAddrs[host], addrs)
continue
}
// Lookup Mx record
mxHost, err := mxLookUp(host)
if err != nil {
failedAddrs = append(failedAddrs, addrs...)
} else {
- mxHostAddrs[mxHost] = append(mxHostAddrs[mxHost], addrs...)
+ mxHostAddrs[mxHost] = append(mxHostAddrs[mxHost], addrs)
}
}
return mxHostAddrs, failedAddrs
}
+func (addrs EmailAddresses) GroupByMxHostFlat() (map[string]EmailAddresses, EmailAddresses) {
+ mxHostAddrs, failedAddrs := addrs.GroupByMxHost()
+ ret := make(map[string]EmailAddresses)
+ for mxHost, addrLists := range mxHostAddrs {
+ allAddrs := EmailAddresses{}
+ for _, addrs := range addrLists {
+ allAddrs = append(allAddrs, addrs...)
+ }
+ ret[mxHost] = allAddrs
+ }
+ return ret, failedAddrs
+}
+
//
// MISC
//
View
4 src/scramble/handlers_rest.go
@@ -261,7 +261,7 @@ func emailSendHandler(w http.ResponseWriter, r *http.Request, userID *UserID) {
email.To = r.FormValue("to")
// for each address, lookup MX record & determine what to do.
- mxHostAddrs, failedHostAddrs := ParseEmailAddresses(email.To).GroupByMxHost()
+ mxHostAddrs, failedHostAddrs := ParseEmailAddresses(email.To).GroupByMxHostFlat()
// fail immediately if any address cannot be resolved.
if len(failedHostAddrs) != 0 {
@@ -468,7 +468,7 @@ func publicKeysHandler(w http.ResponseWriter, r *http.Request) {
// Parse & prepare parameters
needResolution := ParseEmailAddresses(r.FormValue("needResolution"))
needPubKey := ParseEmailAddresses(r.FormValue("needPubKey"))
- needPubKeyByMxHost, needPubKeyFailedAddrs := needPubKey.GroupByMxHost()
+ needPubKeyByMxHost, needPubKeyFailedAddrs := needPubKey.GroupByMxHostFlat()
notaries := strings.Split(r.FormValue("notaries"), ",")
for _, notary := range notaries {
validateHost(notary)
View
34 src/scramble/smtp_client.go
@@ -61,24 +61,26 @@ func smtpSend(msg *OutgoingEmail) error {
// TODO: parallel send?
errs := []string{}
- for mxHost, addrs := range mxHostAddrs {
- if mxHost == GetConfig().SMTPMxHost {
- continue // don't send to self, local deliveries use different logic.
- }
- err := smtpSendTo(msg, mxHost, addrs)
- if err != nil {
- errMsg := err.Error()
- if strings.HasPrefix(errMsg, "550 5.1.1 ") {
- errMsg = errMsg[len("550 5.1.1 "):]
+ for mxHost, addrLists := range mxHostAddrs {
+ for _, addrs := range addrLists {
+ if mxHost == GetConfig().SMTPMxHost {
+ continue // don't send to self, local deliveries use different logic.
}
- if strings.HasSuffix(errMsg, ". Please try") {
- // Make the message a bit nicer for nonexistent email recipients
- errMsg = errMsg[:len(errMsg)-len(". Please try")]
+ err := smtpSendTo(msg, mxHost, addrs)
+ if err != nil {
+ errMsg := err.Error()
+ if strings.HasPrefix(errMsg, "550 5.1.1 ") {
+ errMsg = errMsg[len("550 5.1.1 "):]
+ }
+ if strings.HasSuffix(errMsg, ". Please try") {
+ // Make the message a bit nicer for nonexistent email recipients
+ errMsg = errMsg[:len(errMsg)-len(". Please try")]
+ }
+ errs = append(errs, fmt.Sprintf("Couldn't send mail to %v: %s\n",
+ addrs, errMsg))
+ } else {
+ log.Printf("Email sent to \n", mxHost)
}
- errs = append(errs, fmt.Sprintf("Couldn't send mail to %v: %s\n",
- addrs, errMsg))
- } else {
- log.Printf("Email sent to \n", mxHost)
}
}
if len(errs) > 0 {

0 comments on commit d4992d1

Please sign in to comment.
Something went wrong with that request. Please try again.