// DeleteComplaint removes a previously registered e-mail address from the list of people who complained // of receiving spam from your domain. func (m *MailgunImpl) DeleteComplaint(address string) error { r := simplehttp.NewHTTPRequest(generateApiUrl(m, complaintsEndpoint) + "/" + address) r.SetClient(m.client) r.SetBasicAuth(basicAuthUser, m.ApiKey()) _, err := makeDeleteRequest(r) return err }
// UpdateRoute provides an "in-place" update of the specified route. // Only those route fields which are non-zero or non-empty are updated. // All other fields remain as-is. func (mg *MailgunImpl) UpdateRoute(id string, route Route) (Route, error) { r := simplehttp.NewHTTPRequest(generatePublicApiUrl(routesEndpoint) + "/" + id) r.SetClient(mg.Client()) r.SetBasicAuth(basicAuthUser, mg.ApiKey()) p := simplehttp.NewUrlEncodedPayload() if route.Priority != 0 { p.AddValue("priority", strconv.Itoa(route.Priority)) } if route.Description != "" { p.AddValue("description", route.Description) } if route.Expression != "" { p.AddValue("expression", route.Expression) } if route.Actions != nil { for _, action := range route.Actions { p.AddValue("action", action) } } // For some reason, this API function just returns a bare Route on success. // Unsure why this is the case; it seems like it ought to be a bug. var envelope Route err := putResponseFromJSON(r, p, &envelope) return envelope, err }
// RemoveUnsubscribe removes the e-mail address given from the domain's unsubscription table. // If passing in an ID (discoverable from, e.g., GetUnsubscribes()), the e-mail address associated // with the given ID will be removed. func (mg *MailgunImpl) RemoveUnsubscribe(a string) error { r := simplehttp.NewHTTPRequest(generateApiUrlWithTarget(mg, unsubscribesEndpoint, a)) r.SetClient(mg.Client()) r.SetBasicAuth(basicAuthUser, mg.ApiKey()) _, err := makeDeleteRequest(r) return err }
// GetLists returns the specified set of mailing lists administered by your account. func (mg *MailgunImpl) GetLists(limit, skip int, filter string) (int, []List, error) { r := simplehttp.NewHTTPRequest(generatePublicApiUrl(listsEndpoint)) r.SetClient(mg.Client()) r.SetBasicAuth(basicAuthUser, mg.ApiKey()) p := simplehttp.NewUrlEncodedPayload() if limit != DefaultLimit { p.AddValue("limit", strconv.Itoa(limit)) } if skip != DefaultSkip { p.AddValue("skip", strconv.Itoa(skip)) } if filter != "" { p.AddValue("address", filter) } var envelope struct { Items []List `json:"items"` TotalCount int `json:"total_count"` } response, err := makeRequest(r, "GET", p) if err != nil { return -1, nil, err } err = response.ParseFromJSON(&envelope) return envelope.TotalCount, envelope.Items, err }
// CreateList creates a new mailing list under your Mailgun account. // You need specify only the Address and Name members of the prototype; // Description, and AccessLevel are optional. // If unspecified, Description remains blank, // while AccessLevel defaults to Everyone. func (mg *MailgunImpl) CreateList(prototype List) (List, error) { r := simplehttp.NewHTTPRequest(generatePublicApiUrl(listsEndpoint)) r.SetClient(mg.Client()) r.SetBasicAuth(basicAuthUser, mg.ApiKey()) p := simplehttp.NewUrlEncodedPayload() if prototype.Address != "" { p.AddValue("address", prototype.Address) } if prototype.Name != "" { p.AddValue("name", prototype.Name) } if prototype.Description != "" { p.AddValue("description", prototype.Description) } if prototype.AccessLevel != "" { p.AddValue("access_level", prototype.AccessLevel) } response, err := makePostRequest(r, p) if err != nil { return List{}, err } var l List err = response.ParseFromJSON(&l) return l, err }
// GetMembers returns the list of members belonging to the indicated mailing list. // The s parameter can be set to one of three settings to help narrow the returned data set: // All indicates that you want both Members and unsubscribed members alike, while // Subscribed and Unsubscribed indicate you want only those eponymous subsets. func (mg *MailgunImpl) GetMembers(limit, skip int, s *bool, addr string) (int, []Member, error) { r := simplehttp.NewHTTPRequest(generateMemberApiUrl(listsEndpoint, addr)) r.SetClient(mg.Client()) r.SetBasicAuth(basicAuthUser, mg.ApiKey()) p := simplehttp.NewUrlEncodedPayload() if limit != DefaultLimit { p.AddValue("limit", strconv.Itoa(limit)) } if skip != DefaultSkip { p.AddValue("skip", strconv.Itoa(skip)) } if s != nil { p.AddValue("subscribed", yesNo(*s)) } var envelope struct { TotalCount int `json:"total_count"` Items []Member `json:"items"` } response, err := makeRequest(r, "GET", p) if err != nil { return -1, nil, err } err = response.ParseFromJSON(&envelope) return envelope.TotalCount, envelope.Items, err }
// DeleteMember removes the member from the list. func (mg *MailgunImpl) DeleteMember(member, addr string) error { r := simplehttp.NewHTTPRequest(generateMemberApiUrl(listsEndpoint, addr) + "/" + member) r.SetClient(mg.Client()) r.SetBasicAuth(basicAuthUser, mg.ApiKey()) _, err := makeDeleteRequest(r) return err }
// GetStats returns a basic set of statistics for different events. // Events start at the given start date, if one is provided. // If not, this function will consider all stated events dating to the creation of the sending domain. func (m *MailgunImpl) GetStats(limit int, skip int, startDate *time.Time, event ...string) (int, []Stat, error) { r := simplehttp.NewHTTPRequest(generateApiUrl(m, statsEndpoint)) if limit != -1 { r.AddParameter("limit", strconv.Itoa(limit)) } if skip != -1 { r.AddParameter("skip", strconv.Itoa(skip)) } if startDate != nil { r.AddParameter("start-date", startDate.Format(time.RFC3339)) } for _, e := range event { r.AddParameter("event", e) } r.SetBasicAuth(basicAuthUser, m.ApiKey()) var res statsEnvelope err := getResponseFromJSON(r, &res) if err != nil { return -1, nil, err } else { return res.TotalCount, res.Items, nil } }
// DeleteStoredMessage removes a previously stored message. // Note that Mailgun institutes a policy of automatically deleting messages after a set time. // Consult the current Mailgun API documentation for more details. func (mg *MailgunImpl) DeleteStoredMessage(id string) error { url := generateStoredMessageUrl(mg, messagesEndpoint, id) r := simplehttp.NewHTTPRequest(url) r.SetBasicAuth(basicAuthUser, mg.ApiKey()) _, err := makeDeleteRequest(r) return err }
// DeleteList removes all current members of the list, then removes the list itself. // Attempts to send e-mail to the list will fail subsequent to this call. func (mg *MailgunImpl) DeleteList(addr string) error { r := simplehttp.NewHTTPRequest(generatePublicApiUrl(listsEndpoint) + "/" + addr) r.SetClient(mg.client) r.SetBasicAuth(basicAuthUser, mg.ApiKey()) _, err := makeDeleteRequest(r) return err }
// DeleteWebhook removes the specified webhook from your domain's configuration. func (mg *MailgunImpl) DeleteWebhook(t string) error { r := simplehttp.NewHTTPRequest(generateDomainApiUrl(mg, webhooksEndpoint) + "/" + t) r.SetClient(mg.Client()) r.SetBasicAuth(basicAuthUser, mg.ApiKey()) _, err := makeDeleteRequest(r) return err }
// DeleteTag removes all counters for a particular tag, including the tag itself. func (m *MailgunImpl) DeleteTag(tag string) error { r := simplehttp.NewHTTPRequest(generateApiUrl(m, deleteTagEndpoint) + "/" + tag) r.SetClient(m.Client()) r.SetBasicAuth(basicAuthUser, m.ApiKey()) _, err := makeDeleteRequest(r) return err }
// UpdateMember lets you change certain details about the indicated mailing list member. // Address, Name, Vars, and Subscribed fields may be changed. func (mg *MailgunImpl) UpdateMember(s, l string, prototype Member) (Member, error) { r := simplehttp.NewHTTPRequest(generateMemberApiUrl(listsEndpoint, l) + "/" + s) r.SetBasicAuth(basicAuthUser, mg.ApiKey()) p := simplehttp.NewFormDataPayload() if prototype.Address != "" { p.AddValue("address", prototype.Address) } if prototype.Name != "" { p.AddValue("name", prototype.Name) } if prototype.Vars != nil { vs, err := json.Marshal(prototype.Vars) if err != nil { return Member{}, err } p.AddValue("vars", string(vs)) } if prototype.Subscribed != nil { p.AddValue("subscribed", yesNo(*prototype.Subscribed)) } response, err := makePutRequest(r, p) if err != nil { return Member{}, err } var envelope struct { Member Member `json:"member"` } err = response.ParseFromJSON(&envelope) return envelope.Member, err }
// Retrieve detailed information about the named domain. func (m *MailgunImpl) GetSingleDomain(domain string) (Domain, []DNSRecord, []DNSRecord, error) { r := simplehttp.NewHTTPRequest(generatePublicApiUrl(domainsEndpoint) + "/" + domain) r.SetBasicAuth(basicAuthUser, m.ApiKey()) var envelope singleDomainEnvelope err := getResponseFromJSON(r, &envelope) return envelope.Domain, envelope.ReceivingDNSRecords, envelope.SendingDNSRecords, err }
// GetFirstPage, GetPrevious, and GetNext all have a common body of code. // fetch completes the API fetch common to all three of these functions. func (ei *EventIterator) fetch(url string) error { r := simplehttp.NewHTTPRequest(url) r.SetClient(ei.mg.Client()) r.SetBasicAuth(basicAuthUser, ei.mg.ApiKey()) var response map[string]interface{} err := getResponseFromJSON(r, &response) if err != nil { return err } items := response["items"].([]interface{}) ei.events = make([]Event, len(items)) for i, item := range items { ei.events[i] = item.(map[string]interface{}) } pagings := response["paging"].(map[string]interface{}) links := make(map[string]string, len(pagings)) for key, page := range pagings { links[key] = page.(string) } ei.nextURL = links["next"] ei.prevURL = links["previous"] return err }
// DeleteDomain instructs Mailgun to dispose of the named domain name. func (m *MailgunImpl) DeleteDomain(name string) error { r := simplehttp.NewHTTPRequest(generatePublicApiUrl(domainsEndpoint) + "/" + name) r.SetClient(m.client) r.SetBasicAuth(basicAuthUser, m.ApiKey()) _, err := makeDeleteRequest(r) return err }
// DeleteRoute removes the specified route from your domain's configuration. // To avoid ambiguity, Mailgun identifies the route by unique ID. // See the Route structure definition and the Mailgun API documentation for more details. func (mg *MailgunImpl) DeleteRoute(id string) error { r := simplehttp.NewHTTPRequest(generatePublicApiUrl(routesEndpoint) + "/" + id) r.SetClient(mg.Client()) r.SetBasicAuth(basicAuthUser, mg.ApiKey()) _, err := makeDeleteRequest(r) return err }
// Campaigns have been deprecated since development work on this SDK commenced. // Please refer to http://documentation.mailgun.com/api_reference . func (m *MailgunImpl) DeleteCampaign(id string) error { r := simplehttp.NewHTTPRequest(generateApiUrl(m, campaignsEndpoint) + "/" + id) r.SetClient(m.client) r.SetBasicAuth(basicAuthUser, m.ApiKey()) _, err := makeDeleteRequest(r) return err }
// GetSingleBounce retrieves a single bounce record, if any exist, for the given recipient address. func (m *MailgunImpl) GetSingleBounce(address string) (Bounce, error) { r := simplehttp.NewHTTPRequest(generateApiUrl(m, bouncesEndpoint) + "/" + address) r.SetBasicAuth(basicAuthUser, m.ApiKey()) var response singleBounceEnvelope err := getResponseFromJSON(r, &response) return response.Bounce, err }
// CreateComplaint registers the specified address as a recipient who has complained of receiving spam // from your domain. func (m *MailgunImpl) CreateComplaint(address string) error { r := simplehttp.NewHTTPRequest(generateApiUrl(m, complaintsEndpoint)) r.SetBasicAuth(basicAuthUser, m.ApiKey()) p := simplehttp.NewUrlEncodedPayload() p.AddValue("address", address) _, err := makePostRequest(r, p) return err }
// UpdateWebhook replaces one webhook setting for another. func (mg *MailgunImpl) UpdateWebhook(t, u string) error { r := simplehttp.NewHTTPRequest(generateDomainApiUrl(mg, webhooksEndpoint) + "/" + t) r.SetBasicAuth(basicAuthUser, mg.ApiKey()) p := simplehttp.NewUrlEncodedPayload() p.AddValue("url", u) _, err := makePutRequest(r, p) return err }
// GetSingleComplaint returns a single complaint record filed by a recipient at the email address provided. // If no complaint exists, the Complaint instance returned will be empty. func (m *MailgunImpl) GetSingleComplaint(address string) (Complaint, error) { r := simplehttp.NewHTTPRequest(generateApiUrl(m, complaintsEndpoint) + "/" + address) r.SetBasicAuth(basicAuthUser, m.ApiKey()) var c Complaint err := getResponseFromJSON(r, &c) return c, err }
// GetStoredMessage retrieves information about a received e-mail message. // This provides visibility into, e.g., replies to a message sent to a mailing list. func (mg *MailgunImpl) GetStoredMessage(id string) (StoredMessage, error) { url := generateStoredMessageUrl(mg, messagesEndpoint, id) r := simplehttp.NewHTTPRequest(url) r.SetBasicAuth(basicAuthUser, mg.ApiKey()) var response StoredMessage err := getResponseFromJSON(r, &response) return response, err }
// DeleteCredential attempts to remove the indicated principle from the domain. func (mg *MailgunImpl) DeleteCredential(id string) error { if id == "" { return ErrEmptyParam } r := simplehttp.NewHTTPRequest(generateCredentialsUrl(mg, id)) r.SetBasicAuth(basicAuthUser, mg.ApiKey()) _, err := makeDeleteRequest(r) return err }
// Unsubscribe adds an e-mail address to the domain's unsubscription table. func (mg *MailgunImpl) Unsubscribe(a, t string) error { r := simplehttp.NewHTTPRequest(generateApiUrl(mg, unsubscribesEndpoint)) r.SetBasicAuth(basicAuthUser, mg.ApiKey()) p := simplehttp.NewUrlEncodedPayload() p.AddValue("address", a) p.AddValue("tag", t) _, err := makePostRequest(r, p) return err }
// GetUnsubscribesByAddress retrieves a list of unsubscriptions by recipient address. // Zero is a valid list length. func (mg *MailgunImpl) GetUnsubscribesByAddress(a string) (int, []Unsubscription, error) { r := simplehttp.NewHTTPRequest(generateApiUrlWithTarget(mg, unsubscribesEndpoint, a)) r.SetBasicAuth(basicAuthUser, mg.ApiKey()) var envelope struct { TotalCount int `json:"total_count"` Items []Unsubscription `json:"items"` } err := getResponseFromJSON(r, &envelope) return envelope.TotalCount, envelope.Items, err }
// GetRouteByID retrieves the complete route definition associated with the unique route ID. func (mg *MailgunImpl) GetRouteByID(id string) (Route, error) { r := simplehttp.NewHTTPRequest(generatePublicApiUrl(routesEndpoint) + "/" + id) r.SetBasicAuth(basicAuthUser, mg.ApiKey()) var envelope struct { Message string `json:"message"` *Route `json:"route"` } err := getResponseFromJSON(r, &envelope) return *envelope.Route, err }
// GetListByAddress allows your application to recover the complete List structure // representing a mailing list, so long as you have its e-mail address. func (mg *MailgunImpl) GetListByAddress(addr string) (List, error) { r := simplehttp.NewHTTPRequest(generatePublicApiUrl(listsEndpoint) + "/" + addr) r.SetBasicAuth(basicAuthUser, mg.ApiKey()) response, err := makeGetRequest(r) var envelope struct { List `json:"list"` } err = response.ParseFromJSON(&envelope) return envelope.List, err }
// ChangeCredentialPassword attempts to alter the indicated credential's password. func (mg *MailgunImpl) ChangeCredentialPassword(id, password string) error { if (id == "") || (password == "") { return ErrEmptyParam } r := simplehttp.NewHTTPRequest(generateCredentialsUrl(mg, id)) r.SetBasicAuth(basicAuthUser, mg.ApiKey()) p := simplehttp.NewUrlEncodedPayload() p.AddValue("password", password) _, err := makePutRequest(r, p) return err }
// GetWebhookByType retrieves the currently assigned webhook URL associated with the provided type of webhook. func (mg *MailgunImpl) GetWebhookByType(t string) (string, error) { r := simplehttp.NewHTTPRequest(generateDomainApiUrl(mg, webhooksEndpoint) + "/" + t) r.SetBasicAuth(basicAuthUser, mg.ApiKey()) var envelope struct { Webhook struct { Url *string `json:"url"` } `json:"webhook"` } err := getResponseFromJSON(r, &envelope) return *envelope.Webhook.Url, err }