go-mail
Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Back to homepage

简介

This short tutorial shows you how to get up and running with go-mail from installation to sending your first mail.

要求

go-mail requires a working Go installation (Version 1.16+). Download Go from the Go Downloads Page. 从Go下载页面下载Go。 从Go下载页面下载Go。

安装

可以使用Go模块安装机制通过go get命令安装go-mail。

To install the latest version of go-mail, enter your project folder and simply import the module by issuing the following command:

1
$ go get github.com/wneessen/go-mail

发送您的第一封邮件

go-mail由两个主要组件组成。 go-mail consists of two main components. The Msg which represents the mail message and the Client which takes care of the mail delivery via a SMTP service.

创建新消息

First let’s create a new Msg using the NewMsg() method and assign a sender address as well as a recipient address.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
package main

import (
    "github.com/wneessen/go-mail"
    "log"
)

func main() {
    m := mail.NewMsg()
    if err := m.From("toni.sender@example.com"); err != nil {
        log.Fatalf("failed to set From address: %s", err)
    }
    if err := m.To("tina.recipient@example.com"); err != nil {
        log.Fatalf("failed to set To address: %s", err)
    }
}

在这个小代码片段中,首先我们将go-mail导入我们的项目。 请参见第4行中的import语句。 接下来,我们在第9行中创建了一个新消息。 第1013行设置了发件人和收件人地址。 由于go-mail确保您提供的是有效的邮件地址,因此我们返回一个error。 这样我们就可以确保go-mail接受提供的地址,并且不会在以后引起问题。

接下来,我们要为我们的邮件设置一个主题行,并填充邮件正文内容。

1
2
m.Subject("This is my first mail with go-mail!")
m.SetBodyString(mail.TypeTextPlain, "Do you like this mail? I certainly do!")

The first argument for SetBodyString() is a content type we need to provide. In our example the mail.TypeTextPlain basically represents a text/plain content type - meaning a plain text mail body. 在我们的示例中,mail.TypeTextPlain基本上表示text/plain内容类型-表示纯文本邮件正文。 在我们的示例中,mail.TypeTextPlain基本上表示text/plain内容类型-表示纯文本邮件正文。

发送邮件

现在我们已经准备好发送邮件消息了,让我们将其发送出去。 Now that we have our mail message ready to go, let’s bring it on the way and send it out. For this we’ll use the Client, which handles the SMTP transmission.

1
2
3
4
5
c, err := mail.NewClient("smtp.example.com", mail.WithPort(25), mail.WithSMTPAuth(mail.SMTPAuthPlain), 
    mail.WithUsername("my_username"), mail.WithPassword("extremely_secret_pass"))
if err != nil {
    log.Fatalf("failed to create mail client: %s", err)
}

In this example we connect to the mail server with the hostname smtp.example.com and provide the Client with a couple of options like the port we want to connect to, the fact that we want to use SMTP PLAIN for authentication and the username and password.

最后,我们告诉客户端交付邮件。

1
2
3
if err := c.DialAndSend(m); err != nil {
    log.Fatalf("failed to send mail: %s", err)
}

The DialAndSend() method takes care of establishing the connection and sending out the mail. You have the option to call them separately as well, but we won’t need this for the quick example. 您也可以分别调用它们,但是我们不需要快速示例。 您也可以分别调用它们,但是我们不需要快速示例。

结论

那很简单,不是吗? 您成功地准备了一封邮件消息,并通过第三方邮件服务器将其发送给收件人。 当然,go-mail可以做更多。 查看详细文档以获取所有功能。

完整示例代码

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
func main() {
    m := mail.NewMsg()
    if err := m.From("toni.sender@example.com"); err != nil {
        log.Fatalf("failed to set From address: %s", err)
    }
    if err := m.To("tina.recipient@example.com"); err != nil {
        log.Fatalf("failed to set To address: %s", err)
    }
    m.Subject("This is my first mail with go-mail!")
    m.SetBodyString(mail.TypeTextPlain, "Do you like this mail? I certainly do!")
    c, err := mail.NewClient("smtp.example.com", mail.WithPort(25), mail.WithSMTPAuth(mail.SMTPAuthPlain),
        mail.WithUsername("my_username"), mail.WithPassword("extremely_secret_pass"))
    if err != nil {
        log.Fatalf("failed to create mail client: %s", err)
    }
    if err := c.DialAndSend(m); err != nil {
        log.Fatalf("failed to send mail: %s", err)
    }
}
    m.Subject("This is my first mail with go-mail!")
m.SetBodyString(mail.TypeTextPlain, "Do you like this mail? I certainly do!") I certainly do!")
    c, err := mail.NewClient("smtp.example.com", mail.WithPort(25), mail.WithSMTPAuth(mail.SMTPAuthPlain),
        mail.WithUsername("my_username"), mail.WithPassword("extremely_secret_pass"))
    if err != nil {
        log.Fatalf("failed to create mail client: %s", err)
    }
    if err := c.DialAndSend(m); err != nil {
        log.Fatalf("failed to send mail: %s", err)
    }
}
    m.Subject("This is my first mail with go-mail!")
m.SetBodyString(mail.TypeTextPlain, "Do you like this mail? I certainly do!") I certainly do!")
    func main() {
    m := mail.NewMsg()
    if err := m.From("toni.sender@example.com"); err != nil {
        log.Fatalf("failed to set From address: %s", err)
    }
    if err := m.To("tina.recipient@example.com"); err != nil {
        log.Fatalf("failed to set To address: %s", err)
    }
    m.Subject("This is my first mail with go-mail!")
    m.SetBodyString(mail.TypeTextPlain, "Do you like this mail? I certainly do!")
    c, err := mail.NewClient("smtp.example.com", mail.WithPort(25), mail.WithSMTPAuth(mail.SMTPAuthPlain),
        mail.WithUsername("my_username"), mail.WithPassword("extremely_secret_pass"))
    if err != nil {
        log.Fatalf("failed to create mail client: %s", err)
    }
    if err := c.DialAndSend(m); err != nil {
        log.Fatalf("failed to send mail: %s", err)
    }
}
    m.Subject("This is my first mail with go-mail!")
m.SetBodyString(mail.TypeTextPlain, "Do you like this mail? I certainly do!") I certainly do!")
    c, err := mail.NewClient("smtp.example.com", mail.WithPort(25), mail.WithSMTPAuth(mail.SMTPAuthPlain),
        mail.WithUsername("my_username"), mail.WithPassword("extremely_secret_pass"))
    if err != nil {
        log.Fatalf("failed to create mail client: %s", err)
    }
    if err := c.DialAndSend(m); err != nil {
        log.Fatalf("failed to send mail: %s", err)
    }
}
    m.Subject("This is my first mail with go-mail!")
m.SetBodyString(mail.TypeTextPlain, "Do you like this mail? I certainly do!") I certainly do!")
    c, err := mail.NewClient("smtp.example.com", mail.WithPort(25), mail.WithSMTPAuth(mail.SMTPAuthPlain),
        mail.WithUsername("my_username"), mail.WithPassword("extremely_secret_pass"))
    if err != nil {
        log.Fatalf("failed to create mail client: %s", err)
    }
    if err := c.DialAndSend(m); err != nil {
        log.Fatalf("failed to send mail: %s", err)
    }
}
    m.Subject("This is my first mail with go-mail!")
m.SetBodyString(mail.TypeTextPlain, "Do you like this mail? I certainly do!") I certainly do!")
    func main() {
    m := mail.NewMsg()
    if err := m.From("toni.sender@example.com"); err != nil {
        log.Fatalf("failed to set From address: %s", err)
    }
    if err := m.To("tina.recipient@example.com"); err != nil {
        log.Fatalf("failed to set To address: %s", err)
    }
    m.Subject("This is my first mail with go-mail!")
    m.SetBodyString(mail.TypeTextPlain, "Do you like this mail? I certainly do!")
    c, err := mail.NewClient("smtp.example.com", mail.WithPort(25), mail.WithSMTPAuth(mail.SMTPAuthPlain),
        mail.WithUsername("my_username"), mail.WithPassword("extremely_secret_pass"))
    if err != nil {
        log.Fatalf("failed to create mail client: %s", err)
    }
    if err := c.DialAndSend(m); err != nil {
        log.Fatalf("failed to send mail: %s", err)
    }
}
    m.Subject("This is my first mail with go-mail!")
m.SetBodyString(mail.TypeTextPlain, "Do you like this mail? I certainly do!") I certainly do!")
    c, err := mail.NewClient("smtp.example.com", mail.WithPort(25), mail.WithSMTPAuth(mail.SMTPAuthPlain),
        mail.WithUsername("my_username"), mail.WithPassword("extremely_secret_pass"))
    if err != nil {
        log.Fatalf("failed to create mail client: %s", err)
    }
    if err := c.DialAndSend(m); err != nil {
        log.Fatalf("failed to send mail: %s", err)
    }
}
    m.Subject("This is my first mail with go-mail!")
m.SetBodyString(mail.TypeTextPlain, "Do you like this mail? I certainly do!") I certainly do!")
    c, err := mail.NewClient("smtp.example.com", mail.WithPort(25), mail.WithSMTPAuth(mail.SMTPAuthPlain),
        mail.WithUsername("my_username"), mail.WithPassword("extremely_secret_pass"))
    if err != nil {
        log.Fatalf("failed to create mail client: %s", err)
    }
    if err := c.DialAndSend(m); err != nil {
        log.Fatalf("failed to send mail: %s", err)
    }
}