{"record":{"id":"45f6627d18b69393","repo":"Billionmail/BillionMail","slug":"failed-to-connect-to-smtp-server-v-45f662","errorCode":null,"errorMessage":"failed to connect to SMTP server: %v","messagePattern":"failed to connect to SMTP server: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"core/internal/service/domains/blacklist.go","lineNumber":485,"sourceCode":"\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"failed to parse alert settings: %v\", err)\n\t}\n\n\treturn &settings, nil\n}\n\nfunc sendAlertEmail(ctx context.Context, settings *BlacklistAlertSettings, subject, body string) error {\n\n\tsender := mail_service.NewEmailSender()\n\tsender.Host = settings.SMTPServer\n\tsender.Port = fmt.Sprintf(\"%d\", settings.SMTPPort)\n\tsender.Email = settings.SenderEmail\n\tsender.UserName = settings.SenderEmail\n\tsender.Password = settings.SMTPPassword\n\n\terr := sender.Connect()\n\tif err != nil {\n\t\treturn fmt.Errorf(\"failed to connect to SMTP server: %v\", err)\n\t}\n\tdefer sender.Close()\n\n\tvar failedRecipients []string\n\tfor _, recipient := range settings.RecipientList {\n\n\t\tmessage := mail_service.NewMessage(subject, body)\n\t\tmessage.SetRealName(settings.Name)\n\t\tmessageId := sender.GenerateMessageID()\n\t\tmessage.SetMessageID(messageId)\n\n\t\terr = sender.Send(message, []string{recipient})\n\t\tif err != nil {\n\t\t\tg.Log().Errorf(ctx, \"Failed to send alert email to %s: %v\", recipient, err)\n\t\t\tfailedRecipients = append(failedRecipients, recipient)\n\t\t} else {\n\t\t\tg.Log().Infof(ctx, \"Alert email sent successfully to %s\", recipient)\n\t\t}","sourceCodeStart":467,"sourceCodeEnd":503,"githubUrl":"https://github.com/Billionmail/BillionMail/blob/fc36c76c050c3775c5e899faf7403cf0262d2744/core/internal/service/domains/blacklist.go#L467-L503","documentation":"sendAlertEmail builds an SMTP sender from the alert settings (host, port, sender email, password) and calls sender.Connect(); a failed connection is wrapped as 'failed to connect to SMTP server: %v'. This is a transport-level failure reaching/authenticating the TCP SMTP endpoint before any message is sent.","triggerScenarios":"Called from sendBlacklistAlert when: the configured SMTP host is wrong or unreachable, the port is blocked by a firewall, TLS negotiation fails, credentials are rejected during connect/handshake, or DNS for the SMTP host fails.","commonSituations":"Using port 465 (implicit TLS) with a client expecting STARTTLS on 587 or vice versa; password with special characters mis-stored; outbound port 25/465/587 blocked by cloud provider; SMTP relay requires auth the settings lack; internal mail server (Postfix) not running.","solutions":["Test connectivity from the server: nc -vz <smtp-host> <port> or openssl s_client -connect host:port -starttls smtp","Confirm host/port/encryption mode match the provider (465=implicit TLS, 587=STARTTLS, 25=relay)","Re-enter the SMTP username/password in alert settings to rule out stored-credential typos","Check firewall/security-group egress rules for the SMTP port and verify the local Postfix service if using localhost"],"exampleFix":"// before\nerr := sender.Connect()\nif err != nil {\n\treturn fmt.Errorf(\"failed to connect to SMTP server: %v\", err)\n}\n// after\nerr := sender.Connect()\nif err != nil {\n\treturn fmt.Errorf(\"failed to connect to SMTP server %s:%d as %s: %v\", settings.SMTPHost, settings.SMTPPort, settings.SenderEmail, err)\n}","handlingStrategy":"try-catch","validationCode":"// verify reachability and credentials before triggering alerts\nconn, err := net.DialTimeout(\"tcp\", fmt.Sprintf(\"%s:%d\", settings.SMTPHost, settings.SMTPPort), 5*time.Second)\nif err != nil {\n\treturn fmt.Errorf(\"SMTP host %s:%d unreachable\", settings.SMTPHost, settings.SMTPPort)\n}\nconn.Close()","typeGuard":"func smtpSettingsUsable(s *BlacklistAlertSettings) bool {\n\treturn s.SMTPHost != \"\" && s.SMTPPort > 0 && s.SenderEmail != \"\" && s.SMTPPassword != \"\"\n}","tryCatchPattern":"err := sendAlertEmail(ctx, settings, subject, body)\nif err != nil && strings.Contains(err.Error(), \"failed to connect to SMTP server\") {\n\t// transport failure: check host/port/TLS mode and queue or retry later\n}","preventionTips":["Match port to encryption mode: 465 implicit TLS, 587 STARTTLS, 25 relay","Open egress firewall/security-group rules for the SMTP port","Test credentials with swaks or openssl s_client before saving settings","Monitor the local Postfix service if pointing alerts at localhost"],"tags":["smtp","email","network"],"backgroundTag":"smtp-connection-failed","analyzedSha":"fc36c76c050c3775c5e899faf7403cf0262d2744","analyzedAt":"2026-09-05T21:28:54.019Z","contentChangedAt":"2026-09-05T21:28:54.019Z","schemaVersion":2},"datasetVersion":"2026-09-12T22:17:10.623Z"}