问题描述

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



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

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



选择哪种程序本身就是一个开放且广泛的问题因为有很多程序可以发送邮件。



 / usr / sbin / sendmail  / usr / bin /由实际的 Sendmail 软件包提供的sendmail  ssmtp  nullmailer 


 -t调用 To 


 / usr / sbin / sendmail -t  mail()
 os / exec  net / mail  net / textproto  gomail  Message  WriteTo()
  const sendmail = / usr / sbin / sendmail 

func SubmitMail(m * gomail.Message)(错误错误){
cmd:= exec.Command(sendmail, -t)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr

pw,err:= cmd.StdinPipe()
如果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( )_的
,错误=范围错误{如果错误,则
{=
return
}
}
return
}

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


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.

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.

/usr/sbin/sendmail/usr/bin/sendmailssmtpnullmailer
-tTo
/usr/sbin/sendmail -tmail()
os/execnet/mailnet/textprotogomailMessageWriteTo()
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.

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