mhale / smtpd

An SMTP server package written in Go, in the style of the built-in HTTP server.
The Unlicense
397 stars 92 forks source link

[Question] Suitable for receiving incoming mail from the wider web? #16

Closed sf-steve closed 4 years ago

sf-steve commented 4 years ago

I apologise in advance for the non technical question, but i am attempting to set up a "simple" (ha) service to receive incoming mail (*@domain.com) and dump it into an sql database.

This looked ideal, but my lack of understanding exactly how email works has me stumped. I have built the example used in the readme, but listening on port 25 and using fmt.Printf so i can see the output in the terminal. I uploaded the binary to a test server, ssh in and ran it sudo ./go-email.

I can connect with telnet from my local machine telnet 195.201.233.74 25 and it connect fine. I have added A and MX records on a subdomain (gomail.fl9.uk), and checked with mxtoolbox, and it seems happy: https://mxtoolbox.com/SuperTool.aspx?action=mx%3agomail.fl9.uk&run=toolpage#

Connecting to 195.201.233.74

220 ubuntu-2gb-fsn1-1 MyServerApp ESMTP Service ready [742 ms]
EHLO keeper-us-east-1c.mxtoolbox.com
250-ubuntu-2gb-fsn1-1 greets keeper-us-east-1c.mxtoolbox.com
250-SIZE 0
250 ENHANCEDSTATUSCODES [696 ms]
MAIL FROM:<supertool@mxtoolbox.com>
250 2.1.0 Ok [703 ms]
RCPT TO:<test@mxtoolboxsmtpdiag.com>
250 2.1.5 Ok [703 ms]

LookupServer 4500ms

I can send emails from my gmail, to anything@gomail.fl9.uk, with no bounces or other errors. However, i am not getting any output in the terminal thats running the program.

Have i completely misunderstood how this works?

mhale commented 4 years ago

It seems like you've understood correctly. Does it work if you try to send email locally, to rule out the network being a problem? Here is some example code to test it.

package main

import (
    "log"
    "net/smtp"
)

func main() {
    err := smtp.SendMail(
        "localhost:25",
        nil,
        "sender@example.com",
        []string{"recipient@example.com"},
        []byte(
            "From: Foo Bar <foo@bar.com>\r\n"+
            "Content-Type: text/plain; charset=us-ascii\r\n"+
            "Content-Transfer-Encoding: 7bit\r\n"+
            "Subject: Test mail\r\n"+
            "Date: Tue, 11 Mar 2014 11:14:26 +0800\r\n"+
            "To: Baz Quux <baz@quux.com>\r\n\r\n"+
            "This is the first line.\r\n"+
            ".This line should not have a leading period.\r\n"+
            "This is the third line."),
    )
    if err != nil {
        log.Fatal(err)
    }
}
sf-steve commented 4 years ago

Thanks for responding so quickly. No I haven't tried that, but looking at that code makes me worry I have misunderstood what this library does. It looks very much like it's designed for mail clients to connect to, eg I would put gomail.fl9.uk into the outgoing mail server settings for outlook / thunderbird. What I was attempting to do was capture any email sent TO anything@gomail.fl9.uk from anyone else, without them having to specifically configure their email client.

Is this possible with this library, or have I got everything back to front?

mhale commented 4 years ago

An outgoing SMTP server (e.g. what you configure in Outlook) and an incoming SMTP server are essentially the same thing - they are servers that receive email with the SMTP protocol.

What you're describing is what this library was designed to do, as a component of the Mailrouter project. So yes, it is possible.

The problem is there is more spam than genuine email, and the internet is like the wild west, so there are many schemes that interfere with email flow such as spam filters and ISPs that block port 25. The sample code above, if run on the same host as your server, would help you test your implementation by ruling out the network as the problem.

In your shoes, I would first ensure my code is doing what I wanted it to do by sending mail to it on the same host. If that works, try it from your local machine.

sf-steve commented 4 years ago

Thankyou that's really helpful. Not understanding the fundamental aspects like this has left me feeling quite lost, since I didn't even know what to Google. Now I know I'm on the right track at least. Being able to test locally will also make debugging much easier.

mhale commented 4 years ago

It may help your debugging to enable debug mode (smtpd.Debug = true). The library will log what it is reading and writing on the socket by default, but you can override that with your own functions (see LogRead and LogWrite in the library code).

If you find that systems on the internet can connect to your service fine but you still can't get mail through, there might be a bug in the library. If so, please enable debug mode and create an issue for it here with the log output attached so I can look at it. I may be able to create a test case from the log output, and fix the bug.

sf-steve commented 4 years ago

Great, thankyou. The issue is almost certainly something I'm doing, but if I do find a genuine bug, I'll post a new issue with full details. I'll close this now since my question has been answered.

Thanks again