Documentation ¶
Overview ¶
Package ms provides a simple email service to send emails to remote SMTP servers
Example ¶
package main import ( "crypto/rsa" "crypto/x509" "encoding/pem" "github.com/cevatbarisyilmaz/ms" "io/ioutil" "log" ) func main() { const ( domain = "yourdomain.com" // Your Domain dkimSelector = "default" // DKIM Selector To Use privateKeyPemFile = "private.key" // Location of DKIM Private Key File ) // Get the Private Key for DKIM Signatures var privateKey *rsa.PrivateKey privateKeyData, err := ioutil.ReadFile(privateKeyPemFile) if err != nil { log.Fatal("reading private key file has failed:", err) } pemBlock, _ := pem.Decode(privateKeyData) if pemBlock == nil { log.Fatal("pem block is corrupted") } privateKey, err = x509.ParsePKCS1PrivateKey(pemBlock.Bytes) if err != nil { log.Fatal("parsing der form of private key has failed:", err) } log.Println("private key is loaded from the pem file") // Create the mail service mailService := ms.New(domain, dkimSelector, privateKey) // Create a new mail mail := &ms.Mail{ Headers: map[string][]byte{ "From": []byte("\"Your Domain\" <noreply@yourdomain.com>"), "To": []byte("someuser@anotherdomain.com"), "Subject": []byte("Don't Mind Me, Just Testing Some Mail Library"), }, Body: []byte("This mail confirms that you successfully setup ms!"), } // Send the mail via mail service report, err := mailService.Send(mail) if err != nil { log.Fatal(err) } if len(report) != 0 { log.Fatal(report) } log.Println("success") }
Output:
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Mail ¶
type Mail struct { // From, To, Cc and Bcc headers should start with a capital letter followed by lower cases // Such as "From", "To" etc. Headers map[string][]byte Body []byte }
Mail holds mail headers and the body to send
type Service ¶
type Service struct {
// contains filtered or unexported fields
}
Service is used to send mails
func New ¶
New returns a new Service to send emails via domain is the associated domain name with the host dkimSelector is the DKIM selector to use with DKIM signature dkimSigner is the private key belongs to the domain and DKIM selector tuple check out README if you are not sure what DKIM is
func (*Service) Send ¶
Send sends the mail to a remote SMTP server Returns (nil, error) if there is a major error that prevented service to send any emails Returns (report, nil) if there was not a major error report is a recipient to error map which shows individual errors for each delivery if exists Recipients are email addresses of To, Cc And Bcc targets without display names such as someuser@somedomain.com a 0 length report and nil error means everything went okay