Skip to content

Commit

Permalink
Fix localhost problen in HELO/EHLO command
Browse files Browse the repository at this point in the history
Fix `localhost` problem in HELO/EHLO smtp command.
Replcaed `SendMail` func with a new self defined func.
  • Loading branch information
Koichi MATSUMOTO committed Nov 26, 2020
1 parent ff34443 commit 04acfff
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 4 deletions.
5 changes: 4 additions & 1 deletion server/tinode.conf
Original file line number Diff line number Diff line change
Expand Up @@ -270,11 +270,14 @@
// Only 'addr-spec' of RFC 5322 string will be used for authentication.
"sender": "\"Tinode\" <noreply@example.com>",

// Optional login to use for authentication; if missing, "sender" will be used.
// Login to use for authentication; if missing, no authentication.
"login": "john.doe@example.com",

// Password to use when authenticating the sender.
"sender_password": "your-password-here",

// Server name to use in SMTP
"servername": "example.com"

// Optional list of human languages to try to load templates for. If you don't care about i18n,
// leave it blank or remove. The first language in the list is the default language.
Expand Down
69 changes: 66 additions & 3 deletions server/validate/email/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ package email

import (
"bytes"
"crypto/tls"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"io"
"log"
"math/rand"
qp "mime/quotedprintable"
Expand Down Expand Up @@ -49,6 +51,8 @@ type validator struct {
SMTPAddr string `json:"smtp_server"`
// Port of the SMTP server.
SMTPPort string `json:"smtp_port"`
// ServerName used in SMTP HELO/EHLO command.
ServerName string `json:"servername"`
// Optional whitelist of email domains accepted for registration.
Domains []string `json:"domains"`

Expand Down Expand Up @@ -167,8 +171,8 @@ func (v *validator) Init(jsonconf string) error {
if v.Login != "" {
v.auth = smtp.PlainAuth("", v.Login, v.SenderPassword, v.SMTPAddr)
} else {
// SendFrom could be an RFC 5322 address of the form "John Doe <jdoe@example.com>". Parse it.
v.auth = smtp.PlainAuth("", v.senderEmail, v.SenderPassword, v.SMTPAddr)
// if unset v.Login, skip authentication
v.auth = nil
}

// Optionally resolve paths against the location of this executable file.
Expand Down Expand Up @@ -434,6 +438,64 @@ func (v *validator) Remove(user t.Uid, value string) error {
return store.Users.DelCred(user, validatorName, value)
}

// SendMail replacement
//
func (v *validator) Send_Mail(rcpt []string, msg []byte) error {

client, err := smtp.Dial(v.SMTPAddr + ":" + v.SMTPPort)
if err != nil {
return err
}
defer client.Close()
err = client.Hello(v.ServerName)
if err != nil {
return err
}
istls, _ := client.Extension("starttls")
if istls {
tlsconfig := &tls.Config{
InsecureSkipVerify: true,
ServerName: v.SMTPAddr,
}
err = client.StartTLS(tlsconfig)
if err != nil {
return err
}
}
isauth, _ := client.Extension("auth")
if v.auth != nil && isauth {
err = client.Auth(v.auth)
if err != nil {
return err
}
}
err = client.Reset()
if err != nil {
return err
}
err = client.Mail(v.senderEmail)
if err != nil {
return err
}
for _, to := range rcpt {
err = client.Rcpt(to)
if err != nil {
return err
}
}
var w io.WriteCloser
w, err = client.Data()
if err != nil {
return err
}
_, err = w.Write([]byte(msg))
if err != nil {
return err
}
w.Close()
return client.Quit()
}

// This is a basic SMTP sender which connects to a server using login/password.
// -
// See here how to send email from Amazon SES:
Expand Down Expand Up @@ -488,7 +550,8 @@ func (v *validator) send(to string, content *emailContent) error {
}
message.WriteString("\r\n")

err := smtp.SendMail(v.SMTPAddr+":"+v.SMTPPort, v.auth, v.senderEmail, []string{to}, message.Bytes())
// err := smtp.SendMail(v.SMTPAddr+":"+v.SMTPPort, v.auth, v.senderEmail, []string{to}, message.Bytes())
err := v.Send_Mail([]string{to}, message.Bytes())
if err != nil {
log.Println("SMTP error", to, err)
}
Expand Down

0 comments on commit 04acfff

Please sign in to comment.