Comments (4)
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.
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.
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.
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)
- Incorrect handling of the interrupted AUTH command HOT 1
- Could you please support the authentification type of LOGIN? HOT 2
- Should 'Too many recipients' return 452 4.5.3 instead of 552 5.5.3? HOT 2
- server: DATA command timeout should close the connection HOT 1
- No greeting when starting with `tls.Config` HOT 3
- client: batching commands HOT 1
- Feature Request: give ability to drop connection before greeting HOT 3
- Invalid server example provided in README HOT 1
- v1.16.0 breaks backwards compatibility HOT 3
- Why need to make sure all the data has been consumed ? HOT 1
- Using SMTP server email are not going to external accounts HOT 1
- Valid Reverse-path getting rejected with 500 5.5.4 Unknown MAIL FROM argument HOT 1
- Auth should be mandatory for `MAIL`, `RCPT` & `DATA` commands when `server.AuthDisabled` is `false` HOT 2
- google smtp fails to send message with error `Relay access denied` HOT 1
- What's the best way to check and enforce TLS connection? HOT 11
- Server: unauthenticated access HOT 1
- Handle zero-byte TCP healthcheck connections HOT 4
- Unable to set headers when using SendMail with go templating. HOT 2
- Does the server support multi-threading when receiving emails? HOT 1
- Request an example code for smtp relay HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from go-smtp.