Skip to content

RichardKnop/go-mailchimp

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

56 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

go-mailchimp

A Golang SDK for Mailchimp API v3.

Travis Status for RichardKnop/go-mailchimp

Usage

package main

import (
	"log"

	"github.com/RichardKnop/go-mailchimp"
)

func main() {
	client, err := mailchimp.NewClient("the_api_key-us13", nil)
	if err != nil {
		log.Fatal(err)
	}

	// Check if the email is already subscribed
	memberResponse, err := client.CheckSubscription(
		"listID",
		"[email protected]",
	)

	// User is already subscribed, update the subscription
	if err == nil {
		memberResponse, err = client.UpdateSubscription(
			"listID",
			"[email protected]",
			map[string]interface{}{},
		)

		if err != nil {
			// Check the error response
			errResponse, ok := err.(*mailchimp.ErrorResponse)

			// Could not type assert error response
			if !ok {
				log.Fatal(err)
			}

			log.Fatal(errResponse)
		}

		log.Printf(
			"%s's subscription has been updated. Status: %s",
			memberResponse.EmailAddress,
			memberResponse.Status,
		)
		return
	}

	if err != nil {
		// Check the error response
		errResponse, ok := err.(*mailchimp.ErrorResponse)

		// Could not type assert error response
		if !ok {
			log.Fatal(err)
		}

		// 404 means we can process and subscribe user,
		// error other than 404 means we return error
		if errResponse.Status != http.StatusNotFound {
			log.Fatal(errResponse)
		}
	}

	// Subscribe the email
	memberResponse, err = client.Subscribe(
		"listID",
		"[email protected]",
		map[string]interface{}{},
	)

	if err != nil {
		// Check the error response
		errResponse, ok := err.(*mailchimp.ErrorResponse)

		// Could not type assert error response
		if !ok {
			log.Fatal(err)
		}

		log.Fatal(errResponse)
	}

	log.Printf(
		"%s has been subscribed successfully. Status: %s",
		memberResponse.EmailAddress,
		memberResponse.Status,
	)
}

About

Golang SDK for Mailchimp API v3.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Go 95.6%
  • Makefile 4.4%