github.com/zhongdalu/gf@v1.0.0/g/net/gsmtp/gsmtp.go (about)

     1  // Copyright 2017 gf Author(https://github.com/zhongdalu/gf). All Rights Reserved.
     2  //
     3  // This Source Code Form is subject to the terms of the MIT License.
     4  // If a copy of the MIT was not distributed with this file,
     5  // You can obtain one at https://github.com/zhongdalu/gf.
     6  
     7  // Package gsmtp provides a SMTP client to access remote mail server.
     8  //
     9  // eg:
    10  // s := smtp.New("smtp.exmail.qq.com:25", "notify@a.com", "password")
    11  // glog.Println(s.SendMail("notify@a.com", "ulric@b.com;rain@c.com", "subject", "body, <font color=red>red</font>"))
    12  package gsmtp
    13  
    14  import (
    15  	"encoding/base64"
    16  	"fmt"
    17  	"net/smtp"
    18  	"strings"
    19  )
    20  
    21  type SMTP struct {
    22  	Address  string
    23  	Username string
    24  	Password string
    25  }
    26  
    27  // New creates and returns a new SMTP object.
    28  func New(address, username, password string) *SMTP {
    29  	return &SMTP{
    30  		Address:  address,
    31  		Username: username,
    32  		Password: password,
    33  	}
    34  }
    35  
    36  // SendMail connects to the server at addr, switches to TLS if
    37  // possible, authenticates with the optional mechanism a if possible,
    38  // and then sends an email from address from, to addresses to, with
    39  // message msg.
    40  func (s *SMTP) SendMail(from, tos, subject, body string, contentType ...string) error {
    41  	if s.Address == "" {
    42  		return fmt.Errorf("address is necessary")
    43  	}
    44  
    45  	hp := strings.Split(s.Address, ":")
    46  	if len(hp) != 2 {
    47  		return fmt.Errorf("address format error")
    48  	}
    49  
    50  	arr := strings.Split(tos, ";")
    51  	count := len(arr)
    52  	safeArr := make([]string, 0, count)
    53  	for i := 0; i < count; i++ {
    54  		if arr[i] == "" {
    55  			continue
    56  		}
    57  		safeArr = append(safeArr, arr[i])
    58  	}
    59  
    60  	if len(safeArr) == 0 {
    61  		return fmt.Errorf("tos invalid")
    62  	}
    63  
    64  	tos = strings.Join(safeArr, ";")
    65  	b64 := base64.NewEncoding("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/")
    66  
    67  	header := make(map[string]string)
    68  	header["From"] = from
    69  	header["To"] = tos
    70  	header["Subject"] = fmt.Sprintf("=?UTF-8?B?%s?=", b64.EncodeToString([]byte(subject)))
    71  	header["MIME-Version"] = "1.0"
    72  
    73  	ct := "text/plain; charset=UTF-8"
    74  	if len(contentType) > 0 && contentType[0] == "html" {
    75  		ct = "text/html; charset=UTF-8"
    76  	}
    77  
    78  	header["Content-Type"] = ct
    79  	header["Content-Transfer-Encoding"] = "base64"
    80  
    81  	message := ""
    82  	for k, v := range header {
    83  		message += fmt.Sprintf("%s: %s\r\n", k, v)
    84  	}
    85  	message += "\r\n" + b64.EncodeToString([]byte(body))
    86  
    87  	auth := smtp.PlainAuth("", s.Username, s.Password, hp[0])
    88  	return smtp.SendMail(s.Address, auth, from, strings.Split(tos, ";"), []byte(message))
    89  }