ews

package module
v0.0.0-...-542cc43 Latest Latest
Warning

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

Go to latest
Published: Mar 1, 2019 License: MIT Imports: 6 Imported by: 0

README

Golang wrapper for send email functionality with Exchange server via EWS

Credits: andlabs

Note: Currently only a single attachment is supported

Example usage

package main

import (
	"encoding/base64"
	"fmt"
	"io/ioutil"
	"log"

	"github.com/arawal/ews"
)

func main() {
	fileData, err := ioutil.ReadFile("sample.txt")
	fileEncoding := base64.StdEncoding.EncodeToString(fileData)

	var amd ews.AttachmentMetadata
	amd.Name = "some.txt"
	amd.Content = fileEncoding

	var email ews.EmailMetadata
	email.To = []string{"1@some.org", "2@some.org"}
	email.Subject = "sample"
	email.Body = "sample email body"
	email.Cc = []string{"3@some.org", "4@some.org"}
	email.Bcc = []string{"5@some.org", "6@some.org"}
	email.ReplyTo = "7@some.org"

	var creds ews.Credentials
	creds.Server = "https://outlook.office365.com/EWS/exchange.asmx"
	creds.Username = "yourusername"
	creds.Password = "yourpassword"

	resp, err := ews.SendEmailWithAttachment(creds, email, amd)
	fmt.Println(resp, err)
	return

	fileData, err = ioutil.ReadFile("sample.txt")
	if err != nil {
		log.Fatal(err)
	}

	fileEncoding = base64.StdEncoding.EncodeToString(fileData)
	fmt.Printf("File contents: %s", fileEncoding)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BuildAttachment

func BuildAttachment(metadata AttachmentMetadata) ([]byte, error)

func BuildSendSavedEmail

func BuildSendSavedEmail(emailId, changeKey string) ([]byte, error)

func BuildTextEmail

func BuildTextEmail(metadata EmailMetadata) ([]byte, error)

func Issue

func Issue(creds Credentials, body []byte) (string, error)

func IssueAttachment

func IssueAttachment(creds Credentials, metadata AttachmentMetadata) (string, error)

func IssueEmailWithAttachment

func IssueEmailWithAttachment(creds Credentials, metadata AttachmentMetadata) (string, error)

func IssueTextEmail

func IssueTextEmail(creds Credentials, metadata EmailMetadata) (string, string, string, error)

func SendEmail

func SendEmail(creds Credentials, metadata EmailMetadata) (string, error)

func SendEmailWithAttachment

func SendEmailWithAttachment(creds Credentials, emailMetadata EmailMetadata, attachmentMetadata AttachmentMetadata) (string, error)

Types

type Attachment

type Attachment struct {
	FileAttachment FileAttachment `xml:"t:FileAttachment"`
}

type AttachmentMetadata

type AttachmentMetadata struct {
	Name           string
	Content        string
	EmailID        string
	EmailChangeKey string
}

type Body

type Body struct {
	BodyType string `xml:"BodyType,attr"`
	Body     []byte `xml:",chardata"`
}

type CreateAttachmentResponse

type CreateAttachmentResponse struct {
	XMLName xml.Name `xml:"Envelope"`
	Body    struct {
		CreateAttachmentResponseMessage struct {
			ResponseClass string `xml:"ResponseClass,attr"`
			ResponseCode  string `xml:"ResponseCode"`
			Attachments   struct {
				FileAttachment []struct {
					AttachmentID struct {
						ID                string `xml:"Id,attr"`
						RootItemID        string `xml:"RootItemId,attr"`
						RootItemChangeKey string `xml:"RootItemChangeKey,attr"`
					} `xml:"AttachmentId"`
					LastModifiedTime string `xml:"LastModifiedTime"`
				} `xml:"FileAttachment"`
			} `xml:"Attachments"`
		} `xml:"CreateAttachmentResponse>ResponseMessages>CreateAttachmentResponseMessage"`
	} `xml:"Body"`
}

type CreateItem

type CreateItem struct {
	XMLName            struct{}          `xml:"m:CreateItem"`
	MessageDisposition string            `xml:"MessageDisposition,attr"`
	SavedItemFolderID  SavedItemFolderID `xml:"m:SavedItemFolderId"`
	Items              Messages          `xml:"m:Items"`
}

func CreateItemXML

func CreateItemXML(metadata EmailMetadata) CreateItem

type CreateItemResponse

type CreateItemResponse struct {
	XMLName xml.Name `xml:"Envelope"`
	Body    struct {
		CreateItemResponseMessage struct {
			ResponseClass string `xml:"ResponseClass,attr"`
			ResponseCode  string `xml:"ResponseCode"`
			Items         struct {
				Message []struct {
					ItemID struct {
						ID        string `xml:"Id,attr"`
						ChangeKey string `xml:"ChangeKey,attr"`
					} `xml:"ItemId"`
				} `xml:"Message"`
			} `xml:"Items"`
		} `xml:"CreateItemResponse>ResponseMessages>CreateItemResponseMessage"`
	} `xml:"Body"`
}

type Credentials

type Credentials struct {
	Server   string
	Username string
	Password string
}

type DistinguishedFolderID

type DistinguishedFolderID struct {
	ID string `xml:"Id,attr"`
}

type EmailAttachment

type EmailAttachment struct {
	XMLName      xml.Name     `xml:"CreateAttachment"`
	Xmlns        string       `xml:"xmlns,attr"`
	XmlnsT       string       `xml:"xmlns:t,attr"`
	ParentItemID ItemID       `xml:"ParentItemId"`
	Attachments  []Attachment `xml:"Attachments"`
}

type EmailMetadata

type EmailMetadata struct {
	Action  string // SaveOnly or SendAndSaveCopy
	To      []string
	Cc      []string
	Bcc     []string
	ReplyTo string
	Subject string
	Body    string
	Type    string // Text or HTML
	Folder  string
}

type FileAttachment

type FileAttachment struct {
	Name           string `xml:"t:Name"`
	IsInline       bool   `xml:"t:IsInline"`
	IsContactPhoto bool   `xml:"t:IsContactPhoto"`
	Content        string `xml:"t:Content"`
}

type ItemID

type ItemID struct {
	ID        string `xml:"Id,attr"`
	ChangeKey string `xml:"ChangeKey,attr"`
}

type ItemIDs

type ItemIDs struct {
	ItemID []ItemID `xml:"t:ItemId"`
}

type Mailbox

type Mailbox struct {
	EmailAddress string `xml:"t:EmailAddress"`
}

type Message

type Message struct {
	ItemClass   string       `xml:"t:ItemClass"`
	Subject     string       `xml:"t:Subject"`
	Body        Body         `xml:"t:Body"`
	Attachments []Attachment `xml:"t:Attachments"`
	// Sender       OneMailbox   `xml:"t:Sender"`
	ToRecipients  XMailbox   `xml:"t:ToRecipients"`
	CcRecipients  XMailbox   `xml:"t:CcRecipients"`
	BccRecipients XMailbox   `xml:"t:BccRecipients"`
	ReplyTo       OneMailbox `xml:"t:ReplyTo"`
}

type Messages

type Messages struct {
	Message []Message `xml:"t:Message"`
}

type OneMailbox

type OneMailbox struct {
	Mailbox Mailbox `xml:"t:Mailbox"`
}

type SavedItemFolderID

type SavedItemFolderID struct {
	DistinguishedFolderID DistinguishedFolderID `xml:"t:DistinguishedFolderId"`
}

type SendItem

type SendItem struct {
	XMLName           xml.Name `xml:"SendItem"`
	Xmlns             string   `xml:"xmlns,attr"`
	SaveItemToFolder  bool     `xml:"SaveItemToFolder,attr"`
	ItemIds           ItemIDs  `xml:"ItemIds"`
	SavedItemFolderID string   `xml:"t:SavedItemFolderId"`
}

type SendItemResponse

type SendItemResponse struct {
	XMLName xml.Name `xml:"Envelope"`
	Body    struct {
		SendItemResponse struct {
			ResponseMessages struct {
				SendItemResponseMessage struct {
					ResponseClass string `xml:"ResponseClass,attr"`
					ResponseCode  string `xml:"ResponseCode"`
				} `xml:"SendItemResponseMessage"`
			} `xml:"ResponseMessages"`
		} `xml:"SendItemResponse"`
	} `xml:"Body"`
}

type XMailbox

type XMailbox struct {
	Mailbox []Mailbox `xml:"t:Mailbox"`
}

Jump to

Keyboard shortcuts

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