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

Example not working #28

Closed 3mb3dw0rk5 closed 3 years ago

3mb3dw0rk5 commented 3 years ago

Hi,

I tried to use your simple example in readme.md But calling mail.ReadMessage actually raises an error malformed MIME header: missing colon: "..."

func mailHandler(origin net.Addr, from string, to []string, data []byte) error {
    msg, err := mail.ReadMessage(bytes.NewReader(data))
    if err != nil {
        log.Print(err)
        return nil
    }
        return nil
}

It seems that the mail package is not able to parse the RFC 2821 Received header properly. Any idea how to fix this?

mhale commented 3 years ago

I've just tried with a fresh example and it's working fine for me. What's the raw message being submitted?

3mb3dw0rk5 commented 3 years ago

I face this issue with this little example:

go.mod:

module github.com/3mb3dw0rk5/gosmtpd

go 1.16

require github.com/mhale/smtpd v0.0.0-20210209185612-36ee4150ae3b // indirect

main.go:

package main

import (
    "bytes"
    "log"
    "net"
    "net/mail"

    "github.com/mhale/smtpd"
)

func mailHandler(origin net.Addr, from string, to []string, data []byte) error {
    _, err := mail.ReadMessage(bytes.NewReader(data))
    if err != nil {
        log.Print(err)
        log.Printf("\ndata: \"%s\"", data)
    }
    return nil
}

func main() {
    log.SetFlags(log.LstdFlags | log.Lshortfile)
    log.Printf("Starting daemon")

    srv := &smtpd.Server{
        Addr:         "0.0.0.0:8025",
        Handler:      mailHandler,
        Hostname:     "",
        AuthRequired: true}
    srv.ListenAndServe()
}

Python client sending a message: client.py:

import smtplib
from email.message import EmailMessage

smtp_client = smtplib.SMTP(host="localhost", port=8025)
smtp_client.ehlo()
sender_email = "senderemail@example.com"
receiver_email = "receiveremail@example.com"
msg = "Hello from python!"
smtp_client.sendmail(sender_email, receiver_email, msg)
smtp_client.close()

Output when starting the daemon and running the client script:

2021/03/07 08:55:20 main.go:23: Starting daemon
2021/03/07 08:55:22 main.go:15: malformed MIME header: missing colon: "Hello from python!"
2021/03/07 08:55:22 main.go:16: 
data: "Received: from localhost.localdomain (localhost.localdomain. [::1])
        by notebook (smtpd) with SMTP
        for <receiveremail@example.com>; Sun,  7 Mar 2021 08:55:22 +0100 (CET)
Hello from python!
"
3mb3dw0rk5 commented 3 years ago

Looked into it again and it seems that the python example from doc is not complete. Passing a complete RFC822 content and the smtpd output is as expected.

Can be closed.