Exemple #1
0
func TestTimeoutClose(t *testing.T) {
	addr, closer := runserver(t, &smtpd.Server{
		MaxConnections: 1,
		ReadTimeout:    time.Second,
		WriteTimeout:   time.Second,
	})
	defer closer()

	c1, err := smtp.Dial(addr)
	if err != nil {
		t.Fatalf("Dial failed: %v", err)
	}

	time.Sleep(time.Second * 2)

	c2, err := smtp.Dial(addr)
	if err != nil {
		t.Fatalf("Dial failed: %v", err)
	}
	defer c2.Close()

	if err := c1.Mail("*****@*****.**"); err == nil {
		t.Fatal("MAIL succeeded despite being timed out.")
	}

	if err := c2.Mail("*****@*****.**"); err != nil {
		t.Fatalf("MAIL failed: %v", err)
	}

	if err := c2.Quit(); err != nil {
		t.Fatalf("Quit failed: %v", err)
	}
}
Exemple #2
0
func TestSmtpdServer(t *testing.T) {
	newMailWasReceived = false
	srvPort := startServer()

	conn, err := smtp.Dial(fmt.Sprintf("localhost:%d", srvPort))
	for err != nil {
		conn, err = smtp.Dial(fmt.Sprintf("localhost:%d", srvPort))
	}
	defer conn.Close()

	conn.Mail("*****@*****.**")
	conn.Rcpt("*****@*****.**")

	writeCloser, err := conn.Data()
	if err != nil {
		t.Fatalf("Unexpected error: %v", err)
	}
	defer writeCloser.Close()

	buf := bytes.NewBufferString(`Hello World!`)
	buf.WriteTo(writeCloser)

	if newMailWasReceived != true {
		t.Fatalf("Email was not received")
	}
}
Exemple #3
0
func TestMaxConnections(t *testing.T) {

	ln, err := net.Listen("tcp", "127.0.0.1:0")
	if err != nil {
		t.Fatalf("Listen failed: %v", err)
	}

	defer ln.Close()

	server := &smtpd.Server{
		MaxConnections: 1,
	}

	go func() {
		server.Serve(ln)
	}()

	c1, err := smtp.Dial(ln.Addr().String())
	if err != nil {
		t.Fatalf("Dial failed: %v", err)
	}

	_, err = smtp.Dial(ln.Addr().String())
	if err == nil {
		t.Fatal("Dial succeeded despite MaxConnections = 1")
	}

	c1.Close()
}
Exemple #4
0
func sendMessage(mailMessage MailMessage) {
	conn, err := smtp.Dial("bulk-s13.nexthosting.ro:5445")
	if err != nil {
		log.Fatal("err ", err)
	}

	if err := conn.Mail(mailMessage.from); err != nil {
		log.Fatal(err)
	}
	if err := conn.Rcpt(mailMessage.to); err != nil {
		log.Fatal(err)
	}

	// Send the email body.
	wc, err := conn.Data()
	if err != nil {
		log.Fatal(err)
	}

	_, err = fmt.Fprintf(wc, getMessageBody(mailMessage))
	if err != nil {
		log.Fatal(err)
	}
	err = wc.Close()
	if err != nil {
		log.Fatal(err)
	}

	// Send the QUIT command and close the connection.
	err = conn.Quit()
	if err != nil {
		log.Fatal(err)
	}
}
Exemple #5
0
func (t Tracker) sendMail(body string) error {
	// Connect to the remote SMTP server.
	c, err := smtp.Dial(t.config.Mail.ReceiptMail)
	if err != nil {
		return err
	}
	defer c.Close()

	// Set the sender and recipient.
	c.Mail(t.config.Mail.SenderMail)
	c.Rcpt(t.config.Mail.ReceiptMail)

	// Send the email body.
	wc, err := c.Data()
	if err != nil {
		return err
	}
	defer wc.Close()

	buf := bytes.NewBufferString(body)
	if _, err = buf.WriteTo(wc); err != nil {
		return err
	}
	return nil
}
Exemple #6
0
func main() {
	c, err := smtp.Dial("mail.it.northwestern.edu:25")
	if err != nil {
		log.Fatal(err)
	}
	if err := c.Mail("*****@*****.**"); err != nil {
		log.Fatalf("Error in mail command %s\n", err)
	}
	if err := c.Rcpt("*****@*****.**"); err != nil {
		log.Fatal(err)
	}

	wc, err := c.Data()
	if err != nil {
		log.Fatal(err)
	}
	_, err = fmt.Fprintf(wc, "This is a test mail from golang")
	if err != nil {
		log.Fatal(err)
	}
	err = wc.Close()
	if err != nil {
		log.Fatal(err)
	}
	err = c.Quit()
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println("sent the email")
}
Exemple #7
0
func sendEmail(to string, body []byte) error {
	c, err := smtp.Dial(*mailServer)
	if err != nil {
		return err
	}
	if err = c.Mail(*mailFrom); err != nil {
		return err
	}
	if err = c.Rcpt(to); err != nil {
		return err
	}
	w, err := c.Data()
	if err != nil {
		return err
	}
	_, err = w.Write(body)
	if err != nil {
		return err
	}
	err = w.Close()
	if err != nil {
		return err
	}
	return c.Quit()
}
Exemple #8
0
func sendMail(addr string, a smtp.Auth, from string, to []string, msg []byte) error {
	c, err := smtp.Dial(addr)
	if err != nil {
		return err
	}
	if err = c.StartTLS(nil); err != nil {
		return err
	}
	if err = c.Auth(a); err != nil {
		return err
	}
	if err = c.Mail(from); err != nil {
		return err
	}
	for _, addr := range to {
		if err = c.Rcpt(addr); err != nil {
			return err
		}
	}
	w, err := c.Data()
	if err != nil {
		return err
	}
	_, err = w.Write(msg)
	if err != nil {
		return err
	}
	err = w.Close()
	if err != nil {
		return err
	}
	return c.Quit()
}
Exemple #9
0
func TestRecipientCheck(t *testing.T) {
	addr, closer := runserver(t, &smtpd.Server{
		RecipientChain: []smtpd.Recipient{
			smtpd.RecipientFunc(func(x func(conn *smtpd.Connection)) func(*smtpd.Connection) {
				return func(conn *smtpd.Connection) {
					conn.Error(errors.New("Random error"))
				}
			}),
		},
	})
	defer closer()

	c, err := smtp.Dial(addr)
	if err != nil {
		t.Fatalf("Dial failed: %v", err)
	}

	if err := c.Mail("*****@*****.**"); err != nil {
		t.Fatalf("Mail failed: %v", err)
	}

	if err := c.Rcpt("*****@*****.**"); err == nil {
		t.Fatal("Unexpected RCPT success")
	}
}
Exemple #10
0
func SMTPAuth(a smtp.Auth, cfg *SMTPConfig) error {
	c, err := smtp.Dial(fmt.Sprintf("%s:%d", cfg.Host, cfg.Port))
	if err != nil {
		return err
	}
	defer c.Close()

	if err = c.Hello("gogs"); err != nil {
		return err
	}

	if cfg.TLS {
		if ok, _ := c.Extension("STARTTLS"); ok {
			if err = c.StartTLS(&tls.Config{
				InsecureSkipVerify: cfg.SkipVerify,
				ServerName:         cfg.Host,
			}); err != nil {
				return err
			}
		} else {
			return errors.New("SMTP server unsupports TLS")
		}
	}

	if ok, _ := c.Extension("AUTH"); ok {
		if err = c.Auth(a); err != nil {
			return err
		}
		return nil
	}
	return ErrUnsupportedLoginType
}
Exemple #11
0
func TestInvalidRecipient(t *testing.T) {

	ln, err := net.Listen("tcp", "127.0.0.1:0")
	if err != nil {
		t.Fatalf("Listen failed: %v", err)
	}

	defer ln.Close()

	server := &smtpd.Server{}

	go func() {
		server.Serve(ln)
	}()

	c, err := smtp.Dial(ln.Addr().String())
	if err != nil {
		t.Fatalf("Dial failed: %v", err)
	}

	if err := c.Mail("*****@*****.**"); err != nil {
		t.Fatalf("Mail failed: %v", err)
	}

	if err := c.Rcpt("invalid@@example.org"); err == nil {
		t.Fatal("Unexpected RCPT success")
	}

}
Exemple #12
0
func TestInvalidHelo(t *testing.T) {

	ln, err := net.Listen("tcp", "127.0.0.1:0")
	if err != nil {
		t.Fatalf("Listen failed: %v", err)
	}

	defer ln.Close()

	server := &smtpd.Server{}

	go func() {
		server.Serve(ln)
	}()

	c, err := smtp.Dial(ln.Addr().String())
	if err != nil {
		t.Fatalf("Dial failed: %v", err)
	}

	if err := c.Hello(""); err == nil {
		t.Fatal("Unexpected HELO success")
	}

}
Exemple #13
0
func TestRecipientCheck(t *testing.T) {

	ln, err := net.Listen("tcp", "127.0.0.1:0")
	if err != nil {
		t.Fatalf("Listen failed: %v", err)
	}

	defer ln.Close()

	server := &smtpd.Server{
		RecipientChecker: func(peer smtpd.Peer, addr string) error {
			return smtpd.Error{Code: 552, Message: "Denied"}
		},
	}

	go func() {
		server.Serve(ln)
	}()

	c, err := smtp.Dial(ln.Addr().String())
	if err != nil {
		t.Fatalf("Dial failed: %v", err)
	}

	if err := c.Mail("*****@*****.**"); err != nil {
		t.Fatalf("Mail failed: %v", err)
	}

	if err := c.Rcpt("*****@*****.**"); err == nil {
		t.Fatal("Unexpected RCPT success")
	}

}
Exemple #14
0
func TestHELOCheck(t *testing.T) {

	ln, err := net.Listen("tcp", "127.0.0.1:0")
	if err != nil {
		t.Fatalf("Listen failed: %v", err)
	}

	defer ln.Close()

	server := &smtpd.Server{
		HeloChecker: func(peer smtpd.Peer, name string) error {
			if name != "foobar.local" {
				t.Fatal("Wrong HELO name")
			}
			return smtpd.Error{Code: 552, Message: "Denied"}
		},
	}

	go func() {
		server.Serve(ln)
	}()

	c, err := smtp.Dial(ln.Addr().String())
	if err != nil {
		t.Fatalf("Dial failed: %v", err)
	}

	if err := c.Hello("foobar.local"); err == nil {
		t.Fatal("Unexpected HELO success")
	}

}
Exemple #15
0
func TestListenAndServe(t *testing.T) {

	ln, err := net.Listen("tcp", "127.0.0.1:0")
	if err != nil {
		t.Fatalf("Listen failed: %v", err)
	}

	addr := ln.Addr().String()

	ln.Close()

	server := &smtpd.Server{Addr: addr}

	go func() {
		server.ListenAndServe()
	}()

	time.Sleep(100 * time.Millisecond)

	c, err := smtp.Dial(addr)
	if err != nil {
		t.Fatalf("Dial failed: %v", err)
	}

	if err := c.Quit(); err != nil {
		t.Fatalf("Quit failed: %v", err)
	}

}
Exemple #16
0
func TestLongLine(t *testing.T) {

	ln, err := net.Listen("tcp", "127.0.0.1:0")
	if err != nil {
		t.Fatalf("Listen failed: %v", err)
	}

	defer ln.Close()

	server := &smtpd.Server{}

	go func() {
		server.Serve(ln)
	}()

	c, err := smtp.Dial(ln.Addr().String())
	if err != nil {
		t.Fatalf("Dial failed: %v", err)
	}

	if err := c.Mail(fmt.Sprintf("*****@*****.**", strings.Repeat("x", 65*1024))); err == nil {
		t.Fatalf("MAIL failed: %v", err)
	}

	if err := c.Quit(); err != nil {
		t.Fatalf("Quit failed: %v", err)
	}

}
Exemple #17
0
//DefaultSMTPSendEmail - This is the default SMTP server send email behavior
//There are some issue with the smtp ssl certificate
//Reimplementing the http://golang.org/src/net/smtp/smtp.go?s=7610:7688#L263
//Will switch back to the default smtp.SendMail function
func DefaultSMTPSendEmail(addr string, a smtp.Auth, from string, to []string, msg []byte) error {
	c, err := smtp.Dial(addr)
	if err != nil {
		return err
	}
	defer c.Close()
	if err = c.Mail(from); err != nil {
		return err
	}
	for _, addr := range to {
		if err = c.Rcpt(addr); err != nil {
			return err
		}
	}
	w, err := c.Data()
	if err != nil {
		return err
	}
	_, err = w.Write(msg)
	if err != nil {
		return err
	}
	err = w.Close()
	if err != nil {
		return err
	}
	return c.Quit()

}
Exemple #18
0
func TestRCPTbeforeMAIL(t *testing.T) {

	ln, err := net.Listen("tcp", "127.0.0.1:0")
	if err != nil {
		t.Fatalf("Listen failed: %v", err)
	}

	defer ln.Close()

	server := &smtpd.Server{}

	go func() {
		server.Serve(ln)
	}()

	c, err := smtp.Dial(ln.Addr().String())
	if err != nil {
		t.Fatalf("Dial failed: %v", err)
	}

	if err := c.Rcpt("*****@*****.**"); err == nil {
		t.Fatal("Unexpected RCPT success")
	}

}
Exemple #19
0
// newSMTPClient will connect, auth, say helo to the SMTP server and return the client.
func newSMTPClient(username, password, host string, port int) (*smtp.Client, error) {
	addr := fmt.Sprintf("%s:%d", host, port)
	client, err := smtp.Dial(addr)
	if err != nil {
		return client, err
	}

	// just saying HELO!
	if err = client.Hello("localhost"); err != nil {
		return client, err
	}

	// if the server can handle TLS, use it
	if ok, _ := client.Extension("STARTTLS"); ok {
		if err = client.StartTLS(nil); err != nil {
			return client, err
		}
	}

	// if the server can handle auth, use it
	if ok, _ := client.Extension("AUTH"); ok {
		auth := smtp.PlainAuth("", username, password, host)
		if err = client.Auth(auth); err != nil {
			return client, err
		}
	}

	return client, nil
}
// VerifyEmail checks an email by using SMTP Callback Verification method
//
// Reference:
// http://en.wikipedia.org/wiki/Callback_verification
func VerifyEmail(email string) (isValid bool, err error) {
	mx, err := net.LookupMX(parseDomain(email))
	if err != nil {
		return
	}
	c, err := smtp.Dial(mx[0].Host + ":25")
	if err != nil {
		return
	}

	err = c.Hello("verify-email.org")
	if err != nil {
		return
	}
	err = c.Mail("*****@*****.**")
	if err != nil {
		return
	}
	if err := c.Rcpt(email); err == nil {
		isValid = true
	}

	err = c.Quit()
	return
}
Exemple #21
0
func TestListenAndServe(t *testing.T) {
	addr, closer := runserver(t, &smtpd.Server{})
	closer()
	// Wait here for Windows to release the port.
	time.Sleep(100 * time.Millisecond)

	server := &smtpd.Server{}

	dial := make(chan struct{})
	go func() {
		close(dial)
		err := server.ListenAndServe(addr)
		if err != nil {
			t.Error(err)
		}
	}()

	<-dial
	time.Sleep(100 * time.Millisecond)

	c, err := smtp.Dial(addr)
	if err != nil {
		t.Fatalf("Dial failed: %v", err)
	}

	if err := c.Quit(); err != nil {
		t.Fatalf("Quit failed: %v", err)
	}
}
func TestMaxRecipients(t *testing.T) {

	addr, closer := runserver(t, &smtpd.Server{
		MaxRecipients: 1,
	})

	defer closer()

	c, err := smtp.Dial(addr)
	if err != nil {
		t.Fatalf("Dial failed: %v", err)
	}

	if err := c.Mail("*****@*****.**"); err != nil {
		t.Fatalf("MAIL failed: %v", err)
	}

	if err := c.Rcpt("*****@*****.**"); err != nil {
		t.Fatalf("RCPT failed: %v", err)
	}

	if err := c.Rcpt("*****@*****.**"); err == nil {
		t.Fatal("RCPT succeeded despite MaxRecipients = 1")
	}

	if err := c.Quit(); err != nil {
		t.Fatalf("QUIT failed: %v", err)
	}

}
Exemple #23
0
func TestTLSTimeout(t *testing.T) {

	ln, err := net.Listen("tcp", "127.0.0.1:0")
	if err != nil {
		t.Fatalf("Listen failed: %v", err)
	}

	defer ln.Close()

	cert, err := tls.X509KeyPair(localhostCert, localhostKey)
	if err != nil {
		t.Fatalf("Cert load failed: %v", err)
	}

	server := &smtpd.Server{
		TLSConfig: &tls.Config{
			Certificates: []tls.Certificate{cert},
		},
		ReadTimeout:  time.Second * 2,
		WriteTimeout: time.Second * 2,
	}

	go func() {
		server.Serve(ln)
	}()

	c, err := smtp.Dial(ln.Addr().String())
	if err != nil {
		t.Fatalf("Dial failed: %v", err)
	}

	if err := c.StartTLS(&tls.Config{InsecureSkipVerify: true}); err != nil {
		t.Fatalf("STARTTLS failed: %v", err)
	}

	time.Sleep(time.Second)

	if err := c.Mail("*****@*****.**"); err != nil {
		t.Fatalf("MAIL failed: %v", err)
	}

	time.Sleep(time.Second)

	if err := c.Rcpt("*****@*****.**"); err != nil {
		t.Fatalf("RCPT failed: %v", err)
	}

	time.Sleep(time.Second)

	if err := c.Rcpt("*****@*****.**"); err != nil {
		t.Fatalf("RCPT failed: %v", err)
	}

	time.Sleep(time.Second)

	if err := c.Quit(); err != nil {
		t.Fatalf("Quit failed: %v", err)
	}

}
Exemple #24
0
func sendMail(to, from, content string) error {
	// Check that the SMTP server is OK and connect.
	c, err := smtp.Dial("localhost:25")
	if err != nil {
		log.Println("Could not send e-mail, check your local SMTP-configuration. Got following error:")
		return err
	}

	// Set sender.
	err = c.Mail(from)
	if err != nil {
		return err
	}

	// Set recipient.
	c.Rcpt(to)
	if err != nil {
		return err
	}

	// Write the content to the mail buffer.
	wc, err := c.Data()
	if err != nil {
		return err
	}
	defer wc.Close()
	buf := bytes.NewBufferString(content)
	_, err = buf.WriteTo(wc)
	if err != nil {
		return err
	}

	// Mail successfully sent.
	return nil
}
func TestAuthRejection(t *testing.T) {

	addr, closer := runsslserver(t, &smtpd.Server{
		Authenticator: func(peer smtpd.Peer, username, password string) error {
			return smtpd.Error{Code: 550, Message: "Denied"}
		},
		ForceTLS: true,
	})

	defer closer()

	c, err := smtp.Dial(addr)
	if err != nil {
		t.Fatalf("Dial failed: %v", err)
	}

	if err := c.StartTLS(&tls.Config{InsecureSkipVerify: true}); err != nil {
		t.Fatalf("STARTTLS failed: %v", err)
	}

	if err := c.Auth(smtp.PlainAuth("foo", "foo", "bar", "127.0.0.1")); err == nil {
		t.Fatal("Auth worked despite rejection")
	}

}
Exemple #26
0
func main() {
	c, err := smtp.Dial("")
	if err != nil {
		log.Fatal(err)
	}
	defer c.Quit()

	if err := c.Mail(""); err != nil {
		log.Fatal(err)
	}

	if err := c.Rcpt(""); err != nil {
		log.Fatal(err)
	}

	wc, err := c.Data()
	if err != nil {
		log.Fatal(err)
	}
	defer wc.Close()

	_, err = fmt.Fprintf(wc, "this is email body")
	if err != nil {
		log.Fatal(err)
	}

}
func TestHELO(t *testing.T) {

	addr, closer := runserver(t, &smtpd.Server{})

	defer closer()

	c, err := smtp.Dial(addr)
	if err != nil {
		t.Fatalf("Dial failed: %v", err)
	}

	if err := cmd(c.Text, 502, "MAIL FROM:<*****@*****.**>"); err != nil {
		t.Fatalf("MAIL didn't fail: %v", err)
	}

	if err := cmd(c.Text, 250, "HELO localhost"); err != nil {
		t.Fatalf("HELO failed: %v", err)
	}

	if err := cmd(c.Text, 502, "MAIL FROM:[email protected]"); err != nil {
		t.Fatalf("MAIL didn't fail: %v", err)
	}

	if err := cmd(c.Text, 250, "HELO localhost"); err != nil {
		t.Fatalf("HELO failed: %v", err)
	}

	if err := c.Quit(); err != nil {
		t.Fatalf("Quit failed: %v", err)
	}

}
Exemple #28
0
// Send sends the email, returning any error encountered.
func (e *Email) Send() error {
	if e.From == "" {
		return errors.New("Error: No sender specified. Please set the Email.From field.")
	}
	if e.To == nil || len(e.To) == 0 {
		return errors.New("Error: No recipient specified. Please set the Email.To field.")
	}
	if e.Password == "" {
		return errors.New("Error: No password specified. Please set the Email.Password field.")
	}

	auth := smtp.PlainAuth(
		"",
		e.From,
		e.Password,
		"smtp.gmail.com",
	)

	conn, err := smtp.Dial("smtp.gmail.com:587")
	if err != nil {
		return err
	}

	err = conn.StartTLS(&tls.Config{})
	if err != nil {
		return err
	}

	err = conn.Auth(auth)
	if err != nil {
		return err
	}

	err = conn.Mail(e.From)
	if err != nil {
		if strings.Contains(err.Error(), "530 5.5.1") {
			return errors.New("Error: Authentication failure. Your username or password is incorrect.")
		}
		return err
	}

	for _, recipient := range e.To {
		err = conn.Rcpt(recipient)
		if err != nil {
			return err
		}
	}

	wc, err := conn.Data()
	if err != nil {
		return err
	}
	defer wc.Close()
	_, err = wc.Write(e.Bytes())
	if err != nil {
		return err
	}

	return nil
}
Exemple #29
0
func Example() {
	// Connect to the remote SMTP server.
	c, err := smtp.Dial("mail.example.com:25")
	if err != nil {
		log.Fatal(err)
	}

	// Set the sender and recipient first
	if err := c.Mail("*****@*****.**"); err != nil {
		log.Fatal(err)
	}
	if err := c.Rcpt("*****@*****.**"); err != nil {
		log.Fatal(err)
	}

	// Send the email body.
	wc, err := c.Data()
	if err != nil {
		log.Fatal(err)
	}
	_, err = fmt.Fprintf(wc, "This is the email body")
	if err != nil {
		log.Fatal(err)
	}
	err = wc.Close()
	if err != nil {
		log.Fatal(err)
	}

	// Send the QUIT command and close the connection.
	err = c.Quit()
	if err != nil {
		log.Fatal(err)
	}
}
Exemple #30
0
func TestInterruptedDATA(t *testing.T) {
	addr, closer := runserver(t, &smtpd.Server{
		Handler: func(peer smtpd.Peer, env smtpd.Envelope) error {
			t.Fatal("Accepted DATA despite disconnection")
			return nil
		},
	})
	defer closer()

	c, err := smtp.Dial(addr)
	if err != nil {
		t.Fatalf("Dial failed: %v", err)
	}
	defer c.Close()

	if err := c.Mail("*****@*****.**"); err != nil {
		t.Fatalf("MAIL failed: %v", err)
	}

	if err := c.Rcpt("*****@*****.**"); err != nil {
		t.Fatalf("RCPT failed: %v", err)
	}

	wc, err := c.Data()
	if err != nil {
		t.Fatalf("Data failed: %v", err)
	}

	_, err = fmt.Fprintf(wc, "This is the email body")
	if err != nil {
		t.Fatalf("Data body failed: %v", err)
	}
}