借用网上一段go代码,实现了带附件的邮件发送,可附件是直接作为内容显示的? 如何让附件作为下载?

下面代码是实现了邮件发送,可是附件是作为内容发送的,怎么作为邮件附件让下载啊?

package main

import (
    "net/smtp"
    "bytes"
    "encoding/base64"
    "fmt"
    "io/ioutil"
    "path/filepath"
    "strings"
    "time"
) 

type Attachment struct {
    Filename string
    Data     []byte
    Inline   bool
}
type Message struct {
    From            string
    To              []string
    Cc              []string
    Bcc             []string
    Subject         string
    Body            string
    BodyContentType string
    Attachments     map[string]*Attachment
}

func (m *Message) attach(file string, inline bool) error {
    data, err := ioutil.ReadFile(file)
    if err != nil {
        return err
    }

    _, filename := filepath.Split(file)

    m.Attachments[filename] = &Attachment{
        Filename: filename,
        Data:     data,
        Inline:   inline,
    }

    return nil
}

func (m *Message) Attach(file string) error {
    return m.attach(file, false)
}

func (m *Message) Inline(file string) error {
    return m.attach(file, true)
}

func newMessage(subject string, body string, bodyContentType string) *Message {
    m := &Message{Subject: subject, Body: body, BodyContentType: bodyContentType}
    m.Attachments = make(map[string]*Attachment)
    return m
}

// NewMessage returns a new Message that can compose an email with attachments
func NewMessage(subject string, body string) *Message {
    return newMessage(subject, body, "text/plain")
}

// NewMessage returns a new Message that can compose an HTML email with attachments
func NewHTMLMessage(subject string, body string) *Message {
    return newMessage(subject, body, "text/html")
}

// ToList returns all the recipients of the email
func (m *Message) Tolist() []string {
    tolist := m.To
    for _, cc := range m.Cc {
        tolist = append(tolist, cc)
    }
    for _, bcc := range m.Bcc {
        tolist = append(tolist, bcc)
    }
    return tolist
}

// Bytes returns the mail data
func (m *Message) Bytes() []byte {
    buf := bytes.NewBuffer(nil)
    buf.WriteString("From: " + m.From + "\n")
    t := time.Now()
    buf.WriteString("Date: " + t.Format(time.RFC822) + "\n")
    buf.WriteString("To: " + strings.Join(m.To, ",") + "\n")
    if len(m.Cc) > 0 {
        buf.WriteString("Cc: " + strings.Join(m.Cc, ",") + "\n")
    }
    buf.WriteString("Subject: " + m.Subject + "\n")
    buf.WriteString("MIME-Version: 1.0\n")

    boundary := "f46d043c813270fc6b04c2d223da"

    if len(m.Attachments) > 0 {
        buf.WriteString("Content-Type: multipart/mixed; boundary=" + boundary + "\n")
        buf.WriteString("--" + boundary + "\n")
    }

    buf.WriteString(fmt.Sprintf("Content-Type: %s; charset=utf-8\n\n", m.BodyContentType))
    buf.WriteString(m.Body)
    buf.WriteString("\n")

    if len(m.Attachments) > 0 {
        for _, attachment := range m.Attachments {
            buf.WriteString("\r\n--" + boundary + "\n\n")
            if attachment.Inline {
                buf.WriteString("Content-Type: message/rfc822\n")
                buf.WriteString("Content-Disposition: inline; filename=\"" + attachment.Filename + "\"\n\n")
                buf.Write(attachment.Data)
            } else {
                buf.WriteString("Content-Type: application/octet-stream\n")
                buf.WriteString("Content-Transfer-Encoding: base64\n")
                buf.WriteString("Content-Disposition: attachment; filename=\"" + attachment.Filename + "\"\n\n")
                b := make([]byte, base64.StdEncoding.EncodedLen(len(attachment.Data)))
                base64.StdEncoding.Encode(b, attachment.Data)
                buf.Write(b)
            }

            buf.WriteString("\n--" + boundary)
        }
        buf.WriteString("--")
    }
    return buf.Bytes()
}

func SendMSG(addr string, auth smtp.Auth, m *Message) error {
    return smtp.SendMail(addr, auth, m.From, m.Tolist(), m.Bytes())
}

func main() {
    m := NewHTMLMessage("我的文件email by golang", "this is the body")
    m.Body="测试内容<a href=main.go>aaaa</a>"
    m.From = "myname@example.com"
    m.To = []string{"aa@example.com"}
    m.Cc = []string{"bb@example.com", "cc@example.com"}
    //err := m.Attach("main.go")
    err := m.Inline("main.go")
    if err != nil {
        panic(err)
    }

    err = SendMSG("smtp.example.com:25", smtp.PlainAuth("", "myname@example.com", "mypassword", "smtp.example.com"), m)

    if err != nil {
        panic(err)
    } 
}

共 1 个回复


heimeil

自己给自己qq邮箱发个带附件的邮件,qq邮箱网页版可以查看邮件原文

# 0