如何在golang中发送电子邮件而不使用任何SMTP服务器

huangapple go评论99阅读模式
英文:

How to send emails in golang without any smtp server

问题

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

如何在没有SMTP服务器的情况下发送电子邮件?标准库中的smtp包能帮助我吗?我看到的所有示例都使用smtp包需要一个第三方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.

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.

答案1

得分: 14

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

选择哪个程序是一个开放而广泛的问题,因为有很多能够发送邮件的程序。

另一方面,如果我们谈论的是像GNU/Linux或*BSD这样的POSIX系统,它们通常会带有二进制文件/usr/sbin/sendmail或者有时是/usr/bin/sendmail,它们要么由真正的Sendmail软件包提供,要么由其他丰富多样的MTA提供,从完整的MTA(如PostfixExim)到狭义的“null-clients”(如ssmtpnullmailer)。

通常使用-t命令行选项调用此程序,该选项使其从邮件消息本身的To标头中读取消息接收者的地址。

基本上,调用/usr/sbin/sendmail -t并将完整消息的文本导入其标准输入是PHP的mail()函数的工作原理。

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

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天)的超时时间。

英文:

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.

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.

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.

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.

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
}

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.

答案2

得分: 0

你的问题似乎是:我可以在没有 Web 服务器的情况下向用户展示网页吗?是的,如果你能够编写自己的 Web 服务器,那么可以实现。

你能在 Go 语言上编写 SMTP 服务器吗?是的。谷歌上有一些关于“golang smtp server”的解决方案。所以我很确定你可以将 SMTP 服务器功能集成到你的应用程序中。

但正如评论者所说,你的大部分消息(如果不是全部)可能会直接进入大多数服务商的垃圾邮件文件夹。

英文:

Your question looks like - can I show webpages to users without a web-server? Yes you can if you are able to write your own web-server.

Can you write SMTP server on Go? Yes. Google shows few solutions for "golang smtp server" query. So I'm pretty sure you can integrate SMTP server functional into your application.

But as commenters said - most part of your messages (if not all) would go directly to spam folder on most part of services.

huangapple
  • 本文由 发表于 2016年2月20日 16:23:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/35520804.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定