package carrier
import "petersanchez.com/x/carrier"
Package carrier implements sending email via various services with a single message object.
This package will panic if you pass in email addresses that are invalid.
Please be sure to validate your input before passing it in.
Code:play
Example¶
package main
import (
"log"
"os"
"petersanchez.com/x/carrier"
)
func main() {
svc := carrier.NewConsoleService()
msg := carrier.NewMessage()
msg.SetFrom("me@mydomain.com").
SetTo("recipient@theirdomain.com").
SetCc("copy@somedomain.com")
msg.SetSubject("Sending email from Go!")
// You should handle errors properly in all instances below
file, err := os.Open("funny.jpg")
if err != nil {
log.Fatal(err)
}
msg.AddAttachment("funny.jpg", file, "")
file.Close()
err = msg.SetBody("This is the text email body.")
err = msg.SetBodyHTML("This is the HTML email body.")
// Send email
err = svc.Send(msg)
log.Println("Successfully sent email.")
}
Index ¶
- func SendMail(to string, from string, subject string, text string, html string, svc Service) error
- type ConsoleService
- type Message
- func NewMessage() *Message
- func (m *Message) AddAttachment(filename string, data io.Reader, ctype string) error
- func (m *Message) AddPGPKeys(keys *PGPKeys) error
- func (m *Message) Buffer() *bytes.Buffer
- func (m *Message) Bytes() []byte
- func (m *Message) Close()
- func (m *Message) GetAllRcpts() []string
- func (m *Message) SendMail(svc Service) error
- func (m *Message) SetBcc(address string) *Message
- func (m *Message) SetBccCSV(address string) *Message
- func (m *Message) SetBccList(addresses []string) *Message
- func (m *Message) SetBody(body string) error
- func (m *Message) SetBodyHTML(body string) error
- func (m *Message) SetCc(address string) *Message
- func (m *Message) SetCcCSV(address string) *Message
- func (m *Message) SetCcList(addresses []string) *Message
- func (m *Message) SetFrom(address string) *Message
- func (m *Message) SetHTML(msg io.Reader) error
- func (m *Message) SetText(msg io.Reader) error
- func (m *Message) SetTo(address string) *Message
- func (m *Message) SetToCSV(address string) *Message
- func (m *Message) SetToList(addresses []string) *Message
- func (m *Message) String() string
- type OutboxService
- type PGPKeys
- type Service
Examples ¶
Functions ¶
func SendMail ¶
SendMail is a quick helper to send out an email using a given service
Types ¶
type ConsoleService ¶
type ConsoleService struct {
// contains filtered or unexported fields
}
ConsoleService is a simple email service to print email to the set writer
func NewConsoleService ¶
func NewConsoleService() *ConsoleService
NewConsoleService returns an smtp service instance
func (*ConsoleService) Send ¶
func (c *ConsoleService) Send(msg *Message) error
Send match email Service interface signature
type Message ¶
Message object which holds the entire email message. Each service will take a *Message object and use it to parse out the required data to send the email.
func NewMessage ¶
func NewMessage() *Message
NewMessage returns a new *Message object
func (*Message) AddAttachment ¶
AddAttachment Adds an attachment to the message. If ctype is blank the func will try to guess the content-type
func (*Message) AddPGPKeys ¶
AddPGPKeys sets the PGP keys for this message. If you plan to sign or encrypt the final email than this MUST be called before writing any message body. An error is raised if you try to assign keys after the body has been opened.
func (*Message) Buffer ¶
Buffer Returns the raw email buffer
func (*Message) Bytes ¶
Bytes Returns full email as bytes
func (*Message) Close ¶
func (m *Message) Close()
Close Closes the mail.Writer
func (*Message) GetAllRcpts ¶
GetAllRcpts Returns all email recipients
func (*Message) SendMail ¶
SendMail Sends the message as an email via the given service.
func (*Message) SetBcc ¶
SetBcc Set the Bcc: header
func (*Message) SetBccCSV ¶
SetBccCSV Set Bcc: header. See SetToCSV comment
func (*Message) SetBccList ¶
SetBccList Set the Bcc: header to multiple addresses
func (*Message) SetBody ¶
SetBody Set the text/plain part of the email
func (*Message) SetBodyHTML ¶
SetBodyHTML Set the text/html part of the email
func (*Message) SetCc ¶
SetCc Set the Cc: header
func (*Message) SetCcCSV ¶
SetCcCSV Set Cc: header. See SetToCSV comment.
func (*Message) SetCcList ¶
SetCcList Set the Cc: address header to multiple addresses
func (*Message) SetFrom ¶
SetFrom Set the From: header
func (*Message) SetHTML ¶
SetHTML Set the text/html part of the email, via io.Reader instance
func (*Message) SetText ¶
SetText Set the text/plain part of the email, via io.Reader instance
func (*Message) SetTo ¶
SetTo Set the To: header
func (*Message) SetToCSV ¶
SetToCSV Set the To: header from a comma separated list of emails. Ie, user1@foobar.com, user2@foobar.com, etc.
func (*Message) SetToList ¶
SetToList Set the To: header to multiple addresses
func (*Message) String ¶
String Returns full email as string
type OutboxService ¶
type OutboxService struct {
// contains filtered or unexported fields
}
OutboxService is a simple email service to store messages in a slice for use in tests, etc.
func NewOutboxService ¶
func NewOutboxService() *OutboxService
NewOutboxService returns an smtp service instance
func (*OutboxService) Send ¶
func (c *OutboxService) Send(msg *Message) error
Send match email Service interface signature
type PGPKeys ¶
type PGPKeys struct {
// contains filtered or unexported fields
}
PGPKeys holds signer and recipient PGP keys
Code:play
Example¶
package main
import (
"log"
"os"
"petersanchez.com/x/carrier"
)
func main() {
// You should handle errors properly in all instances below
privkey, err := os.Open("privkey.asc")
if err != nil {
panic(err)
}
defer privkey.Close()
// public key is optional. If present, the email will encrypted
// for the recipient using the public key
pubkey, err := os.Open("pubkey.asc")
defer pubkey.Close()
// Give pubkey if you want the email encrypted. If just providing
// the private key, the email will be signed with the given key.
keys, err := carrier.NewPGPKeys(privkey, nil)
svc := carrier.NewConsoleService()
msg := carrier.NewMessage().
SetFrom("me@mydomain.com").
SetTo("recipient@theirdomain.com").
SetCc("copy@somedomain.com")
msg.SetSubject("Sending email from Go!")
// You MUST assign keys before setting the body.
err = msg.AddPGPKeys(keys)
err = msg.SetBody("This is the text email body.")
err = msg.SetBodyHTML("This is the HTML email body.")
// Send email
err = svc.Send(msg)
log.Println("Successfully sent email.")
}
func NewPGPKeys ¶
NewPGPKeys returns a PGPKey instance. If rkey is empty then the final email this PGPKeys instance is used for will only be signed.
type Service ¶
Service interface
Source Files ¶
carrier.go console.go message.go outbox.go send.go
- Version
- v0.2.3 (latest)
- Published
- Jan 14, 2025
- Platform
- linux/amd64
- Imports
- 12 packages
- Last checked
- 18 hours ago –
Tools for package owners.