sendcloud

package module
v0.0.0-...-b415f80 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 23, 2024 License: Apache-2.0 Imports: 14 Imported by: 0

README

sendcloud-email-go

This Go client provides an interface to interact with SendCloud's email delivery service. It exposes two main methods: SendCommonEmail for sending regular emails and SendTemplateEmail for sending emails using predefined templates.

Supported Go Versions

This SDK supports Go 1.16 and above.

SendCommonEmail

The SendCommonEmail method allows you to send a regular email with a custom subject, body, and recipient list.

Usage
package main  
  
import (
	"context"
	"fmt"
	"github.com/frank-email-sms/sendcloud-email-go"
	"log"
	"time"
)

func main() {
	client, err := sendcloud.NewSendCloud("*", "*")
	if err != nil {
		log.Fatal(err)
	}
	ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
	defer cancel()
	args :=  &sendcloud.CommonMail{
		Receiver: sendcloud.MailReceiver{
			To: "a@ifaxin.com;b@ifaxin.com",
		},
		Body: sendcloud.MailBody{
			From: "SendCloud@SendCloud.com",
			Subject: "Email from SendCloud SDK",
			FromName: "SendCloud",
		},
		Content: sendcloud.TextContent{
			Html:  "<p>This is an HTML email.</p>",
		},
	}
	result, err := client.SendCommonEmail(ctx, args)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(result)
}

SendTemplateEmail

The SendTemplateEmail method allows you to send an email using a predefined template. This is useful when you want to send emails with consistent design and layout.

Usage
package main  
  
import (
	"context"
	"fmt"
	"github.com/frank-email-sms/sendcloud-email-go"
	"log"
	"time"
)

func main() {
	client, err := sendcloud.NewSendCloud("*", "*")
	if err != nil {
		log.Fatal(err)
	}
	ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
	defer cancel()
	args :=  &sendcloud.TemplateMail{
		Receiver: sendcloud.MailReceiver{
			To: "a@ifaxin.com;b@ifaxin.com",
		},
		Body: sendcloud.MailBody{
			From: "SendCloud@SendCloud.com",
			Subject: "Email from SendCloud SDK",
			FromName: "SendCloud",
		},
		Content: sendcloud.TemplateContent{
			TemplateInvokeName:  "test_template_active",
		},
	}
	result, err := client.SendTemplateEmail(ctx, args)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(result)
}

Code Examples

All code examples for this project are located in the root directory in the sendcloud_test.go file. This file contains tests that demonstrate the usage of the email sending functionality and can also be used as examples for integration into your own code.

How to View the Examples

To view the code examples, simply open the sendcloud_test.go file in the root directory of the project. You can also run the tests in this file to see the functionality in action.

Running the Tests

To run the tests in sendcloud_test.go, navigate to the root directory of the project and run the following command:

Configuration

Before using the client, make sure to initialize it with the necessary configuration such as API keys or other credentials. The exact configuration steps may vary depending on the SendCloud client package you are using.

Handling Errors

Always make sure to handle errors returned by the methods. They may indicate issues such as invalid credentials, API errors, or other problems that need to be addressed.

Result Handling

The result returned by the methods contains information about the success or failure of the email sending operation. Make sure to check the status code and message to understand the outcome.

Documentation

Index

Constants

View Source
const (
	APIBase = "https://api.sendcloud.net/apiv2/mail"
)
View Source
const MAX_MAILLIST = 5
View Source
const MAX_RECEIVERS = 100

Variables

This section is empty.

Functions

This section is empty.

Types

type CalendarMail

type CalendarMail struct {
	Receiver MailReceiver
	Body     MailBody
	Content  TextContent
	Calendar MailCalendar
}

type CommonMail

type CommonMail struct {
	Receiver MailReceiver
	Body     MailBody
	Content  TextContent
}

type ErrorResponse

type ErrorResponse struct {
	Response *http.Response // HTTP response that caused this error
	Message  string         `json:"message"` // error message
}

func (*ErrorResponse) Error

func (r *ErrorResponse) Error() string

type Filter

type Filter struct {
	SubscriptionTracking TrackingFilter `json:"subscription_tracking"`
	OpenTracking         TrackingFilter `json:"open_tracking"`
	ClickTracking        TrackingFilter `json:"click_tracking"`
}

func (Filter) ValidateFilter

func (f Filter) ValidateFilter() error

type FilterSettings

type FilterSettings struct {
	Enable string `json:"enable"`
}

type MailBody

type MailBody struct {
	From            string
	Subject         string
	ContentSummary  string
	FromName        string
	ReplyTo         string
	LabelName       string
	Headers         map[string]string
	Attachments     []*os.File
	Xsmtpapi        XSMTPAPI
	SendRequestID   string
	RespEmailID     bool
	UseNotification bool
}

func (*MailBody) AddAttachment

func (e *MailBody) AddAttachment(attachment *os.File)

AddAttachment - Add an attachment content.

func (*MailBody) AddHeaders

func (e *MailBody) AddHeaders(headers map[string]string)

AddHeaders - Add the headers of the email.

func (*MailBody) PrepareMailBodyParams

func (e *MailBody) PrepareMailBodyParams(params *url.Values)

func (*MailBody) SetContentSummary

func (e *MailBody) SetContentSummary(contentSummary string)

SetContentSummary - Set the content summary of the email.

func (*MailBody) SetFrom

func (e *MailBody) SetFrom(from string)

SetFrom - Set the from address.

func (*MailBody) SetFromName

func (e *MailBody) SetFromName(fromName string)

SetFromName - Set the from name of the email.

func (*MailBody) SetLabelName

func (e *MailBody) SetLabelName(labelName string)

SetLabelName - Set the label name of the email.

func (*MailBody) SetReplyTo

func (e *MailBody) SetReplyTo(replyTo string)

SetReplyTo - Set the reply to address.

func (*MailBody) SetXsmtpapi

func (e *MailBody) SetXsmtpapi(xsmtpapi XSMTPAPI)

SetXsmtpapi - Set the xsmtpapi of the email.

type MailCalendar

type MailCalendar struct {
	StartTime          time.Time
	EndTime            time.Time
	Title              string
	OrganizerName      string
	OrganizerEmail     string
	Location           string
	Description        string
	ParticipatorNames  string
	ParticipatorEmails string
	UID                string
	IsCancel           bool
	IsUpdate           bool
	ValarmTime         int
}

func (*MailCalendar) PrepareMailCalendarParams

func (e *MailCalendar) PrepareMailCalendarParams(params *url.Values)

func (*MailCalendar) SetDescription

func (e *MailCalendar) SetDescription(description string)

SetDescription - Set the description of the calendar.

func (*MailCalendar) SetEndTime

func (e *MailCalendar) SetEndTime(endTime time.Time)

SetEndTime - Set the end time of the calendar.

func (*MailCalendar) SetIsCancel

func (e *MailCalendar) SetIsCancel(isCancel bool)

SetIsCancel - Set the isCancel of the calendar.

func (*MailCalendar) SetIsUpdate

func (e *MailCalendar) SetIsUpdate(isUpdate bool)

SetIsUpdate - Set the isUpdate of the calendar.

func (*MailCalendar) SetLocation

func (e *MailCalendar) SetLocation(location string)

SetLocation - Set the location of the calendar.

func (*MailCalendar) SetOrganizerEmail

func (e *MailCalendar) SetOrganizerEmail(organizerEmail string)

SetOrganizerEmail - Set the organizer email of the calendar.

func (*MailCalendar) SetOrganizerName

func (e *MailCalendar) SetOrganizerName(organizerName string)

SetOrganizerName - Set the organizer name of the calendar.

func (*MailCalendar) SetParticipatorEmails

func (e *MailCalendar) SetParticipatorEmails(participatorEmails string)

SetParticipatorEmails - Set the participator emails of the calendar.

func (*MailCalendar) SetParticipatorNames

func (e *MailCalendar) SetParticipatorNames(participatorNames string)

SetParticipatorNames - Set the participator names of the calendar.

func (*MailCalendar) SetStartTime

func (e *MailCalendar) SetStartTime(startTime time.Time)

SetStartTime - Set the start time of the calendar.

func (*MailCalendar) SetTitle

func (e *MailCalendar) SetTitle(title string)

SetTitle - Set the title of the calendar.

func (*MailCalendar) SetUID

func (e *MailCalendar) SetUID(uid string)

SetUID - Set the UID of the calendar.

func (*MailCalendar) SetValarmTime

func (e *MailCalendar) SetValarmTime(valarmTime int)

SetValarmTime - Set the valarmTime of the calendar.

type MailReceiver

type MailReceiver struct {
	To             string
	CC             string
	BCC            string
	UseAddressList bool
}

type Response

type Response struct {
	*http.Response
}

type SendCloud

type SendCloud struct {
	// contains filtered or unexported fields
}

func NewSendCloud

func NewSendCloud(apiUser string, apiKey string) (*SendCloud, error)

func (*SendCloud) MultipartSendCalendarMail

func (client *SendCloud) MultipartSendCalendarMail(e *CalendarMail) (*multipart.Writer, *bytes.Buffer, error)

func (*SendCloud) MultipartSendCommonMail

func (client *SendCloud) MultipartSendCommonMail(e *CommonMail) (*multipart.Writer, *bytes.Buffer, error)

func (*SendCloud) MultipartSendTemplateEmail

func (client *SendCloud) MultipartSendTemplateEmail(e *TemplateMail) (*multipart.Writer, *bytes.Buffer, error)

func (*SendCloud) PrepareReceiverParams

func (client *SendCloud) PrepareReceiverParams(e *MailReceiver) url.Values

func (*SendCloud) PrepareSendCalendarMailParams

func (client *SendCloud) PrepareSendCalendarMailParams(e *CalendarMail) url.Values

func (*SendCloud) PrepareSendCommonEmailParams

func (client *SendCloud) PrepareSendCommonEmailParams(e *CommonMail) url.Values

func (*SendCloud) PrepareSendTemplateEmailParams

func (client *SendCloud) PrepareSendTemplateEmailParams(e *TemplateMail) url.Values

func (*SendCloud) SendCalendarMail

func (client *SendCloud) SendCalendarMail(ctx context.Context, args *CalendarMail) (*SendEmailResult, error)

func (*SendCloud) SendCommonEmail

func (client *SendCloud) SendCommonEmail(ctx context.Context, args *CommonMail) (*SendEmailResult, error)

func (*SendCloud) SendTemplateEmail

func (client *SendCloud) SendTemplateEmail(ctx context.Context, args *TemplateMail) (*SendEmailResult, error)

type SendEmailResult

type SendEmailResult struct {
	Result     bool        `json:"result"`
	StatusCode int         `json:"statusCode"`
	Message    string      `json:"message"`
	Info       interface{} `json:"info"`
}

type Settings

type Settings struct {
	Unsubscribe UnsubscribeSettings `json:"unsubscribe"`
}

Settings 表示设置的结构体

type TemplateContent

type TemplateContent struct {
	TemplateInvokeName string
}

func (*TemplateContent) SetTemplateInvokeName

func (e *TemplateContent) SetTemplateInvokeName(name string)

SetTemplateInvokeName - Set the template invoke name.

type TemplateMail

type TemplateMail struct {
	Receiver MailReceiver
	Body     MailBody
	Content  TemplateContent
}

type TextContent

type TextContent struct {
	Html  string
	Plain string
}

func (*TextContent) SetHTML

func (e *TextContent) SetHTML(html string)

SetHTML - Set the html content of the email, required if not using a template.

func (*TextContent) SetPlain

func (e *TextContent) SetPlain(plain string)

SetPlain - Set the plain content of the email, required if not using a template.

type TrackingFilter

type TrackingFilter struct {
	Settings FilterSettings `json:"settings"`
}

type UnsubscribeSettings

type UnsubscribeSettings struct {
	PageID []int `json:"page_id"`
}

UnsubscribeSettings 表示退订设置的结构体

type XSMTPAPI

type XSMTPAPI struct {
	To       []string                 `json:"to,omitempty"`
	Sub      map[string][]interface{} `json:"sub,omitempty"`
	Pubsub   map[string]interface{}   `json:"pubsub,omitempty"`
	Filters  *Filter                  `json:"filters,omitempty"`
	Settings *Settings                `json:"settings,omitempty"`
}

func (XSMTPAPI) IsEmpty

func (x XSMTPAPI) IsEmpty() bool

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL