如何在没有任何 smtp 服务器的情况下在 golang 中发送电子邮件 [英] How to send emails in golang without any smtp server

查看:20
本文介绍了如何在没有任何 smtp 服务器的情况下在 golang 中发送电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从一个用 golang 编写的服务器应用程序发送批量邮件.我只是不想使用任何第三方 smtp 服务器来避免使用配额限制.

I want to send bulk mail from a server application written in golang. I just don't want to use any third-party smtp server to avoid usage quota limitations.

如何在没有 smtp 服务器的情况下发送电子邮件?标准库中的 smtp 包可以帮助我吗?我看到的所有使用 smtp 包的示例都需要第三方 smtp 服务器来发送电子邮件.

How can I send email without a smtp server? Is smtp package in standard library can help me? All example I see using the smtp package requires a third-party smtp server to send email.

推荐答案

只有一种方法可以在不直接与 SMTP 服务器通信的情况下发送电子邮件:将此操作委托给其他程序.

There is only one way to send an e-mail message without directly communicating with an SMTP server: delegate this action to some other program.

选择什么程序本身就是一个开放而广泛的问题,因为有很多程序可以发送邮件.

What program to pick, is an open and wide question in itself because there are lots of programs which are able to send mails.

另一方面,如果我们谈论 POSIX 系统,例如基于 GNU/Linux 或 *BSD 的操作系统,它们通常带有二进制 /usr/sbin/sendmail 或者,有时,/usr/bin/sendmail 由真正的 Sendmail 提供 包或另一个 MTA 大量存在——从成熟的 MTA 等作为 后缀Exim 到范围狭窄的null-clients",例如 ssmtpnullmailer.

On the other hand, if we talk about POSIX systems such as GNU/Linux- or *BSD-based operating systems, they usually come with the binary /usr/sbin/sendmail or, sometimes, /usr/bin/sendmail which is either provided by the real Sendmail package or by another MTA which exist in abundance—ranging from full-blown MTAs such as Postfix or Exim to narrow-scoped "null-clients" such as ssmtp or nullmailer.

这个程序通常用 -t 命令行选项调用,这使它从邮件消息本身的 To 标头中读取消息收件人的地址.

This program is usually called with the -t command-line option which makes it read the addresses of the message recipients from the To headers of the mail message itself.

基本上调用 /usr/sbin/sendmail -t 并将完整消息的文本传送到其标准输入是 PHP 的 mail() 函数 确实如此,FWIW.

Basically calling /usr/sbin/sendmail -t and piping the full message's text to its standard input is what PHP's mail() function does, FWIW.

虽然您可以直接使用 os/execnet/mailnet/textproto 标准包进行此调用,但流行的 gomail 包提供了一种更简单的方法:它的 Message 类型提供了 WriteTo() 方法,该方法能够写入正在运行的 Sendmail 实例,如下所示(从我的真实程序中复制):

While you can do this call directly using the os/exec, net/mail and net/textproto standard packages, the popular gomail package provides a simpler way to do that: its Message type provides the WriteTo() method which is able to write to a running Sendmail instance, like this (copied from a real program of mine):

const sendmail = "/usr/sbin/sendmail"

func submitMail(m *gomail.Message) (err error) {
    cmd := exec.Command(sendmail, "-t")
    cmd.Stdout = os.Stdout
    cmd.Stderr = os.Stderr

    pw, err := cmd.StdinPipe()
    if err != nil {
        return
    }

    err = cmd.Start()
    if err != nil {
        return
    }

    var errs [3]error
    _, errs[0] = m.WriteTo(pw)
    errs[1] = pw.Close()
    errs[2] = cmd.Wait()
    for _, err = range errs {
        if err != nil {
            return
        }
    }
    return
}

实际上,依靠 MTA 传递邮件的一个优势是成熟的 MTA 支持邮件队列:也就是说,如果 MTA 无法立即发送您的邮件(例如,由于网络中断等),那么它将消息保存在一个特殊的目录中,然后会定期尝试一次又一次地传递它,直到成功或(通常是巨大的,如 4-5 天)超时到期.

Actually, relying on MTA to deliver your mails has an advantage is that full-blown MTAs support mail queuing: that is, if an MTA is unable to send your message right away (say, due to a network outage etc) so it will save the message in a special directory and will then periodically try delivering it again and again until that succeeds or a (usually huge, like 4-5 days) timeout expires.

这篇关于如何在没有任何 smtp 服务器的情况下在 golang 中发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆