排第四偶家 发表于 2018-9-20 07:44:32

依据 smtp协议的简单golang 的发邮件实现

package email  import (
  "bytes"
  "encoding/base64"
  "fmt"
  "io/ioutil"
  "net/smtp"
  "strings"
  "time"
  )
  //SendEmailWithAttachment : send email with attachment
  func SendEmailWithAttachment(user, passwd, host, to, subject string) error {
  hp := strings.Split(host, ":")
  auth := smtp.PlainAuth("", user, passwd, hp)
  buffer := bytes.NewBuffer(nil)
  boudary := "THIS_IS_BOUNDARY_JUST_MAKE_YOURS"
  header := fmt.Sprintf("To:%s\r\n"+
  "From:%s\r\n"+
  "Subject:%s\r\n"+
  "Content-Type:multipart/mixed;Boundary=\"%s\"\r\n"+
  "Mime-Version:1.0\r\n"+
  "Date:%s\r\n", to, user, subject, boudary, time.Now().String())
  buffer.WriteString(header)
  fmt.Print(header)
  msg1 := "\r\n\r\n--" + boudary + "\r\n" + "Content-Type:text/plain;\r\n\r\n这是正文啊\r\n"
  buffer.WriteString(msg1)
  fmt.Print(msg1)
  msg2 := fmt.Sprintf(
  "\r\n--%s\r\n"+
  "Content-Transfer-Encoding: base64\r\n"+
  "Content-Disposition: attachment;\r\n"+
  "Content-Type:image/jpg;name=\"test.jpg\"\r\n", boudary)
  buffer.WriteString(msg2)
  fmt.Print(msg2)
  attachmentBytes, err := ioutil.ReadFile("./test.jpg")
  if err != nil {
  fmt.Println("ReadFile ./test.jpg Error : " + err.Error())
  return err
  }
  b := make([]byte, base64.StdEncoding.EncodedLen(len(attachmentBytes)))
  base64.StdEncoding.Encode(b, attachmentBytes)
  buffer.WriteString("\r\n")
  fmt.Print("\r\n")
  fmt.Print("图片base64编码")
  for i, l := 0, len(b); i < l; i++ {
  buffer.WriteByte(b)
  if (i+1)%76 == 0 {
  buffer.WriteString(&quot;\r\n&quot;)
  }
  }
  buffer.WriteString(&quot;\r\n--&quot; + boudary + &quot;--&quot;)
  fmt.Print(&quot;\r\n--&quot; + boudary + &quot;--&quot;)
  sendto := strings.Split(to, &quot;;&quot;)
  err = smtp.SendMail(host, auth, user, sendto, buffer.Bytes())
  return err
  }

页: [1]
查看完整版本: 依据 smtp协议的简单golang 的发邮件实现