当前位置: 首页>>代码示例>>Golang>>正文


Golang Client.Send方法代码示例

本文整理汇总了Golang中github.com/SparkPost/gosparkpost.Client.Send方法的典型用法代码示例。如果您正苦于以下问题:Golang Client.Send方法的具体用法?Golang Client.Send怎么用?Golang Client.Send使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在github.com/SparkPost/gosparkpost.Client的用法示例。


在下文中一共展示了Client.Send方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: TestTransmissions

func TestTransmissions(t *testing.T) {
	if true {
		// Temporarily disable test so TravisCI reports build success instead of test failure.
		return
	}

	cfgMap, err := test.LoadConfig()
	if err != nil {
		t.Error(err)
		return
	}
	cfg, err := sp.NewConfig(cfgMap)
	if err != nil {
		t.Error(err)
		return
	}

	var client sp.Client
	err = client.Init(cfg)
	if err != nil {
		t.Error(err)
		return
	}

	campaignID := "msys_smoke"
	tlist, res, err := client.Transmissions(&campaignID, nil)
	if err != nil {
		t.Error(err)
		return
	}
	t.Errorf("List: %d, %d entries", res.HTTP.StatusCode, len(tlist))
	for _, tr := range tlist {
		t.Errorf("%s: %s", tr.ID, tr.CampaignID)
	}

	// TODO: 404 from Transmission Create could mean either
	// Recipient List or Content wasn't found - open doc ticket
	// to make error message more specific

	T := &sp.Transmission{
		CampaignID: "msys_smoke",
		ReturnPath: "[email protected]",
		Recipients: []string{"[email protected]", "[email protected]"},
		// Single-recipient Transmissions are transient - Retrieve will 404
		//Recipients: []string{"[email protected]"},
		Content: sp.Content{
			Subject: "this is a test message",
			HTML:    "this is the <b>HTML</b> body of the test message",
			From: map[string]string{
				"name":  "Dave Gray",
				"email": "[email protected]",
			},
		},
		Metadata: map[string]interface{}{
			"binding": "example",
		},
	}
	err = T.Validate()
	if err != nil {
		t.Error(err)
		return
	}

	id, _, err := client.Send(T)
	if err != nil {
		t.Error(err)
		return
	}

	t.Errorf("Transmission created with id [%s]", id)

	tr, res, err := client.Transmission(id)
	if err != nil {
		t.Error(err)
		return
	}

	if res != nil {
		t.Errorf("Retrieve returned HTTP %s\n", res.HTTP.Status)
		if len(res.Errors) > 0 {
			for _, e := range res.Errors {
				json, err := e.Json()
				if err != nil {
					t.Error(err)
				}
				t.Errorf("%s\n", json)
			}
		} else {
			t.Errorf("Transmission retrieved: %s=%s\n", tr.ID, tr.State)
		}
	}

	res, err = client.TransmissionDelete(id)
	if err != nil {
		t.Error(err)
		return
	}

	t.Errorf("Delete returned HTTP %s\n%s\n", res.HTTP.Status, res.Body)

//.........这里部分代码省略.........
开发者ID:cbroglie,项目名称:gosparkpost,代码行数:101,代码来源:transmissions_test.go

示例2: main


//.........这里部分代码省略.........
	}

	headerTo := strings.Join(to, ",")

	tx.Recipients = []sp.Recipient{}
	for _, r := range to {
		var recip sp.Recipient = sp.Recipient{Address: sp.Address{Email: r, HeaderTo: headerTo}}
		if hasSubs {
			recip.SubstitutionData = subJson
		}
		tx.Recipients = append(tx.Recipients.([]sp.Recipient), recip)
	}

	if len(cc) > 0 {
		for _, r := range cc {
			var recip sp.Recipient = sp.Recipient{Address: sp.Address{Email: r, HeaderTo: headerTo}}
			if hasSubs {
				recip.SubstitutionData = subJson
			}
			tx.Recipients = append(tx.Recipients.([]sp.Recipient), recip)
		}
		if content.Headers == nil {
			content.Headers = map[string]string{}
		}
		content.Headers["cc"] = strings.Join(cc, ",")
	}

	if len(bcc) > 0 {
		for _, r := range bcc {
			var recip sp.Recipient = sp.Recipient{Address: sp.Address{Email: r, HeaderTo: headerTo}}
			if hasSubs {
				recip.SubstitutionData = subJson
			}
			tx.Recipients = append(tx.Recipients.([]sp.Recipient), recip)
		}
	}

	if len(headers) > 0 {
		if content.Headers == nil {
			content.Headers = map[string]string{}
		}
		hb := regexp.MustCompile(`:\s*`)
		for _, hstr := range headers {
			hra := hb.Split(hstr, 2)
			content.Headers[hra[0]] = hra[1]
		}
	}

	tx.Content = content

	if strings.TrimSpace(*sendDelay) != "" {
		if tx.Options == nil {
			tx.Options = &sp.TxOptions{}
		}
		dur, err := time.ParseDuration(*sendDelay)
		if err != nil {
			log.Fatal(err)
		}
		start := sp.RFC3339(time.Now().Add(dur))
		tx.Options.StartTime = &start
	}

	if *inline != false {
		if tx.Options == nil {
			tx.Options = &sp.TxOptions{}
		}
		tx.Options.InlineCSS = true
	}

	if *dryrun != false {
		jsonBytes, err := json.Marshal(tx)
		if err != nil {
			log.Fatal(err)
		}
		os.Stdout.Write(jsonBytes)
		os.Exit(0)
	}

	id, req, err := sparky.Send(tx)
	if err != nil {
		log.Fatal(err)
	}

	if *httpDump {
		if reqDump, ok := req.Verbose["http_requestdump"]; ok {
			os.Stdout.Write([]byte(reqDump))
		} else {
			os.Stdout.Write([]byte("*** No request dump available! ***\n\n"))
		}

		if resDump, ok := req.Verbose["http_responsedump"]; ok {
			os.Stdout.Write([]byte(resDump))
			os.Stdout.Write([]byte("\n"))
		} else {
			os.Stdout.Write([]byte("*** No response dump available! ***\n"))
		}
	} else {
		log.Printf("HTTP [%s] TX %s\n", req.HTTP.Status, id)
	}
}
开发者ID:SparkPost,项目名称:gosparkpost,代码行数:101,代码来源:sparks.go


注:本文中的github.com/SparkPost/gosparkpost.Client.Send方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。