示例#1
0
// 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.SetClient(mg.Client())
	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
}
示例#2
0
// CreateMemberList registers multiple Members and non-Member members to a single mailing list
// in a single round-trip.
// s indicates the default subscribed status (Subscribed or Unsubscribed).
// Use All to elect not to provide a default.
// The newMembers list can take one of two JSON-encodable forms: an slice of strings, or
// a slice of Member structures.
// If a simple slice of strings is passed, each string refers to the member's e-mail address.
// Otherwise, each Member needs to have at least the Address field filled out.
// Other fields are optional, but may be set according to your needs.
func (mg *MailgunImpl) CreateMemberList(s *bool, addr string, newMembers []interface{}) error {
	r := simplehttp.NewHTTPRequest(generateMemberApiUrl(listsEndpoint, addr) + ".json")
	r.SetClient(mg.Client())
	r.SetBasicAuth(basicAuthUser, mg.ApiKey())
	p := simplehttp.NewFormDataPayload()
	if s != nil {
		p.AddValue("subscribed", yesNo(*s))
	}
	bs, err := json.Marshal(newMembers)
	if err != nil {
		return err
	}
	fmt.Println(string(bs))
	p.AddValue("members", string(bs))
	_, err = makePostRequest(r, p)
	return err
}
示例#3
0
// CreateMember registers a new member of the indicated mailing list.
// If merge is set to true, then the registration may update an existing Member's settings.
// Otherwise, an error will occur if you attempt to add a member with a duplicate e-mail address.
func (mg *MailgunImpl) CreateMember(merge bool, addr string, prototype Member) error {
	vs, err := json.Marshal(prototype.Vars)
	if err != nil {
		return err
	}

	r := simplehttp.NewHTTPRequest(generateMemberApiUrl(listsEndpoint, addr))
	r.SetClient(mg.Client())
	r.SetBasicAuth(basicAuthUser, mg.ApiKey())
	p := simplehttp.NewFormDataPayload()
	p.AddValue("upsert", yesNo(merge))
	p.AddValue("address", prototype.Address)
	p.AddValue("name", prototype.Name)
	p.AddValue("vars", string(vs))
	if prototype.Subscribed != nil {
		p.AddValue("subscribed", yesNo(*prototype.Subscribed))
	}
	_, err = makePostRequest(r, p)
	return err
}
示例#4
0
// Send attempts to queue a message (see Message, NewMessage, and its methods) for delivery.
// It returns the Mailgun server response, which consists of two components:
// a human-readable status message, and a message ID.  The status and message ID are set only
// if no error occurred.
func (m *MailgunImpl) Send(message *Message) (mes string, id string, err error) {
	if !isValid(message) {
		err = errors.New("Message not valid")
	} else {
		payload := simplehttp.NewFormDataPayload()

		message.specific.addValues(payload)
		for _, to := range message.to {
			payload.AddValue("to", to)
		}
		for _, tag := range message.tags {
			payload.AddValue("o:tag", tag)
		}
		for _, campaign := range message.campaigns {
			payload.AddValue("o:campaign", campaign)
		}
		if message.dkimSet {
			payload.AddValue("o:dkim", yesNo(message.dkim))
		}
		if message.deliveryTime != nil {
			payload.AddValue("o:deliverytime", formatMailgunTime(message.deliveryTime))
		}
		if message.testMode {
			payload.AddValue("o:testmode", "yes")
		}
		if message.trackingSet {
			payload.AddValue("o:tracking", yesNo(message.tracking))
		}
		if message.trackingClicksSet {
			payload.AddValue("o:tracking-clicks", yesNo(message.trackingClicks))
		}
		if message.trackingOpensSet {
			payload.AddValue("o:tracking-opens", yesNo(message.trackingOpens))
		}
		if message.headers != nil {
			for header, value := range message.headers {
				payload.AddValue("h:"+header, value)
			}
		}
		if message.variables != nil {
			for variable, value := range message.variables {
				payload.AddValue("v:"+variable, value)
			}
		}
		if message.recipientVariables != nil {
			j, err := json.Marshal(message.recipientVariables)
			if err != nil {
				return "", "", err
			}
			payload.AddValue("recipient-variables", string(j))
		}
		if message.attachments != nil {
			for _, attachment := range message.attachments {
				payload.AddFile("attachment", attachment)
			}
		}
		if message.readerAttachments != nil {
			for _, readerAttachment := range message.readerAttachments {
				payload.AddReadCloser("attachment", readerAttachment.Filename, readerAttachment.ReadCloser)
			}
		}
		if message.inlines != nil {
			for _, inline := range message.inlines {
				payload.AddFile("inline", inline)
			}
		}

		r := simplehttp.NewHTTPRequest(generateApiUrl(m, message.specific.endpoint()))
		r.SetClient(m.Client())
		r.SetBasicAuth(basicAuthUser, m.ApiKey())

		var response sendMessageResponse
		err = postResponseFromJSON(r, payload, &response)
		if err == nil {
			mes = response.Message
			id = response.Id
		}
	}

	return
}