[leasing] Move to email service.

Bug: skia:13284
Change-Id: I0c84eb278e10cd17534bd3b136fe32df6b1f9dfd
Reviewed-on: https://skia-review.googlesource.com/c/buildbot/+/551640
Reviewed-by: Ravi Mistry <rmistry@google.com>
Commit-Queue: Joe Gregorio <jcgregorio@google.com>
diff --git a/leasing/go/leasing/BUILD.bazel b/leasing/go/leasing/BUILD.bazel
index 34eb2e5..af16f7a 100644
--- a/leasing/go/leasing/BUILD.bazel
+++ b/leasing/go/leasing/BUILD.bazel
@@ -12,6 +12,7 @@
     importpath = "go.skia.org/infra/leasing/go/leasing",
     visibility = ["//visibility:private"],
     deps = [
+        "//email/go/emailclient",
         "//go/allowed",
         "//go/baseapp",
         "//go/cas",
diff --git a/leasing/go/leasing/mail.go b/leasing/go/leasing/mail.go
index d7f1b49..ee59321 100644
--- a/leasing/go/leasing/mail.go
+++ b/leasing/go/leasing/mail.go
@@ -8,6 +8,7 @@
 	"fmt"
 	"strings"
 
+	"go.skia.org/infra/email/go/emailclient"
 	"go.skia.org/infra/go/email"
 	"go.skia.org/infra/go/httputils"
 	"go.skia.org/infra/go/rotations"
@@ -17,25 +18,21 @@
 const (
 	leasingEmailDisplayName = "Leasing Server"
 
+	leasingEmailAddress = "leasing@skia.org"
+
 	gmailCachedToken = "leasing_gmail_cached_token"
 
 	connectionInstructionsPage = "https://skia.org/dev/testing/swarmingbots#connecting-to-swarming-bots"
 )
 
 var (
-	gmail *email.GMail
+	mail emailclient.Client
 
 	httpClient = httputils.NewTimeoutClient()
 )
 
-func MailInit(emailClientId, emailClientSecret, tokenFile string) error {
-	var err error
-	gmail, err = email.NewGMail(emailClientId, emailClientSecret, tokenFile)
-	if err != nil {
-		return fmt.Errorf("Could not initialize gmail object: %s", err)
-	}
-
-	return nil
+func MailInit() {
+	mail = emailclient.New()
 }
 
 func getRecipients(taskOwner string) []string {
@@ -84,16 +81,7 @@
 	if err != nil {
 		return "", fmt.Errorf("Failed to get view action markup: %s", err)
 	}
-	messageId, err := gmail.SendWithMarkup(leasingEmailDisplayName, getRecipients(ownerEmail), subject, body, markup, "")
-	if err != nil {
-		return "", fmt.Errorf("Could not send start email: %s", err)
-	}
-	// Get threadingReference so that future emails can thread off it.
-	threadingReference, err := gmail.GetThreadingReference(messageId)
-	if err != nil {
-		return "", fmt.Errorf("Could not get threading data: %s", err)
-	}
-	return threadingReference, nil
+	return mail.SendWithMarkup(leasingEmailDisplayName, leasingEmailAddress, getRecipients(ownerEmail), subject, body, markup, "")
 }
 
 func SendWarningEmail(ownerEmail, swarmingServer, swarmingId, swarmingBot, threadingReference string) error {
@@ -111,7 +99,7 @@
 	if err != nil {
 		return fmt.Errorf("Failed to get view action markup: %s", err)
 	}
-	if _, err := gmail.SendWithMarkup(leasingEmailDisplayName, getRecipients(ownerEmail), subject, body, markup, threadingReference); err != nil {
+	if _, err := mail.SendWithMarkup(leasingEmailDisplayName, leasingEmailAddress, getRecipients(ownerEmail), subject, body, markup, threadingReference); err != nil {
 		return fmt.Errorf("Could not send warning email: %s", err)
 	}
 	return nil
@@ -134,7 +122,7 @@
 	if err != nil {
 		return fmt.Errorf("Failed to get view action markup: %s", err)
 	}
-	if _, err := gmail.SendWithMarkup(leasingEmailDisplayName, getRecipients(ownerEmail), subject, body, markup, threadingReference); err != nil {
+	if _, err := mail.SendWithMarkup(leasingEmailDisplayName, leasingEmailAddress, getRecipients(ownerEmail), subject, body, markup, threadingReference); err != nil {
 		return fmt.Errorf("Could not send failure email: %s", err)
 	}
 	return nil
@@ -155,7 +143,7 @@
 	if err != nil {
 		return fmt.Errorf("Failed to get view action markup: %s", err)
 	}
-	if _, err := gmail.SendWithMarkup(leasingEmailDisplayName, getRecipients(ownerEmail), subject, body, markup, threadingReference); err != nil {
+	if _, err := mail.SendWithMarkup(leasingEmailDisplayName, leasingEmailAddress, getRecipients(ownerEmail), subject, body, markup, threadingReference); err != nil {
 		return fmt.Errorf("Could not send completion email: %s", err)
 	}
 	return nil
@@ -176,7 +164,7 @@
 	if err != nil {
 		return fmt.Errorf("Failed to get view action markup: %s", err)
 	}
-	if _, err := gmail.SendWithMarkup(leasingEmailDisplayName, getRecipients(ownerEmail), subject, body, markup, threadingReference); err != nil {
+	if _, err := mail.SendWithMarkup(leasingEmailDisplayName, leasingEmailAddress, getRecipients(ownerEmail), subject, body, markup, threadingReference); err != nil {
 		return fmt.Errorf("Could not send completion email: %s", err)
 	}
 	return nil
diff --git a/leasing/go/leasing/main.go b/leasing/go/leasing/main.go
index b81d638..2ba41fc 100644
--- a/leasing/go/leasing/main.go
+++ b/leasing/go/leasing/main.go
@@ -122,9 +122,7 @@
 		}
 		*emailTokenCacheFile = fout.Name()
 	}
-	if err := MailInit(cfg.Installed.ClientID, cfg.Installed.ClientSecret, *emailTokenCacheFile); err != nil {
-		sklog.Fatalf("Failed to init mail library: %s", err)
-	}
+	MailInit()
 
 	var allow allowed.Allow
 	if !*baseapp.Local {