12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667 |
- package email
- import (
- "net/smtp"
- "strconv"
- )
- /*
- Email.go
- This script handle mailing services using SMTP protocol
- */
- type Sender struct {
- Hostname string //E.g. mail.gandi.net
- Port int //E.g. 587
- Username string //Username of the email account
- Password string //Password of the email account
- SenderAddr string //e.g. [email protected]
- }
- // Create a new email sender object
- func NewEmailSender(hostname string, port int, username string, password string, senderAddr string) *Sender {
- return &Sender{
- Hostname: hostname,
- Port: port,
- Username: username,
- Password: password,
- SenderAddr: senderAddr,
- }
- }
- /*
- Send a email to a reciving addr
- Example Usage:
- SendEmail(
- [email protected],
- "Free donuts",
- "Come get your free donuts on this Sunday!"
- )
- */
- func (s *Sender) SendEmail(to string, subject string, content string) error {
- // Parse the email content
- msg := []byte("To: " + to + "\n" +
- "From: Zoraxy <" + s.SenderAddr + ">\n" +
- "Subject: " + subject + "\n" +
- "MIME-version: 1.0;\nContent-Type: text/html; charset=\"UTF-8\";\n\n" +
- content + "\n\n")
- // Initialize the auth variable
- var auth smtp.Auth
- if s.Password != "" {
- // Login to the SMTP server
- // Username can be username (e.g. admin) or email (e.g. [email protected]), depending on SMTP service provider
- auth = smtp.PlainAuth("", s.Username, s.Password, s.Hostname)
- }
- // Send the email
- err := smtp.SendMail(s.Hostname+":"+strconv.Itoa(s.Port), auth, s.SenderAddr, []string{to}, msg)
- if err != nil {
- return err
- }
- return nil
- }
|