Go Go-Simple-Mail包进行批量SMTP邮件发送

go-simple-mail 包提供了一种简便的方式来处理和发送邮件。这个包支持保持活动连接、TLS和SSL加密协议,非常适合批量SMTP邮件发送需求。 

1、安装Go-Simple-Mail包

go get -u github.com/xhit/go-simple-mail/v2

2、配置SMTP服务器连接

go-simple-mail包支持多种SMTP服务器连接的配置,包括TLS和SSL。 

package main

import (
    mail "github.com/xhit/go-simple-mail/v2"
    "log"
    "time"
)

func main() {
    server := mail.NewSMTPClient()
    server.Host = "smtp.example.com"
    server.Port = 587 // 或者465(SSL)
    server.Username = "your-email@example.com"
    server.Password = "your-email-password"
    server.Encryption = mail.EncryptionTLS // 可以用 mail.EncryptionNone, mail.EncryptionSSL, mail.EncryptionTLS

    server.KeepAlive = true
    server.ConnectTimeout = 10 * time.Second
    server.SendTimeout = 10 * time.Second

    smtpClient, err := server.Connect()
    if err != nil {
        log.Fatal(err)
    }

    // 发送邮件的代码可以放在这里
}

建了一个SMTP服务器连接对象,配置了主机、端口、用户名、密码,以及加密方式等参数。通过server.Connect()方法建立连接,并捕获可能的错误。

3、发送单封邮件

在建立了SMTP连接之后,我们可以使用go-simple-mail包发送邮件。

package main

import (
    mail "github.com/xhit/go-simple-mail/v2"
    "log"
    "time"
)

func main() {
    server := mail.NewSMTPClient()
    server.Host = "smtp.example.com"
    server.Port = 587
    server.Username = "your-email@example.com"
    server.Password = "your-email-password"
    server.Encryption = mail.EncryptionTLS

    server.KeepAlive = true
    server.ConnectTimeout = 10 * time.Second
    server.SendTimeout = 10 * time.Second

    smtpClient, err := server.Connect()
    if err != nil {
        log.Fatal(err)
    }

    email := mail.NewMSG()
    email.SetFrom("From Me <your-email@example.com>").
        AddTo("recipient1@example.com").
        SetSubject("Test Email Subject")

    email.SetBody(mail.TextHTML, "<h1>Hello, this is a test email!</h1>")

    err = email.Send(smtpClient)
    if err != nil {
        log.Fatal(err)
    } else {
        log.Println("Email Sent Successfully!")
    }
}

创建了一封邮件,并设置了发件人、收件人、主题和邮件内容。最后调用email.Send(smtpClient)方法发送邮件,并处理潜在的错误。

4、批量发送邮件

使用go-simple-mail包不仅可以发送单封邮件,还可以用来批量发送邮件,这在需要群发邮件时非常有用。

package main

import (
    mail "github.com/xhit/go-simple-mail/v2"
    "log"
    "time"
)

func main() {
    server := mail.NewSMTPClient()
    server.Host = "smtp.example.com"
    server.Port = 587
    server.Username = "your-email@example.com"
    server.Password = "your-email-password"
    server.Encryption = mail.EncryptionTLS

    server.KeepAlive = true
    server.ConnectTimeout = 10 * time.Second
    server.SendTimeout = 10 * time.Second

    smtpClient, err := server.Connect()
    if err != nil {
        log.Fatal(err)
    }

    recipients := []string{"recipient1@example.com", "recipient2@example.com", "recipient3@example.com"}
    subject := "Bulk Email Test"
    body := "<h1>This is a test bulk email</h1>"

    for _, recipient := range recipients {
        email := mail.NewMSG()
        email.SetFrom("From Me <your-email@example.com>").
            AddTo(recipient).
            SetSubject(subject)

        email.SetBody(mail.TextHTML, body)

        err = email.Send(smtpClient)
        if err != nil {
            log.Printf("Could not send email to %s: %v", recipient, err)
        } else {
            log.Printf("Email sent to %s successfully!", recipient)
        }
    }
}

将收件人放入一个列表,通过循环创建和发送邮件, 同时处理每一次的发送结果。

5、添加附件

除了发送文本邮件,go-simple-mail包还支持添加附件。我们可以通过email.AddAttachment()方法添加文件附件。

package main

import (
    mail "github.com/xhit/go-simple-mail/v2"
    "log"
    "time"
)

func main() {
    server := mail.NewSMTPClient()
    server.Host = "smtp.example.com"
    server.Port = 587
    server.Username = "your-email@example.com"
    server.Password = "your-email-password"
    server.Encryption = mail.EncryptionTLS

    server.KeepAlive = true
    server.ConnectTimeout = 10 * time.Second
    server.SendTimeout = 10 * time.Second

    smtpClient, err := server.Connect()
    if err != nil {
        log.Fatal(err)
    }

    email := mail.NewMSG()
    email.SetFrom("From Me <your-email@example.com>").
        AddTo("recipient@example.com").
        SetSubject("Email with Attachment")

    email.SetBody(mail.TextHTML, "<h1>This email contains an attachment</h1>")

    email.AddAttachment("path/to/file.pdf")

    err = email.Send(smtpClient)
    if err != nil {
        log.Fatal(err)
    } else {
        log.Println("Email Sent Successfully with Attachment!")
    }
}

使用email.AddAttachment("path/to/file.pdf") 添加了一个PDF文件作为附件。

  • 8
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值