// Send sends the message through GMail.
// Pass in client to over-ride the http.Client used. Otherwise the default will be used
// Returns the Message-Id header for the sent email
Send sends the message through GMail.
Pass in client to over-ride the http.Client used. Otherwise the default will be used
Returns the Message-Id header for the sent email
// Send sends the message through GMail . // Pass in client to over - ride the http . Client used . Otherwise the default will be used // Returns the Message - Id header for the sent email
func (m *GoogleMessage) Send(client *http.Client) (string, error) {
	var err error
	var buf bytes.Buffer
	m.WriteTo(&buf)
	body := base64.RawURLEncoding.EncodeToString(buf.Bytes())
	var gmailMessage = &gmail.Message{Raw: body}
	var srv *gmail.Service
	if client != nil {
		srv, err = gmail.NewService(context.Background(), option.WithTokenSource(m), option.WithUserAgent("XANTDev/gmail-go"), option.WithHTTPClient(client))
	} else {
		srv, err = gmail.NewService(context.Background(), option.WithTokenSource(m), option.WithUserAgent("XANTDev/gmail-go"))
	}
	resp, err := srv.Users.Messages.Send("me", gmailMessage).Do()
	if err != nil {
		return "", err
	}
	sentMsg, err := srv.Users.Messages.Get("me", resp.Id).Do()
	if err != nil {
		return "", err
	}
	var messageID string
	if sentMsg.Payload != nil {
		if sentMsg.Payload.Headers != nil {
			for _, v := range sentMsg.Payload.Headers {
				if strings.ToLower(v.Name) == "message-id" {
					messageID = v.Value
					break
				}
			}
		}
	}
	return messageID, nil
}
func ( m * GoogleMessage ) Send ( client * http . Client ) ( string , error ) { var err error var buf bytes . Buffer m . WriteTo ( & buf ) body := base64 . RawURLEncoding . EncodeToString ( buf . Bytes ( ) ) var gmailMessage = & gmail . Message { Raw : body } var srv * gmail . Service if client != nil { srv , err = gmail . NewService ( context . Background ( ) , option . WithTokenSource ( m ) , option . WithUserAgent ( " " ) , option . WithHTTPClient ( client ) ) } else { srv , err = gmail . NewService ( context . Background ( ) , option . WithTokenSource ( m ) , option . WithUserAgent ( " " ) ) } resp , err := srv . Users . Messages . Send ( " " , gmailMessage ) . Do ( ) if err != nil { return " " , err } sentMsg , err := srv . Users . Messages . Get ( " " , resp . Id ) . Do ( ) if err != nil { return " " , err } var messageID string if sentMsg . Payload != nil { if sentMsg . Payload . Headers != nil { for _ , v := range sentMsg . Payload . Headers { if strings . ToLower ( v . Name ) == " " { messageID = v . Value break } } } } return messageID , nil }
Send sends the message through GMail.
Send sends the message through GMail .