duanbeng8872 2016-02-20 08:23
浏览 289
已采纳

如何在没有任何SMTP服务器的情况下使用golang发送电子邮件

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.

  • 写回答

2条回答 默认 最新

  • doudouji2016 2016-02-20 10:20
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 Matlab打开默认名称带有/的光谱数据
  • ¥50 easyExcel模板 动态单元格合并列
  • ¥15 res.rows如何取值使用
  • ¥15 在odoo17开发环境中,怎么实现库存管理系统,或独立模块设计与AGV小车对接?开发方面应如何设计和开发?请详细解释MES或WMS在与AGV小车对接时需完成的设计和开发
  • ¥15 CSP算法实现EEG特征提取,哪一步错了?
  • ¥15 游戏盾如何溯源服务器真实ip?需要30个字。后面的字是凑数的
  • ¥15 vue3前端取消收藏的不会引用collectId
  • ¥15 delphi7 HMAC_SHA256方式加密
  • ¥15 关于#qt#的问题:我想实现qcustomplot完成坐标轴
  • ¥15 下列c语言代码为何输出了多余的空格