1package mailgun 2 3import ( 4 "strconv" 5 "time" 6) 7 8// DefaultLimit and DefaultSkip instruct the SDK to rely on Mailgun's reasonable defaults for Paging settings. 9const ( 10 DefaultLimit = -1 11 DefaultSkip = -1 12) 13 14// Disabled, Tag, and Delete indicate spam actions. 15// Disabled prevents Mailgun from taking any action on what it perceives to be spam. 16// Tag instruments the received message with headers providing a measure of its spamness. 17// Delete instructs Mailgun to just block or delete the message all-together. 18const ( 19 Tag = "tag" 20 Disabled = "disabled" 21 Delete = "delete" 22) 23 24// A Domain structure holds information about a domain used when sending mail. 25// The SpamAction field must be one of Tag, Disabled, or Delete. 26type Domain struct { 27 CreatedAt string `json:"created_at"` 28 SMTPLogin string `json:"smtp_login"` 29 Name string `json:"name"` 30 SMTPPassword string `json:"smtp_password"` 31 Wildcard bool `json:"wildcard"` 32 SpamAction string `json:"spam_action"` 33} 34 35// DNSRecord structures describe intended records to properly configure your domain for use with Mailgun. 36// Note that Mailgun does not host DNS records. 37type DNSRecord struct { 38 Priority string 39 RecordType string `json:"record_type"` 40 Valid string 41 Name string 42 Value string 43} 44 45type domainsEnvelope struct { 46 TotalCount int `json:"total_count"` 47 Items []Domain `json:"items"` 48} 49 50type singleDomainEnvelope struct { 51 Domain Domain `json:"domain"` 52 ReceivingDNSRecords []DNSRecord `json:"receiving_dns_records"` 53 SendingDNSRecords []DNSRecord `json:"sending_dns_records"` 54} 55 56// GetCreatedAt returns the time the domain was created as a normal Go time.Time type. 57func (d Domain) GetCreatedAt() (t time.Time, err error) { 58 t, err = parseMailgunTime(d.CreatedAt) 59 return 60} 61 62// GetDomains retrieves a set of domains from Mailgun. 63// 64// Assuming no error, both the number of items retrieved and a slice of Domain instances. 65// The number of items returned may be less than the specified limit, if it's specified. 66// Note that zero items and a zero-length slice do not necessarily imply an error occurred. 67// Except for the error itself, all results are undefined in the event of an error. 68func (m *MailgunImpl) GetDomains(limit, skip int) (int, []Domain, error) { 69 r := newHTTPRequest(generatePublicApiUrl(m, domainsEndpoint)) 70 r.setClient(m.Client()) 71 if limit != DefaultLimit { 72 r.addParameter("limit", strconv.Itoa(limit)) 73 } 74 if skip != DefaultSkip { 75 r.addParameter("skip", strconv.Itoa(skip)) 76 } 77 r.setBasicAuth(basicAuthUser, m.ApiKey()) 78 79 var envelope domainsEnvelope 80 err := getResponseFromJSON(r, &envelope) 81 if err != nil { 82 return -1, nil, err 83 } 84 return envelope.TotalCount, envelope.Items, nil 85} 86 87// Retrieve detailed information about the named domain. 88func (m *MailgunImpl) GetSingleDomain(domain string) (Domain, []DNSRecord, []DNSRecord, error) { 89 r := newHTTPRequest(generatePublicApiUrl(m, domainsEndpoint) + "/" + domain) 90 r.setClient(m.Client()) 91 r.setBasicAuth(basicAuthUser, m.ApiKey()) 92 var envelope singleDomainEnvelope 93 err := getResponseFromJSON(r, &envelope) 94 return envelope.Domain, envelope.ReceivingDNSRecords, envelope.SendingDNSRecords, err 95} 96 97// CreateDomain instructs Mailgun to create a new domain for your account. 98// The name parameter identifies the domain. 99// The smtpPassword parameter provides an access credential for the domain. 100// The spamAction domain must be one of Delete, Tag, or Disabled. 101// The wildcard parameter instructs Mailgun to treat all subdomains of this domain uniformly if true, 102// and as different domains if false. 103func (m *MailgunImpl) CreateDomain(name string, smtpPassword string, spamAction string, wildcard bool) error { 104 r := newHTTPRequest(generatePublicApiUrl(m, domainsEndpoint)) 105 r.setClient(m.Client()) 106 r.setBasicAuth(basicAuthUser, m.ApiKey()) 107 108 payload := newUrlEncodedPayload() 109 payload.addValue("name", name) 110 payload.addValue("smtp_password", smtpPassword) 111 payload.addValue("spam_action", spamAction) 112 payload.addValue("wildcard", strconv.FormatBool(wildcard)) 113 _, err := makePostRequest(r, payload) 114 return err 115} 116 117// DeleteDomain instructs Mailgun to dispose of the named domain name. 118func (m *MailgunImpl) DeleteDomain(name string) error { 119 r := newHTTPRequest(generatePublicApiUrl(m, domainsEndpoint) + "/" + name) 120 r.setClient(m.Client()) 121 r.setBasicAuth(basicAuthUser, m.ApiKey()) 122 _, err := makeDeleteRequest(r) 123 return err 124} 125