GithubHelp home page GithubHelp logo

Can not receive mail about go-smtp HOT 4 CLOSED

emersion avatar emersion commented on August 26, 2024
Can not receive mail

from go-smtp.

Comments (4)

emersion avatar emersion commented on August 26, 2024

go-smtp is a library letting you handling messages as you want. You'll need a backend that forwards e-mails to be able to "receive it".

from go-smtp.

hyahm avatar hyahm commented on August 26, 2024

I had change it to

package main

import (
	"errors"
	"io"
	"io/ioutil"
	"log"
	"strings"
	"time"

	"github.com/emersion/go-smtp"
)

type Backend struct{}

// Login handles a login command with username and password.
func (bkd *Backend) Login(state *smtp.ConnectionState, username, password string) (smtp.Session, error) {
	if username != "username" || password != "password" {
		return nil, errors.New("Invalid username or password")
	}
	return &Session{
		to: make([]string, 0),
	}, nil
}

// AnonymousLogin requires clients to authenticate using SMTP AUTH before sending emails
func (bkd *Backend) AnonymousLogin(state *smtp.ConnectionState) (smtp.Session, error) {
	return nil, smtp.ErrAuthRequired
}

// A Session is returned after successful login.
type Session struct {
	to   []string
	from string
	data []byte
}

func (s *Session) Mail(from string, opts smtp.MailOptions) error {
	s.from = from
	log.Println("Mail from:", from)
	return nil
}

func (s *Session) Rcpt(to string) error {
	for _, v := range strings.Split(to, ",") {
		s.to = append(s.to, v)
	}
	log.Println("Rcpt to:", to)
	return nil
}

func (s *Session) Data(r io.Reader) error {
	b, err := ioutil.ReadAll(r)
	if err != nil {
		return err
	}
	log.Println("Data:", string(b))
	err = smtp.SendMail("smtp.qq.com:465", nil, s.from, s.to, r)
	if err != nil {
		return err
	}

	return nil
}

func (s *Session) Reset() {}

func (s *Session) Logout() error {
	return nil
}

func main() {
	be := &Backend{}

	s := smtp.NewServer(be)

	s.Addr = ":1025"
	s.Domain = "smtp.hyahm.com"
	s.ReadTimeout = 10 * time.Second
	s.WriteTimeout = 10 * time.Second
	s.MaxMessageBytes = 1024 * 1024
	s.MaxRecipients = 50
	s.AllowInsecureAuth = true

	log.Println("Starting server at", s.Addr)
	if err := s.ListenAndServe(); err != nil {
		log.Fatal(err)
	}
}

error message

(554, b'5.0.0 Error: transaction failed, blame it on the weather: EOF')

from go-smtp.

metagates-dev avatar metagates-dev commented on August 26, 2024

go-smtp is a library letting you handling messages as you want. You'll need a backend that forwards e-mails to be able to "receive it".

@emersion
so we need implement the forwards e-mails by ourself, right? I want to build a mail server like exim4.

from go-smtp.

metagates-dev avatar metagates-dev commented on August 26, 2024

I think i knew how to implement the rcpt.

package main

import (
    "crypto/tls"
    "io"
    "log"
    "strings"

    "github.com/emersion/go-smtp"
)

func main() {
    // Set up authentication information.
    //auth := sasl.NewPlainClient("", "username", "password")

    // Need set the SPF for sender, and get the mx DNS to send.
    c, err := smtp.DialTLS("xxxx:465", &tls.Config{InsecureSkipVerify: true}) 
    if err != nil {
        log.Fatal(err)
    }
    defer c.Close()
    if err := c.Hello("163.com"); err != nil {
        log.Fatal(err)
    }
    println("hello ok")
    if err := c.Mail("[email protected]", nil); err != nil {
        log.Fatal(err)
    }
    println("mail ok")
    if err := c.Rcpt("[email protected]"); err != nil {
        log.Fatal(err)
    }
    println("rcpt ok")
    writer, err := c.Data()
    if err != nil {
        log.Fatal(err)
    }
    msg := strings.NewReader("To: [email protected]\r\n" +
        "Subject: discount Gophers!\r\n" +
        "\r\n" +
        "This is the email body.\r\n")
    _, err = io.Copy(writer, msg)
    if err != nil {
        log.Fatal(err)
    }
    if err := writer.Close(); err != nil {
        log.Fatal(err)
    }
    println("send data done")
}

from go-smtp.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.