Documentation
¶
Overview ¶
package shared provides utility functions and structures used across the newman project
Index ¶
- Constants
- func BuildMimeMessage(message *EmailMessage) ([]byte, error)
- func GetMimeType(filename string) string
- func IsHTML(str string) bool
- func ValidateEmailAddress(email string) string
- func ValidateEmailAddresses(emails []string) []string
- func ValidateEmailMessage(msg *EmailMessage) error
- type Attachment
- func (a *Attachment) GetBase64Content() []byte
- func (a *Attachment) GetBase64StringContent() string
- func (a *Attachment) GetFilename() string
- func (a *Attachment) GetRawContent() []byte
- func (a Attachment) MarshalJSON() ([]byte, error)
- func (a *Attachment) SetContent(content []byte)
- func (a *Attachment) SetFilename(filename string)
- func (a *Attachment) UnmarshalJSON(data []byte) error
- type EmailMessage
- func (e *EmailMessage) AddAttachment(attachment *Attachment) *EmailMessage
- func (e *EmailMessage) AddBCCRecipient(recipient string) *EmailMessage
- func (e *EmailMessage) AddCCRecipient(recipient string) *EmailMessage
- func (e *EmailMessage) AddToRecipient(recipient string) *EmailMessage
- func (e *EmailMessage) GetAttachments() []*Attachment
- func (e *EmailMessage) GetBCC() []string
- func (e *EmailMessage) GetCC() []string
- func (e *EmailMessage) GetFrom() string
- func (e *EmailMessage) GetHTML() string
- func (e *EmailMessage) GetReplyTo() string
- func (e *EmailMessage) GetSubject() string
- func (e *EmailMessage) GetText() string
- func (e *EmailMessage) GetTo() []string
- func (e *EmailMessage) MarshalJSON() ([]byte, error)
- func (e *EmailMessage) SetAttachments(attachments []*Attachment) *EmailMessage
- func (e *EmailMessage) SetBCC(bcc []string) *EmailMessage
- func (e *EmailMessage) SetCC(cc []string) *EmailMessage
- func (e *EmailMessage) SetFrom(from string) *EmailMessage
- func (e *EmailMessage) SetHTML(html string) *EmailMessage
- func (e *EmailMessage) SetMaxAttachmentSize(size int) *EmailMessage
- func (e *EmailMessage) SetReplyTo(replyTo string) *EmailMessage
- func (e *EmailMessage) SetSubject(subject string) *EmailMessage
- func (e *EmailMessage) SetText(text string) *EmailMessage
- func (e *EmailMessage) SetTo(to []string) *EmailMessage
- func (e *EmailMessage) UnmarshalJSON(data []byte) error
- type MissingRequiredFieldError
- type Tag
Examples ¶
Constants ¶
const DefaultMaxAttachmentSize = 25 * 1024 * 1024 // 25 MB
Variables ¶
This section is empty.
Functions ¶
func BuildMimeMessage ¶
func BuildMimeMessage(message *EmailMessage) ([]byte, error)
BuildMimeMessage constructs the MIME message for the email, including text, HTML, and attachments
func GetMimeType ¶
GetMimeType returns the MIME type based on the file extension
Example ¶
package main
import (
"fmt"
"github.com/theopenlane/newman/shared"
)
func main() {
filename := "document.pdf"
mimeType := shared.GetMimeType(filename)
fmt.Println(mimeType)
}
Output: application/pdf
func IsHTML ¶
IsHTML checks if a string contains HTML tags
Example (False) ¶
package main
import (
"fmt"
"github.com/theopenlane/newman/shared"
)
func main() {
plainText := "Just a plain text"
result := shared.IsHTML(plainText)
fmt.Println(result)
}
Output: false
Example (PartiallyContainsHTML) ¶
package main
import (
"fmt"
"github.com/theopenlane/newman/shared"
)
func main() {
combined := "Plain text with <html> tag"
result := shared.IsHTML(combined)
fmt.Println(result)
}
Output: true
Example (True) ¶
package main
import (
"fmt"
"github.com/theopenlane/newman/shared"
)
func main() {
html := "<html><body>HTML body</body></html>"
result := shared.IsHTML(html)
fmt.Println(result)
}
Output: true
func ValidateEmailAddress ¶ added in v0.1.1
ValidateEmailAddress trims the email and checks if it is a valid email address
Example ¶
package main
import (
"fmt"
"github.com/theopenlane/newman/shared"
)
func main() {
email := "newman@usps.com"
result := shared.ValidateEmailAddress(email)
fmt.Println(result)
}
Output:
Example (Not) ¶
package main
import (
"fmt"
"github.com/theopenlane/newman/shared"
)
func main() {
email := "test@com"
result := shared.ValidateEmailAddress(email)
fmt.Println(result)
}
Output:
Example (Trim) ¶
package main
import (
"fmt"
"github.com/theopenlane/newman/shared"
)
func main() {
email := " newman@usps.com "
result := shared.ValidateEmailAddress(email)
fmt.Println(result)
}
Output:
func ValidateEmailAddresses ¶ added in v0.1.1
ValidateEmailAddresses trims and validates each email in the slice
Example ¶
package main
import (
"fmt"
"github.com/theopenlane/newman/shared"
)
func main() {
emails := []string{"newman@usps.com", "test@domain_name.com"}
result := shared.ValidateEmailAddresses(emails)
fmt.Println(result)
}
Output:
Example (Partial) ¶
package main
import (
"fmt"
"github.com/theopenlane/newman/shared"
)
func main() {
emails := []string{"newman@usps.com", "test@com"}
result := shared.ValidateEmailAddresses(emails)
fmt.Println(result)
}
Output:
func ValidateEmailMessage ¶ added in v0.1.1
func ValidateEmailMessage(msg *EmailMessage) error
ValidateEmailMessage checks that the required fields of an EmailMessage are present and valid
Types ¶
type Attachment ¶
type Attachment struct {
// filename is the name of the attachment file
Filename string
// content is the binary content of the attachment
Content []byte
// contentType is the MIME type of the file
ContentType string
// Filepath is the path to the attachment file
FilePath string
}
Attachment represents an email attachment with its filename and content
func NewAttachment ¶
func NewAttachment(filename string, content []byte) *Attachment
NewAttachment creates a new Attachment instance with the specified filename and content
func NewAttachmentFromFile ¶
func NewAttachmentFromFile(filePath string) (*Attachment, error)
NewAttachmentFromFile creates a new Attachment instance from the specified file path
func (*Attachment) GetBase64Content ¶
func (a *Attachment) GetBase64Content() []byte
GetBase64Content returns the content of the attachment as a base64-encoded byte slice
func (*Attachment) GetBase64StringContent ¶
func (a *Attachment) GetBase64StringContent() string
GetBase64StringContent returns the content of the attachment as a base64-encoded string
func (*Attachment) GetFilename ¶
func (a *Attachment) GetFilename() string
GetFilename returns the trimmed filename of the attachment
func (*Attachment) GetRawContent ¶
func (a *Attachment) GetRawContent() []byte
GetRawContent returns the content of the attachment as its raw byte slice
func (Attachment) MarshalJSON ¶
func (a Attachment) MarshalJSON() ([]byte, error)
MarshalJSON custom marshaler for Attachment
func (*Attachment) SetContent ¶
func (a *Attachment) SetContent(content []byte)
SetContent sets the content of the attachment
func (*Attachment) SetFilename ¶
func (a *Attachment) SetFilename(filename string)
SetFilename sets the filename of the attachment
func (*Attachment) UnmarshalJSON ¶
func (a *Attachment) UnmarshalJSON(data []byte) error
UnmarshalJSON custom unmarshaler for Attachment
type EmailMessage ¶
type EmailMessage struct {
// From is the email address of the sender
From string `json:"from"`
// To is the email address of the recipient
To []string `json:"to"`
// Subject is the subject of the email
Subject string `json:"subject"`
// Bcc is the email address of the blind carbon copy recipient
Bcc []string `json:"bcc,omitempty"`
// Cc is the email address of the carbon copy recipient
Cc []string `json:"cc,omitempty"`
// ReplyTo is the email address to reply to
ReplyTo string `json:"reply_to,omitempty"`
// HTML is the HTML content of the email
HTML string `json:"html,omitempty"`
// Text is the text content of the email
Text string `json:"text,omitempty"`
// Tags is the list of tags associated with the email
Tags []Tag `json:"tags,omitempty"`
// Attachments is the list of attachments associated with the email
Attachments []*Attachment `json:"attachments,omitempty"`
// Headers is the list of headers associated with the email
Headers map[string]string `json:"headers,omitempty"`
// contains filtered or unexported fields
}
EmailMessage contains the fields for sending an email
func NewEmailMessage ¶
func NewEmailMessage(from string, to []string, subject string, body string) *EmailMessage
NewEmailMessage creates a new EmailMessage with the required fields
func NewFullEmailMessage ¶
func NewFullEmailMessage(from string, to []string, subject string, cc []string, bcc []string, replyTo string, textBody string, htmlBody string, attachments []*Attachment) *EmailMessage
NewFullEmailMessage creates a new EmailMessage with all fields
func (*EmailMessage) AddAttachment ¶
func (e *EmailMessage) AddAttachment(attachment *Attachment) *EmailMessage
AddAttachment adds an attachment to the email
func (*EmailMessage) AddBCCRecipient ¶
func (e *EmailMessage) AddBCCRecipient(recipient string) *EmailMessage
AddBCCRecipient adds a recipient email address to the BCC field
func (*EmailMessage) AddCCRecipient ¶
func (e *EmailMessage) AddCCRecipient(recipient string) *EmailMessage
AddCCRecipient adds a recipient email address to the CC field
func (*EmailMessage) AddToRecipient ¶
func (e *EmailMessage) AddToRecipient(recipient string) *EmailMessage
AddToRecipient adds a recipient email address to the To field
func (*EmailMessage) GetAttachments ¶
func (e *EmailMessage) GetAttachments() []*Attachment
GetAttachments returns the attachments to be included in the email, filtering out those that exceed the maximum size
func (*EmailMessage) GetBCC ¶
func (e *EmailMessage) GetBCC() []string
GetBCC returns a slice of trimmed and validated BCC recipient email addresses
func (*EmailMessage) GetCC ¶
func (e *EmailMessage) GetCC() []string
GetCC returns a slice of trimmed and validated CC recipient email addresses
func (*EmailMessage) GetFrom ¶
func (e *EmailMessage) GetFrom() string
GetFrom returns the trimmed and validated sender email address
func (*EmailMessage) GetHTML ¶
func (e *EmailMessage) GetHTML() string
GetHTML returns the HTML content of the email
func (*EmailMessage) GetReplyTo ¶
func (e *EmailMessage) GetReplyTo() string
GetReplyTo returns the trimmed and validated reply-to email address
func (*EmailMessage) GetSubject ¶
func (e *EmailMessage) GetSubject() string
GetSubject returns the email subject
func (*EmailMessage) GetText ¶
func (e *EmailMessage) GetText() string
GetText returns the plain text content of the email
func (*EmailMessage) GetTo ¶
func (e *EmailMessage) GetTo() []string
GetTo returns a slice of trimmed and validated recipient email addresses
func (*EmailMessage) MarshalJSON ¶
func (e *EmailMessage) MarshalJSON() ([]byte, error)
MarshalJSON is a custom marshaler for EmailMessage
Example ¶
package main
import (
"encoding/json"
"fmt"
"github.com/theopenlane/newman/shared"
)
func main() {
email := shared.NewFullEmailMessage(
"newman@usps.com",
[]string{"jerry@seinfeld.com"},
"Subject",
[]string{"cc@example.com"},
[]string{"bcc@example.com"},
"replyto@example.com",
"This is the email content.",
"<p>This is the email content.</p>",
[]*shared.Attachment{
shared.NewAttachment("attachment1.txt", []byte("file content")),
},
)
jsonData, err := json.Marshal(email)
if err != nil {
fmt.Println("Error marshaling to JSON:", err)
return
}
fmt.Println("JSON output:", string(jsonData))
}
Output: JSON output: {"from":"newman@usps.com","to":["jerry@seinfeld.com"],"cc":["cc@example.com"],"bcc":["bcc@example.com"],"replyTo":"replyto@example.com","subject":"Subject","text":"This is the email content.","html":"\u003cp\u003eThis is the email content.\u003c/p\u003e","attachments":[{"filename":"attachment1.txt","content":"ZmlsZSBjb250ZW50"}]}
func (*EmailMessage) SetAttachments ¶
func (e *EmailMessage) SetAttachments(attachments []*Attachment) *EmailMessage
SetAttachments sets the attachments for the email
func (*EmailMessage) SetBCC ¶
func (e *EmailMessage) SetBCC(bcc []string) *EmailMessage
SetBCC sets the BCC recipients email addresses
func (*EmailMessage) SetCC ¶
func (e *EmailMessage) SetCC(cc []string) *EmailMessage
SetCC sets the CC recipients email addresses
func (*EmailMessage) SetFrom ¶
func (e *EmailMessage) SetFrom(from string) *EmailMessage
SetFrom sets the sender email address
func (*EmailMessage) SetHTML ¶
func (e *EmailMessage) SetHTML(html string) *EmailMessage
SetHTML sets the HTML content of the email
func (*EmailMessage) SetMaxAttachmentSize ¶
func (e *EmailMessage) SetMaxAttachmentSize(size int) *EmailMessage
SetMaxAttachmentSize sets the maximum attachment size
func (*EmailMessage) SetReplyTo ¶
func (e *EmailMessage) SetReplyTo(replyTo string) *EmailMessage
SetReplyTo sets the reply-to email address
func (*EmailMessage) SetSubject ¶
func (e *EmailMessage) SetSubject(subject string) *EmailMessage
SetSubject sets the email subject
func (*EmailMessage) SetText ¶
func (e *EmailMessage) SetText(text string) *EmailMessage
SetText sets the plain text content of the email
func (*EmailMessage) SetTo ¶
func (e *EmailMessage) SetTo(to []string) *EmailMessage
SetTo sets the recipient email addresses
func (*EmailMessage) UnmarshalJSON ¶
func (e *EmailMessage) UnmarshalJSON(data []byte) error
UnmarshalJSON is a custom unmarshaler for EmailMessage
Example ¶
package main
import (
"encoding/json"
"fmt"
"github.com/theopenlane/newman/shared"
)
func main() {
jsonInput := `{
"from": "newman@usps.com",
"to": ["jerry@seinfeld.com"],
"cc": ["cc@example.com"],
"bcc": ["bcc@example.com"],
"replyTo": "replyto@example.com",
"subject": "Subject",
"text": "This is the email content.",
"html": "<p>This is the email content.</p>",
"attachments": [{"filename": "attachment1.txt", "content": "ZmlsZSBjb250ZW50"}]
}`
var email shared.EmailMessage
err := json.Unmarshal([]byte(jsonInput), &email)
if err != nil {
fmt.Println("Error unmarshalling from JSON:", err)
return
}
jsonData, err := json.Marshal(&email)
if err != nil {
fmt.Println("Error marshaling to JSON:", err)
return
}
fmt.Println("JSON output:", string(jsonData))
}
Output: JSON output: {"from":"newman@usps.com","to":["jerry@seinfeld.com"],"cc":["cc@example.com"],"bcc":["bcc@example.com"],"replyTo":"replyto@example.com","subject":"Subject","text":"This is the email content.","html":"\u003cp\u003eThis is the email content.\u003c/p\u003e","attachments":[{"filename":"attachment1.txt","content":"ZmlsZSBjb250ZW50"}]}
type MissingRequiredFieldError ¶ added in v0.1.1
type MissingRequiredFieldError struct {
// RequiredField that is missing
RequiredField string `json:"required_field"`
}
MissingRequiredFieldError is returned when a required field was not provided in a request
func (*MissingRequiredFieldError) Error ¶ added in v0.1.1
func (e *MissingRequiredFieldError) Error() string
Error returns the InvalidEmailConfigError in string format