//formAlias, subject, htmlBody, textBody are optional func (this *Client) SendSingleMail(accountName, replyToAddress, addressType, toAddress, formAlias, subject, htmlBody, textBody string) error { initMap := this.newParamMap() initMap.Add("Action", "SingleSendMail") initMap.Add("AccountName", accountName) initMap.Add("ReplyToAddress", replyToAddress) initMap.Add("AddressType", addressType) initMap.Add("ToAddress", toAddress) if formAlias != "" { initMap.Add("FormAlias", formAlias) } if subject != "" { initMap.Add("Subject", subject) } if htmlBody != "" { initMap.Add("HtmlBody", htmlBody) } if textBody != "" { initMap.Add("TextBody", textBody) } signature := util.CreateSignatureForRequest("GET", initMap, this.accessKeySecret+"&") initMap.Add("Signature", signature) finalUrl := Url + "?" + initMap.Encode() println(finalUrl) if rsp, rspErr := http.Get(finalUrl); nil != rspErr { return rspErr } else { defer rsp.Body.Close() body, err := ioutil.ReadAll(rsp.Body) if rsp.StatusCode > 400 { return errors.New(string(body)) } if err != nil { // handle error return err } //only print the request id for debuging println(string(body)) return nil } }
//remember to setup the accountName in your aliyun console //addressType should be "1" or "0", //0:random address, it's recommanded //1:sender's address //tagName is optional, you can use "" if you don't wanna use it //please set the receiverName and template in the console of Aliyun before you call this API,if you use tagName, you should set it as well func (this *Client) SendBatchMail(accountName, addressType, templateName, receiverName, tagName string) error { initMap := this.newParamMap() if addressType != "0" && addressType != "1" { return errors.New("invalid addressType") } initMap.Add("Action", "BatchSendMail") initMap.Add("AccountName", accountName) initMap.Add("AddressType", addressType) initMap.Add("TemplateName", templateName) initMap.Add("ReceiversName", receiverName) if tagName != "" { initMap.Add("TagName", tagName) } signature := util.CreateSignatureForRequest("GET", initMap, this.accessKeySecret+"&") initMap.Add("Signature", signature) finalUrl := Url + "?" + initMap.Encode() println(finalUrl) if rsp, rspErr := http.Get(finalUrl); nil != rspErr { return rspErr } else { defer rsp.Body.Close() body, err := ioutil.ReadAll(rsp.Body) if rsp.StatusCode > 400 { return errors.New(string(body)) } if err != nil { // handle error return err } //only print the request id for debuging println(string(body)) return nil } }
//please set the signature and template in the console of Aliyun before you call this API func (this *Client) SendSms(signatureId, templateId, recNum string, paramMap map[string]string) error { initMap := this.newParamMap() if bytes, err := json.Marshal(paramMap); nil != err { println("marsh error") } else { value := string(bytes) initMap.Add("ParamString", value) } initMap.Add("Action", "SingleSendSms") initMap.Add("SignName", signatureId) initMap.Add("TemplateCode", templateId) initMap.Add("RecNum", recNum) signature := util.CreateSignatureForRequest("GET", initMap, this.accessKeySecret+"&") initMap.Add("Signature", signature) finalUrl := Url + "?" + initMap.Encode() println(finalUrl) if rsp, rspErr := http.Get(finalUrl); nil != rspErr { return rspErr } else { defer rsp.Body.Close() body, err := ioutil.ReadAll(rsp.Body) if rsp.StatusCode > 400 { return errors.New(string(body)) } if err != nil { // handle error return err } //only print the request id for debuging println(string(body)) return nil } }
// Invoke sends the raw HTTP request for ECS services func (client *Client) Invoke(action string, args interface{}, response interface{}) error { request := Request{} request.init(action, client.AccessKeyId) query := util.ConvertToQueryValues(request) util.SetQueryValues(args, &query) // Sign request signature := util.CreateSignatureForRequest(ECSRequestMethod, &query, client.AccessKeySecret) // Generate the request URL requestURL := ECSDefaultEndpoint + "?" + query.Encode() + "&Signature=" + url.QueryEscape(signature) httpReq, err := http.NewRequest(ECSRequestMethod, requestURL, nil) if err != nil { return getECSError(err) } t0 := time.Now() httpResp, err := client.httpClient.Do(httpReq) t1 := time.Now() if err != nil { return getECSError(err) } statusCode := httpResp.StatusCode if client.debug { log.Printf("Invoke %s %s %d (%v)", ECSRequestMethod, requestURL, statusCode, t1.Sub(t0)) } defer httpResp.Body.Close() body, err := ioutil.ReadAll(httpResp.Body) if err != nil { return getECSError(err) } if client.debug { var prettyJSON bytes.Buffer err = json.Indent(&prettyJSON, body, "", " ") log.Println(string(prettyJSON.Bytes())) } if statusCode >= 400 && statusCode <= 599 { errorResponse := ErrorResponse{} err = json.Unmarshal(body, &errorResponse) ecsError := &Error{ ErrorResponse: errorResponse, StatusCode: statusCode, } return ecsError } err = json.Unmarshal(body, response) //log.Printf("%++v", response) if err != nil { return getECSError(err) } return nil }