Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add headers to SMTP client to prevent being tagged as spam #970

Merged
merged 6 commits into from
Jul 24, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions changelog/unreleased/smtp-headers.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Enhancement: Add required headers to SMTP client to prevent being tagged as spam

Mails being sent through the client, specially through unauthenticated SMTP were
being tagged as spam due to missing headers.

https://github.com/cs3org/reva/pull/970
48 changes: 34 additions & 14 deletions pkg/smtpclient/smtpclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,13 @@ package smtpclient

import (
"bytes"
"encoding/base64"
"fmt"
"net/smtp"
"os"
"time"

"github.com/google/uuid"
"github.com/pkg/errors"
)

Expand All @@ -37,21 +41,34 @@ type SMTPCredentials struct {

// SendMail allows sending mails using a set of client credentials.
func (creds *SMTPCredentials) SendMail(recipient, subject, body string) error {

header := map[string]string{
"From": creds.SenderMail,
"To": recipient,
"Subject": subject,
"Date": time.Now().Format(time.RFC1123Z),
"Message-ID": uuid.New().String(),
"MIME-Version": "1.0",
"Content-Type": "text/plain; charset=\"utf-8\"",
"Content-Transfer-Encoding": "base64",
}

message := ""
for k, v := range header {
message += fmt.Sprintf("%s: %s\r\n", k, v)
}
message += "\r\n" + base64.StdEncoding.EncodeToString([]byte(body))

if creds.DisableAuth {
return creds.sendMailSMTP(recipient, subject, body)
return creds.sendMailSMTP(recipient, subject, message)
}
return creds.sendMailAuthSMTP(recipient, subject, body)
return creds.sendMailAuthSMTP(recipient, subject, message)
}

func (creds *SMTPCredentials) sendMailAuthSMTP(recipient, subject, body string) error {
func (creds *SMTPCredentials) sendMailAuthSMTP(recipient, subject, message string) error {

auth := smtp.PlainAuth("", creds.SenderMail, creds.SenderPassword, creds.SMTPServer)

message := "From: " + creds.SenderMail + "\n" +
"To: " + recipient + "\n" +
"Subject: " + subject + "\n\n" +
body

err := smtp.SendMail(
fmt.Sprintf("%s:%d", creds.SMTPServer, creds.SMTPPort),
auth,
Expand All @@ -67,14 +84,22 @@ func (creds *SMTPCredentials) sendMailAuthSMTP(recipient, subject, body string)
return nil
}

func (creds *SMTPCredentials) sendMailSMTP(recipient, subject, body string) error {
func (creds *SMTPCredentials) sendMailSMTP(recipient, subject, message string) error {

c, err := smtp.Dial(fmt.Sprintf("%s:%d", creds.SMTPServer, creds.SMTPPort))
if err != nil {
return err
}
defer c.Close()

host, err := os.Hostname()
ishank011 marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return err
}
if err = c.Hello(host); err != nil {
ishank011 marked this conversation as resolved.
Show resolved Hide resolved
return err
}

if err = c.Mail(creds.SenderMail); err != nil {
return err
}
Expand All @@ -88,12 +113,7 @@ func (creds *SMTPCredentials) sendMailSMTP(recipient, subject, body string) erro
}
defer wc.Close()

message := "From: " + creds.SenderMail + "\n" +
"To: " + recipient + "\n" +
"Subject: " + subject + "\n\n" +
body
buf := bytes.NewBufferString(message)

if _, err = buf.WriteTo(wc); err != nil {
return err
}
Expand Down