4

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.

7
  • 2
    You need an SMTP server somewhere. Any server/service (e.g. Mandrill) will enforce some kind of quotas to prevent spam and maintain reputation. You could run a server locally, or your own, but you will find that most mail won't be delivered due to lack of sender reputation.
    – elithrar
    Feb 20, 2016 at 8:28
  • 1
    The down voter, I don't understand the reason for down vote. Can you clarify what is wrong with the question? Feb 20, 2016 at 8:34
  • 1
    @elithrar, I am not currently worrying about reputations and all. Anyway I am not going to send the mail to public email domain. I just need to know how the objective can be achieved in golang Feb 20, 2016 at 8:36
  • Oh, after writing my answer I actually "got it" that your question is not technical but rather political. Well, the answer then is "no": your mail will still be needed to be sent through some SMTP server unless you're using some mail service exposing non-SMTP API, but I doubt that that will somehow make it possible to overcome quota limitations of that service. So basically the answer is "rent a VPS and run a real MTA there".
    – kostix
    Feb 20, 2016 at 11:33
  • 1
    I disagree. Of course it is possible to send mails without an SMTP server on the sender side: Take the email address, find the according MX, contact it directly and (try to) send the mail. Will it be accepted? Most likely not. Does this method work? It sure does. That's what MTAs do. Feb 21, 2016 at 11:24

2 Answers 2

14

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.

4
  • I'm using exim cmd := exec.Command("/usr/sbin/exim", "-t") but I'm not receiving any mail on bcc m.SetHeader("Bcc", "[email protected]") , however I receive mail on "To" set to different email address in the same email. Also I just checked All "Cc" are getting mails Apr 21, 2018 at 5:51
  • @GarvitJain I fail to see how is this related to the essence of the OP's question and my answer to it. Care to post a constructive question to superuser.com/questions/tagged/exim ?
    – kostix
    Apr 22, 2018 at 10:50
  • @garvit-jain, my comment above was destined to you; sorry, it's hard to properly refer to a person while typing on a smartphone.
    – kostix
    Apr 22, 2018 at 10:56
  • 1
    I've posted the question : stackoverflow.com/questions/49974478/… Apr 23, 2018 at 6:14
0

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.

Not the answer you're looking for? Browse other questions tagged or ask your own question.