fix: smtp hello for tls connections (#180)

Co-authored-by: Elias Schneider <login@eliasschneider.com>
This commit is contained in:
Kyle Mendell
2025-01-27 04:37:50 -06:00
committed by GitHub
parent 04c7f180de
commit 781ff7ae7b
2 changed files with 56 additions and 28 deletions

View File

@@ -4,10 +4,6 @@ import (
"bytes" "bytes"
"crypto/tls" "crypto/tls"
"fmt" "fmt"
"github.com/stonith404/pocket-id/backend/internal/common"
"github.com/stonith404/pocket-id/backend/internal/model"
"github.com/stonith404/pocket-id/backend/internal/utils/email"
"gorm.io/gorm"
htemplate "html/template" htemplate "html/template"
"mime/multipart" "mime/multipart"
"mime/quotedprintable" "mime/quotedprintable"
@@ -17,6 +13,11 @@ import (
"os" "os"
ttemplate "text/template" ttemplate "text/template"
"time" "time"
"github.com/stonith404/pocket-id/backend/internal/common"
"github.com/stonith404/pocket-id/backend/internal/model"
"github.com/stonith404/pocket-id/backend/internal/utils/email"
"gorm.io/gorm"
) )
var netDialer = &net.Dialer{ var netDialer = &net.Dialer{
@@ -89,16 +90,31 @@ func SendEmail[V any](srv *EmailService, toEmail email.Address, template email.T
) )
c.Body(body) c.Body(body)
// Set up the TLS configuration // Connect to the SMTP server
client, err := srv.getSmtpClient()
if err != nil {
return fmt.Errorf("failed to connect to SMTP server: %w", err)
}
defer client.Close()
// Send the email
if err := srv.sendEmailContent(client, toEmail, c); err != nil {
return fmt.Errorf("send email content: %w", err)
}
return nil
}
func (srv *EmailService) getSmtpClient() (client *smtp.Client, err error) {
port := srv.appConfigService.DbConfig.SmtpPort.Value
smtpAddress := srv.appConfigService.DbConfig.SmtpHost.Value + ":" + port
tlsConfig := &tls.Config{ tlsConfig := &tls.Config{
InsecureSkipVerify: srv.appConfigService.DbConfig.SmtpSkipCertVerify.Value == "true", InsecureSkipVerify: srv.appConfigService.DbConfig.SmtpSkipCertVerify.Value == "true",
ServerName: srv.appConfigService.DbConfig.SmtpHost.Value, ServerName: srv.appConfigService.DbConfig.SmtpHost.Value,
} }
// Connect to the SMTP server // Connect to the SMTP server
port := srv.appConfigService.DbConfig.SmtpPort.Value
smtpAddress := srv.appConfigService.DbConfig.SmtpHost.Value + ":" + port
var client *smtp.Client
if srv.appConfigService.DbConfig.SmtpTls.Value == "false" { if srv.appConfigService.DbConfig.SmtpTls.Value == "false" {
client, err = srv.connectToSmtpServer(smtpAddress) client, err = srv.connectToSmtpServer(smtpAddress)
} else if port == "465" { } else if port == "465" {
@@ -112,24 +128,14 @@ func SendEmail[V any](srv *EmailService, toEmail email.Address, template email.T
tlsConfig, tlsConfig,
) )
} }
if err != nil { if err != nil {
return fmt.Errorf("failed to connect to SMTP server: %w", err) return nil, fmt.Errorf("failed to connect to SMTP server: %w", err)
}
defer client.Close()
// Set the hello message manually as for example Google rejects the default "localhost" value
hostname, err := os.Hostname()
if err == nil {
if err := client.Hello(hostname); err != nil {
return fmt.Errorf("failed to say hello to SMTP server: %w", err)
}
} }
// Set up the authentication if user or password are set
smtpUser := srv.appConfigService.DbConfig.SmtpUser.Value smtpUser := srv.appConfigService.DbConfig.SmtpUser.Value
smtpPassword := srv.appConfigService.DbConfig.SmtpPassword.Value smtpPassword := srv.appConfigService.DbConfig.SmtpPassword.Value
// Set up the authentication if user or password are set
if smtpUser != "" || smtpPassword != "" { if smtpUser != "" || smtpPassword != "" {
auth := smtp.PlainAuth("", auth := smtp.PlainAuth("",
srv.appConfigService.DbConfig.SmtpUser.Value, srv.appConfigService.DbConfig.SmtpUser.Value,
@@ -137,16 +143,11 @@ func SendEmail[V any](srv *EmailService, toEmail email.Address, template email.T
srv.appConfigService.DbConfig.SmtpHost.Value, srv.appConfigService.DbConfig.SmtpHost.Value,
) )
if err := client.Auth(auth); err != nil { if err := client.Auth(auth); err != nil {
return fmt.Errorf("failed to authenticate SMTP client: %w", err) return nil, fmt.Errorf("failed to authenticate SMTP client: %w", err)
} }
} }
// Send the email return client, err
if err := srv.sendEmailContent(client, toEmail, c); err != nil {
return fmt.Errorf("send email content: %w", err)
}
return nil
} }
func (srv *EmailService) connectToSmtpServer(serverAddr string) (*smtp.Client, error) { func (srv *EmailService) connectToSmtpServer(serverAddr string) (*smtp.Client, error) {
@@ -155,6 +156,15 @@ func (srv *EmailService) connectToSmtpServer(serverAddr string) (*smtp.Client, e
return nil, fmt.Errorf("failed to connect to SMTP server: %w", err) return nil, fmt.Errorf("failed to connect to SMTP server: %w", err)
} }
client, err := smtp.NewClient(conn, srv.appConfigService.DbConfig.SmtpHost.Value) client, err := smtp.NewClient(conn, srv.appConfigService.DbConfig.SmtpHost.Value)
if err != nil {
conn.Close()
return nil, fmt.Errorf("failed to create SMTP client: %w", err)
}
if err := srv.sendHelloCommand(client); err != nil {
return nil, fmt.Errorf("failed to say hello to SMTP server: %w", err)
}
return client, err return client, err
} }
@@ -174,6 +184,10 @@ func (srv *EmailService) connectToSmtpServerUsingImplicitTLS(serverAddr string,
return nil, fmt.Errorf("failed to create SMTP client: %w", err) return nil, fmt.Errorf("failed to create SMTP client: %w", err)
} }
if err := srv.sendHelloCommand(client); err != nil {
return nil, fmt.Errorf("failed to say hello to SMTP server: %w", err)
}
return client, nil return client, nil
} }
@@ -189,12 +203,26 @@ func (srv *EmailService) connectToSmtpServerUsingStartTLS(serverAddr string, tls
return nil, fmt.Errorf("failed to create SMTP client: %w", err) return nil, fmt.Errorf("failed to create SMTP client: %w", err)
} }
if err := srv.sendHelloCommand(client); err != nil {
return nil, fmt.Errorf("failed to say hello to SMTP server: %w", err)
}
if err := client.StartTLS(tlsConfig); err != nil { if err := client.StartTLS(tlsConfig); err != nil {
return nil, fmt.Errorf("failed to start TLS: %w", err) return nil, fmt.Errorf("failed to start TLS: %w", err)
} }
return client, nil return client, nil
} }
func (srv *EmailService) sendHelloCommand(client *smtp.Client) error {
hostname, err := os.Hostname()
if err == nil {
if err := client.Hello(hostname); err != nil {
return err
}
}
return nil
}
func (srv *EmailService) sendEmailContent(client *smtp.Client, toEmail email.Address, c *email.Composer) error { func (srv *EmailService) sendEmailContent(client *smtp.Client, toEmail email.Address, c *email.Composer) error {
if err := client.Mail(srv.appConfigService.DbConfig.SmtpFrom.Value); err != nil { if err := client.Mail(srv.appConfigService.DbConfig.SmtpFrom.Value); err != nil {
return fmt.Errorf("failed to set sender: %w", err) return fmt.Errorf("failed to set sender: %w", err)

View File

@@ -77,7 +77,7 @@
isSendingTestEmail = true; isSendingTestEmail = true;
await appConfigService await appConfigService
.sendTestEmail() .sendTestEmail()
.then(() => toast.success('Test email sent successfully to your Email address.')) .then(() => toast.success('Test email sent successfully to your email address.'))
.catch(() => .catch(() =>
toast.error('Failed to send test email. Check the server logs for more information.') toast.error('Failed to send test email. Check the server logs for more information.')
) )