2019-02-10 07:07:37 +05:30
// Copyright 2019 The Gitea Authors. All rights reserved.
2022-11-27 23:50:29 +05:30
// SPDX-License-Identifier: MIT
2019-02-10 07:07:37 +05:30
package setting
import (
2023-05-17 02:25:51 +05:30
"context"
Rework mailer settings (#18982)
* `PROTOCOL`: can be smtp, smtps, smtp+startls, smtp+unix, sendmail, dummy
* `SMTP_ADDR`: domain for SMTP, or path to unix socket
* `SMTP_PORT`: port for SMTP; defaults to 25 for `smtp`, 465 for `smtps`, and 587 for `smtp+startls`
* `ENABLE_HELO`, `HELO_HOSTNAME`: reverse `DISABLE_HELO` to `ENABLE_HELO`; default to false + system hostname
* `FORCE_TRUST_SERVER_CERT`: replace the unclear `SKIP_VERIFY`
* `CLIENT_CERT_FILE`, `CLIENT_KEY_FILE`, `USE_CLIENT_CERT`: clarify client certificates here
Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
2022-08-02 10:54:18 +05:30
"net"
2019-02-10 07:07:37 +05:30
"net/mail"
Rework mailer settings (#18982)
* `PROTOCOL`: can be smtp, smtps, smtp+startls, smtp+unix, sendmail, dummy
* `SMTP_ADDR`: domain for SMTP, or path to unix socket
* `SMTP_PORT`: port for SMTP; defaults to 25 for `smtp`, 465 for `smtps`, and 587 for `smtp+startls`
* `ENABLE_HELO`, `HELO_HOSTNAME`: reverse `DISABLE_HELO` to `ENABLE_HELO`; default to false + system hostname
* `FORCE_TRUST_SERVER_CERT`: replace the unclear `SKIP_VERIFY`
* `CLIENT_CERT_FILE`, `CLIENT_KEY_FILE`, `USE_CLIENT_CERT`: clarify client certificates here
Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
2022-08-02 10:54:18 +05:30
"strings"
2024-07-15 02:57:00 +05:30
"text/template"
2020-05-03 04:34:31 +05:30
"time"
2019-02-10 07:07:37 +05:30
"code.gitea.io/gitea/modules/log"
2019-08-23 22:10:30 +05:30
2019-02-10 07:07:37 +05:30
shellquote "github.com/kballard/go-shellquote"
)
// Mailer represents mail service.
type Mailer struct {
// Mailer
2024-06-04 00:12:52 +05:30
Name string ` ini:"NAME" `
From string ` ini:"FROM" `
EnvelopeFrom string ` ini:"ENVELOPE_FROM" `
OverrideEnvelopeFrom bool ` ini:"-" `
FromName string ` ini:"-" `
FromEmail string ` ini:"-" `
SendAsPlainText bool ` ini:"SEND_AS_PLAIN_TEXT" `
SubjectPrefix string ` ini:"SUBJECT_PREFIX" `
OverrideHeader map [ string ] [ ] string ` ini:"-" `
2019-02-10 07:07:37 +05:30
// SMTP sender
2022-11-27 15:38:40 +05:30
Protocol string ` ini:"PROTOCOL" `
SMTPAddr string ` ini:"SMTP_ADDR" `
SMTPPort string ` ini:"SMTP_PORT" `
User string ` ini:"USER" `
Passwd string ` ini:"PASSWD" `
EnableHelo bool ` ini:"ENABLE_HELO" `
HeloHostname string ` ini:"HELO_HOSTNAME" `
ForceTrustServerCert bool ` ini:"FORCE_TRUST_SERVER_CERT" `
UseClientCert bool ` ini:"USE_CLIENT_CERT" `
ClientCertFile string ` ini:"CLIENT_CERT_FILE" `
ClientKeyFile string ` ini:"CLIENT_KEY_FILE" `
2019-02-10 07:07:37 +05:30
// Sendmail sender
2022-11-27 15:38:40 +05:30
SendmailPath string ` ini:"SENDMAIL_PATH" `
SendmailArgs [ ] string ` ini:"-" `
SendmailTimeout time . Duration ` ini:"SENDMAIL_TIMEOUT" `
SendmailConvertCRLF bool ` ini:"SENDMAIL_CONVERT_CRLF" `
2024-07-15 02:57:00 +05:30
// Customization
FromDisplayNameFormat string ` ini:"FROM_DISPLAY_NAME_FORMAT" `
FromDisplayNameFormatTemplate * template . Template ` ini:"-" `
2019-02-10 07:07:37 +05:30
}
2022-01-20 23:16:10 +05:30
// MailService the global mailer
var MailService * Mailer
2019-02-10 07:07:37 +05:30
2023-02-19 21:42:01 +05:30
func loadMailsFrom ( rootCfg ConfigProvider ) {
loadMailerFrom ( rootCfg )
loadRegisterMailFrom ( rootCfg )
loadNotifyMailFrom ( rootCfg )
loadIncomingEmailFrom ( rootCfg )
}
func loadMailerFrom ( rootCfg ConfigProvider ) {
2023-01-12 01:39:24 +05:30
sec := rootCfg . Section ( "mailer" )
2019-02-10 07:07:37 +05:30
// Check mailer setting.
if ! sec . Key ( "ENABLED" ) . MustBool ( ) {
return
}
2022-11-27 15:38:40 +05:30
// Handle Deprecations and map on to new configuration
2023-02-21 03:48:26 +05:30
// DEPRECATED should not be removed because users maybe upgrade from lower version to the latest version
// if these are removed, the warning will not be shown
deprecatedSetting ( rootCfg , "mailer" , "MAILER_TYPE" , "mailer" , "PROTOCOL" , "v1.19.0" )
Rework mailer settings (#18982)
* `PROTOCOL`: can be smtp, smtps, smtp+startls, smtp+unix, sendmail, dummy
* `SMTP_ADDR`: domain for SMTP, or path to unix socket
* `SMTP_PORT`: port for SMTP; defaults to 25 for `smtp`, 465 for `smtps`, and 587 for `smtp+startls`
* `ENABLE_HELO`, `HELO_HOSTNAME`: reverse `DISABLE_HELO` to `ENABLE_HELO`; default to false + system hostname
* `FORCE_TRUST_SERVER_CERT`: replace the unclear `SKIP_VERIFY`
* `CLIENT_CERT_FILE`, `CLIENT_KEY_FILE`, `USE_CLIENT_CERT`: clarify client certificates here
Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
2022-08-02 10:54:18 +05:30
if sec . HasKey ( "MAILER_TYPE" ) && ! sec . HasKey ( "PROTOCOL" ) {
if sec . Key ( "MAILER_TYPE" ) . String ( ) == "sendmail" {
2022-11-27 15:38:40 +05:30
sec . Key ( "PROTOCOL" ) . MustString ( "sendmail" )
Rework mailer settings (#18982)
* `PROTOCOL`: can be smtp, smtps, smtp+startls, smtp+unix, sendmail, dummy
* `SMTP_ADDR`: domain for SMTP, or path to unix socket
* `SMTP_PORT`: port for SMTP; defaults to 25 for `smtp`, 465 for `smtps`, and 587 for `smtp+startls`
* `ENABLE_HELO`, `HELO_HOSTNAME`: reverse `DISABLE_HELO` to `ENABLE_HELO`; default to false + system hostname
* `FORCE_TRUST_SERVER_CERT`: replace the unclear `SKIP_VERIFY`
* `CLIENT_CERT_FILE`, `CLIENT_KEY_FILE`, `USE_CLIENT_CERT`: clarify client certificates here
Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
2022-08-02 10:54:18 +05:30
}
2019-02-10 07:07:37 +05:30
}
2023-02-21 03:48:26 +05:30
deprecatedSetting ( rootCfg , "mailer" , "HOST" , "mailer" , "SMTP_ADDR" , "v1.19.0" )
Rework mailer settings (#18982)
* `PROTOCOL`: can be smtp, smtps, smtp+startls, smtp+unix, sendmail, dummy
* `SMTP_ADDR`: domain for SMTP, or path to unix socket
* `SMTP_PORT`: port for SMTP; defaults to 25 for `smtp`, 465 for `smtps`, and 587 for `smtp+startls`
* `ENABLE_HELO`, `HELO_HOSTNAME`: reverse `DISABLE_HELO` to `ENABLE_HELO`; default to false + system hostname
* `FORCE_TRUST_SERVER_CERT`: replace the unclear `SKIP_VERIFY`
* `CLIENT_CERT_FILE`, `CLIENT_KEY_FILE`, `USE_CLIENT_CERT`: clarify client certificates here
Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
2022-08-02 10:54:18 +05:30
if sec . HasKey ( "HOST" ) && ! sec . HasKey ( "SMTP_ADDR" ) {
givenHost := sec . Key ( "HOST" ) . String ( )
addr , port , err := net . SplitHostPort ( givenHost )
2023-01-12 01:39:24 +05:30
if err != nil && strings . Contains ( err . Error ( ) , "missing port in address" ) {
addr = givenHost
} else if err != nil {
Rework mailer settings (#18982)
* `PROTOCOL`: can be smtp, smtps, smtp+startls, smtp+unix, sendmail, dummy
* `SMTP_ADDR`: domain for SMTP, or path to unix socket
* `SMTP_PORT`: port for SMTP; defaults to 25 for `smtp`, 465 for `smtps`, and 587 for `smtp+startls`
* `ENABLE_HELO`, `HELO_HOSTNAME`: reverse `DISABLE_HELO` to `ENABLE_HELO`; default to false + system hostname
* `FORCE_TRUST_SERVER_CERT`: replace the unclear `SKIP_VERIFY`
* `CLIENT_CERT_FILE`, `CLIENT_KEY_FILE`, `USE_CLIENT_CERT`: clarify client certificates here
Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
2022-08-02 10:54:18 +05:30
log . Fatal ( "Invalid mailer.HOST (%s): %v" , givenHost , err )
2019-02-10 07:07:37 +05:30
}
2023-01-12 01:39:24 +05:30
if addr == "" {
addr = "127.0.0.1"
}
2022-11-27 15:38:40 +05:30
sec . Key ( "SMTP_ADDR" ) . MustString ( addr )
sec . Key ( "SMTP_PORT" ) . MustString ( port )
2019-02-10 07:07:37 +05:30
}
2023-02-21 03:48:26 +05:30
deprecatedSetting ( rootCfg , "mailer" , "IS_TLS_ENABLED" , "mailer" , "PROTOCOL" , "v1.19.0" )
Rework mailer settings (#18982)
* `PROTOCOL`: can be smtp, smtps, smtp+startls, smtp+unix, sendmail, dummy
* `SMTP_ADDR`: domain for SMTP, or path to unix socket
* `SMTP_PORT`: port for SMTP; defaults to 25 for `smtp`, 465 for `smtps`, and 587 for `smtp+startls`
* `ENABLE_HELO`, `HELO_HOSTNAME`: reverse `DISABLE_HELO` to `ENABLE_HELO`; default to false + system hostname
* `FORCE_TRUST_SERVER_CERT`: replace the unclear `SKIP_VERIFY`
* `CLIENT_CERT_FILE`, `CLIENT_KEY_FILE`, `USE_CLIENT_CERT`: clarify client certificates here
Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
2022-08-02 10:54:18 +05:30
if sec . HasKey ( "IS_TLS_ENABLED" ) && ! sec . HasKey ( "PROTOCOL" ) {
if sec . Key ( "IS_TLS_ENABLED" ) . MustBool ( ) {
2022-11-27 15:38:40 +05:30
sec . Key ( "PROTOCOL" ) . MustString ( "smtps" )
Rework mailer settings (#18982)
* `PROTOCOL`: can be smtp, smtps, smtp+startls, smtp+unix, sendmail, dummy
* `SMTP_ADDR`: domain for SMTP, or path to unix socket
* `SMTP_PORT`: port for SMTP; defaults to 25 for `smtp`, 465 for `smtps`, and 587 for `smtp+startls`
* `ENABLE_HELO`, `HELO_HOSTNAME`: reverse `DISABLE_HELO` to `ENABLE_HELO`; default to false + system hostname
* `FORCE_TRUST_SERVER_CERT`: replace the unclear `SKIP_VERIFY`
* `CLIENT_CERT_FILE`, `CLIENT_KEY_FILE`, `USE_CLIENT_CERT`: clarify client certificates here
Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
2022-08-02 10:54:18 +05:30
} else {
2022-11-27 15:38:40 +05:30
sec . Key ( "PROTOCOL" ) . MustString ( "smtp+starttls" )
Rework mailer settings (#18982)
* `PROTOCOL`: can be smtp, smtps, smtp+startls, smtp+unix, sendmail, dummy
* `SMTP_ADDR`: domain for SMTP, or path to unix socket
* `SMTP_PORT`: port for SMTP; defaults to 25 for `smtp`, 465 for `smtps`, and 587 for `smtp+startls`
* `ENABLE_HELO`, `HELO_HOSTNAME`: reverse `DISABLE_HELO` to `ENABLE_HELO`; default to false + system hostname
* `FORCE_TRUST_SERVER_CERT`: replace the unclear `SKIP_VERIFY`
* `CLIENT_CERT_FILE`, `CLIENT_KEY_FILE`, `USE_CLIENT_CERT`: clarify client certificates here
Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
2022-08-02 10:54:18 +05:30
}
}
2023-02-21 03:48:26 +05:30
deprecatedSetting ( rootCfg , "mailer" , "DISABLE_HELO" , "mailer" , "ENABLE_HELO" , "v1.19.0" )
2022-11-27 15:38:40 +05:30
if sec . HasKey ( "DISABLE_HELO" ) && ! sec . HasKey ( "ENABLE_HELO" ) {
sec . Key ( "ENABLE_HELO" ) . MustBool ( ! sec . Key ( "DISABLE_HELO" ) . MustBool ( ) )
}
2023-02-21 03:48:26 +05:30
deprecatedSetting ( rootCfg , "mailer" , "SKIP_VERIFY" , "mailer" , "FORCE_TRUST_SERVER_CERT" , "v1.19.0" )
2022-11-27 15:38:40 +05:30
if sec . HasKey ( "SKIP_VERIFY" ) && ! sec . HasKey ( "FORCE_TRUST_SERVER_CERT" ) {
sec . Key ( "FORCE_TRUST_SERVER_CERT" ) . MustBool ( sec . Key ( "SKIP_VERIFY" ) . MustBool ( ) )
}
2023-02-21 03:48:26 +05:30
deprecatedSetting ( rootCfg , "mailer" , "USE_CERTIFICATE" , "mailer" , "USE_CLIENT_CERT" , "v1.19.0" )
2022-11-27 15:38:40 +05:30
if sec . HasKey ( "USE_CERTIFICATE" ) && ! sec . HasKey ( "USE_CLIENT_CERT" ) {
sec . Key ( "USE_CLIENT_CERT" ) . MustBool ( sec . Key ( "USE_CERTIFICATE" ) . MustBool ( ) )
}
2023-02-21 03:48:26 +05:30
deprecatedSetting ( rootCfg , "mailer" , "CERT_FILE" , "mailer" , "CLIENT_CERT_FILE" , "v1.19.0" )
2022-11-27 15:38:40 +05:30
if sec . HasKey ( "CERT_FILE" ) && ! sec . HasKey ( "CLIENT_CERT_FILE" ) {
sec . Key ( "CERT_FILE" ) . MustString ( sec . Key ( "CERT_FILE" ) . String ( ) )
}
2023-02-21 03:48:26 +05:30
deprecatedSetting ( rootCfg , "mailer" , "KEY_FILE" , "mailer" , "CLIENT_KEY_FILE" , "v1.19.0" )
2022-11-27 15:38:40 +05:30
if sec . HasKey ( "KEY_FILE" ) && ! sec . HasKey ( "CLIENT_KEY_FILE" ) {
sec . Key ( "KEY_FILE" ) . MustString ( sec . Key ( "KEY_FILE" ) . String ( ) )
}
2023-02-21 03:48:26 +05:30
deprecatedSetting ( rootCfg , "mailer" , "ENABLE_HTML_ALTERNATIVE" , "mailer" , "SEND_AS_PLAIN_TEXT" , "v1.19.0" )
2022-11-27 15:38:40 +05:30
if sec . HasKey ( "ENABLE_HTML_ALTERNATIVE" ) && ! sec . HasKey ( "SEND_AS_PLAIN_TEXT" ) {
sec . Key ( "SEND_AS_PLAIN_TEXT" ) . MustBool ( ! sec . Key ( "ENABLE_HTML_ALTERNATIVE" ) . MustBool ( false ) )
}
if sec . HasKey ( "PROTOCOL" ) && sec . Key ( "PROTOCOL" ) . String ( ) == "smtp+startls" {
log . Error ( "Deprecated fallback `[mailer]` `PROTOCOL = smtp+startls` present. Use `[mailer]` `PROTOCOL = smtp+starttls`` instead. This fallback will be removed in v1.19.0" )
sec . Key ( "PROTOCOL" ) . SetValue ( "smtp+starttls" )
}
2024-04-22 19:28:19 +05:30
// Handle aliases
if sec . HasKey ( "USERNAME" ) && ! sec . HasKey ( "USER" ) {
sec . Key ( "USER" ) . SetValue ( sec . Key ( "USERNAME" ) . String ( ) )
}
if sec . HasKey ( "PASSWORD" ) && ! sec . HasKey ( "PASSWD" ) {
sec . Key ( "PASSWD" ) . SetValue ( sec . Key ( "PASSWORD" ) . String ( ) )
}
2022-11-27 15:38:40 +05:30
// Set default values & validate
sec . Key ( "NAME" ) . MustString ( AppName )
sec . Key ( "PROTOCOL" ) . In ( "" , [ ] string { "smtp" , "smtps" , "smtp+starttls" , "smtp+unix" , "sendmail" , "dummy" } )
sec . Key ( "ENABLE_HELO" ) . MustBool ( true )
sec . Key ( "FORCE_TRUST_SERVER_CERT" ) . MustBool ( false )
sec . Key ( "USE_CLIENT_CERT" ) . MustBool ( false )
sec . Key ( "SENDMAIL_PATH" ) . MustString ( "sendmail" )
sec . Key ( "SENDMAIL_TIMEOUT" ) . MustDuration ( 5 * time . Minute )
sec . Key ( "SENDMAIL_CONVERT_CRLF" ) . MustBool ( true )
sec . Key ( "FROM" ) . MustString ( sec . Key ( "USER" ) . String ( ) )
// Now map the values on to the MailService
MailService = & Mailer { }
if err := sec . MapTo ( MailService ) ; err != nil {
log . Fatal ( "Unable to map [mailer] section on to MailService. Error: %v" , err )
}
2024-06-04 00:12:52 +05:30
overrideHeader := rootCfg . Section ( "mailer.override_header" ) . Keys ( )
MailService . OverrideHeader = make ( map [ string ] [ ] string )
for _ , key := range overrideHeader {
MailService . OverrideHeader [ key . Name ( ) ] = key . Strings ( "," )
}
2022-11-27 15:38:40 +05:30
// Infer SMTPPort if not set
Rework mailer settings (#18982)
* `PROTOCOL`: can be smtp, smtps, smtp+startls, smtp+unix, sendmail, dummy
* `SMTP_ADDR`: domain for SMTP, or path to unix socket
* `SMTP_PORT`: port for SMTP; defaults to 25 for `smtp`, 465 for `smtps`, and 587 for `smtp+startls`
* `ENABLE_HELO`, `HELO_HOSTNAME`: reverse `DISABLE_HELO` to `ENABLE_HELO`; default to false + system hostname
* `FORCE_TRUST_SERVER_CERT`: replace the unclear `SKIP_VERIFY`
* `CLIENT_CERT_FILE`, `CLIENT_KEY_FILE`, `USE_CLIENT_CERT`: clarify client certificates here
Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
2022-08-02 10:54:18 +05:30
if MailService . SMTPPort == "" {
switch MailService . Protocol {
case "smtp" :
MailService . SMTPPort = "25"
case "smtps" :
MailService . SMTPPort = "465"
2022-11-27 15:38:40 +05:30
case "smtp+starttls" :
Rework mailer settings (#18982)
* `PROTOCOL`: can be smtp, smtps, smtp+startls, smtp+unix, sendmail, dummy
* `SMTP_ADDR`: domain for SMTP, or path to unix socket
* `SMTP_PORT`: port for SMTP; defaults to 25 for `smtp`, 465 for `smtps`, and 587 for `smtp+startls`
* `ENABLE_HELO`, `HELO_HOSTNAME`: reverse `DISABLE_HELO` to `ENABLE_HELO`; default to false + system hostname
* `FORCE_TRUST_SERVER_CERT`: replace the unclear `SKIP_VERIFY`
* `CLIENT_CERT_FILE`, `CLIENT_KEY_FILE`, `USE_CLIENT_CERT`: clarify client certificates here
Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
2022-08-02 10:54:18 +05:30
MailService . SMTPPort = "587"
}
}
2022-11-27 15:38:40 +05:30
// Infer Protocol
Rework mailer settings (#18982)
* `PROTOCOL`: can be smtp, smtps, smtp+startls, smtp+unix, sendmail, dummy
* `SMTP_ADDR`: domain for SMTP, or path to unix socket
* `SMTP_PORT`: port for SMTP; defaults to 25 for `smtp`, 465 for `smtps`, and 587 for `smtp+startls`
* `ENABLE_HELO`, `HELO_HOSTNAME`: reverse `DISABLE_HELO` to `ENABLE_HELO`; default to false + system hostname
* `FORCE_TRUST_SERVER_CERT`: replace the unclear `SKIP_VERIFY`
* `CLIENT_CERT_FILE`, `CLIENT_KEY_FILE`, `USE_CLIENT_CERT`: clarify client certificates here
Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
2022-08-02 10:54:18 +05:30
if MailService . Protocol == "" {
if strings . ContainsAny ( MailService . SMTPAddr , "/\\" ) {
MailService . Protocol = "smtp+unix"
} else {
switch MailService . SMTPPort {
case "25" :
MailService . Protocol = "smtp"
case "465" :
MailService . Protocol = "smtps"
case "587" :
2022-11-27 15:38:40 +05:30
MailService . Protocol = "smtp+starttls"
Rework mailer settings (#18982)
* `PROTOCOL`: can be smtp, smtps, smtp+startls, smtp+unix, sendmail, dummy
* `SMTP_ADDR`: domain for SMTP, or path to unix socket
* `SMTP_PORT`: port for SMTP; defaults to 25 for `smtp`, 465 for `smtps`, and 587 for `smtp+startls`
* `ENABLE_HELO`, `HELO_HOSTNAME`: reverse `DISABLE_HELO` to `ENABLE_HELO`; default to false + system hostname
* `FORCE_TRUST_SERVER_CERT`: replace the unclear `SKIP_VERIFY`
* `CLIENT_CERT_FILE`, `CLIENT_KEY_FILE`, `USE_CLIENT_CERT`: clarify client certificates here
Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
2022-08-02 10:54:18 +05:30
default :
log . Error ( "unable to infer unspecified mailer.PROTOCOL from mailer.SMTP_PORT = %q, assume using smtps" , MailService . SMTPPort )
MailService . Protocol = "smtps"
2023-01-12 01:39:24 +05:30
if MailService . SMTPPort == "" {
MailService . SMTPPort = "465"
}
Rework mailer settings (#18982)
* `PROTOCOL`: can be smtp, smtps, smtp+startls, smtp+unix, sendmail, dummy
* `SMTP_ADDR`: domain for SMTP, or path to unix socket
* `SMTP_PORT`: port for SMTP; defaults to 25 for `smtp`, 465 for `smtps`, and 587 for `smtp+startls`
* `ENABLE_HELO`, `HELO_HOSTNAME`: reverse `DISABLE_HELO` to `ENABLE_HELO`; default to false + system hostname
* `FORCE_TRUST_SERVER_CERT`: replace the unclear `SKIP_VERIFY`
* `CLIENT_CERT_FILE`, `CLIENT_KEY_FILE`, `USE_CLIENT_CERT`: clarify client certificates here
Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
2022-08-02 10:54:18 +05:30
}
}
}
// we want to warn if users use SMTP on a non-local IP;
// we might as well take the opportunity to check that it has an IP at all
2023-01-09 21:39:46 +05:30
// This check is not needed for sendmail
switch MailService . Protocol {
case "sendmail" :
var err error
MailService . SendmailArgs , err = shellquote . Split ( sec . Key ( "SENDMAIL_ARGS" ) . String ( ) )
if err != nil {
log . Error ( "Failed to parse Sendmail args: '%s' with error %v" , sec . Key ( "SENDMAIL_ARGS" ) . String ( ) , err )
}
case "smtp" , "smtps" , "smtp+starttls" , "smtp+unix" :
ips := tryResolveAddr ( MailService . SMTPAddr )
if MailService . Protocol == "smtp" {
for _ , ip := range ips {
2023-05-17 02:25:51 +05:30
if ! ip . IP . IsLoopback ( ) {
2023-01-09 21:39:46 +05:30
log . Warn ( "connecting over insecure SMTP protocol to non-local address is not recommended" )
break
}
Rework mailer settings (#18982)
* `PROTOCOL`: can be smtp, smtps, smtp+startls, smtp+unix, sendmail, dummy
* `SMTP_ADDR`: domain for SMTP, or path to unix socket
* `SMTP_PORT`: port for SMTP; defaults to 25 for `smtp`, 465 for `smtps`, and 587 for `smtp+startls`
* `ENABLE_HELO`, `HELO_HOSTNAME`: reverse `DISABLE_HELO` to `ENABLE_HELO`; default to false + system hostname
* `FORCE_TRUST_SERVER_CERT`: replace the unclear `SKIP_VERIFY`
* `CLIENT_CERT_FILE`, `CLIENT_KEY_FILE`, `USE_CLIENT_CERT`: clarify client certificates here
Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
2022-08-02 10:54:18 +05:30
}
}
2023-01-09 21:39:46 +05:30
case "dummy" : // just mention and do nothing
Rework mailer settings (#18982)
* `PROTOCOL`: can be smtp, smtps, smtp+startls, smtp+unix, sendmail, dummy
* `SMTP_ADDR`: domain for SMTP, or path to unix socket
* `SMTP_PORT`: port for SMTP; defaults to 25 for `smtp`, 465 for `smtps`, and 587 for `smtp+startls`
* `ENABLE_HELO`, `HELO_HOSTNAME`: reverse `DISABLE_HELO` to `ENABLE_HELO`; default to false + system hostname
* `FORCE_TRUST_SERVER_CERT`: replace the unclear `SKIP_VERIFY`
* `CLIENT_CERT_FILE`, `CLIENT_KEY_FILE`, `USE_CLIENT_CERT`: clarify client certificates here
Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
2022-08-02 10:54:18 +05:30
}
if MailService . From != "" {
parsed , err := mail . ParseAddress ( MailService . From )
if err != nil {
log . Fatal ( "Invalid mailer.FROM (%s): %v" , MailService . From , err )
}
MailService . FromName = parsed . Name
MailService . FromEmail = parsed . Address
} else {
log . Error ( "no mailer.FROM provided, email system may not work." )
2019-02-10 07:07:37 +05:30
}
2024-07-15 02:57:00 +05:30
MailService . FromDisplayNameFormatTemplate , _ = template . New ( "mailFrom" ) . Parse ( "{{ .DisplayName }}" )
if MailService . FromDisplayNameFormat != "" {
template , err := template . New ( "mailFrom" ) . Parse ( MailService . FromDisplayNameFormat )
if err != nil {
log . Error ( "mailer.FROM_DISPLAY_NAME_FORMAT is no valid template: %v" , err )
} else {
MailService . FromDisplayNameFormatTemplate = template
}
}
2021-11-19 21:05:20 +05:30
switch MailService . EnvelopeFrom {
case "" :
MailService . OverrideEnvelopeFrom = false
case "<>" :
MailService . EnvelopeFrom = ""
MailService . OverrideEnvelopeFrom = true
default :
Rework mailer settings (#18982)
* `PROTOCOL`: can be smtp, smtps, smtp+startls, smtp+unix, sendmail, dummy
* `SMTP_ADDR`: domain for SMTP, or path to unix socket
* `SMTP_PORT`: port for SMTP; defaults to 25 for `smtp`, 465 for `smtps`, and 587 for `smtp+startls`
* `ENABLE_HELO`, `HELO_HOSTNAME`: reverse `DISABLE_HELO` to `ENABLE_HELO`; default to false + system hostname
* `FORCE_TRUST_SERVER_CERT`: replace the unclear `SKIP_VERIFY`
* `CLIENT_CERT_FILE`, `CLIENT_KEY_FILE`, `USE_CLIENT_CERT`: clarify client certificates here
Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
2022-08-02 10:54:18 +05:30
parsed , err := mail . ParseAddress ( MailService . EnvelopeFrom )
2021-11-19 21:05:20 +05:30
if err != nil {
log . Fatal ( "Invalid mailer.ENVELOPE_FROM (%s): %v" , MailService . EnvelopeFrom , err )
}
MailService . OverrideEnvelopeFrom = true
MailService . EnvelopeFrom = parsed . Address
}
2019-02-10 07:07:37 +05:30
log . Info ( "Mail Service Enabled" )
}
2023-02-19 21:42:01 +05:30
func loadRegisterMailFrom ( rootCfg ConfigProvider ) {
if ! rootCfg . Section ( "service" ) . Key ( "REGISTER_EMAIL_CONFIRM" ) . MustBool ( ) {
2019-02-10 07:07:37 +05:30
return
} else if MailService == nil {
log . Warn ( "Register Mail Service: Mail Service is not enabled" )
return
}
Service . RegisterEmailConfirm = true
log . Info ( "Register Mail Service Enabled" )
}
2023-02-19 21:42:01 +05:30
func loadNotifyMailFrom ( rootCfg ConfigProvider ) {
if ! rootCfg . Section ( "service" ) . Key ( "ENABLE_NOTIFY_MAIL" ) . MustBool ( ) {
2019-02-10 07:07:37 +05:30
return
} else if MailService == nil {
log . Warn ( "Notify Mail Service: Mail Service is not enabled" )
return
}
Service . EnableNotifyMail = true
log . Info ( "Notify Mail Service Enabled" )
}
Rework mailer settings (#18982)
* `PROTOCOL`: can be smtp, smtps, smtp+startls, smtp+unix, sendmail, dummy
* `SMTP_ADDR`: domain for SMTP, or path to unix socket
* `SMTP_PORT`: port for SMTP; defaults to 25 for `smtp`, 465 for `smtps`, and 587 for `smtp+startls`
* `ENABLE_HELO`, `HELO_HOSTNAME`: reverse `DISABLE_HELO` to `ENABLE_HELO`; default to false + system hostname
* `FORCE_TRUST_SERVER_CERT`: replace the unclear `SKIP_VERIFY`
* `CLIENT_CERT_FILE`, `CLIENT_KEY_FILE`, `USE_CLIENT_CERT`: clarify client certificates here
Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
2022-08-02 10:54:18 +05:30
2023-05-17 02:25:51 +05:30
func tryResolveAddr ( addr string ) [ ] net . IPAddr {
Rework mailer settings (#18982)
* `PROTOCOL`: can be smtp, smtps, smtp+startls, smtp+unix, sendmail, dummy
* `SMTP_ADDR`: domain for SMTP, or path to unix socket
* `SMTP_PORT`: port for SMTP; defaults to 25 for `smtp`, 465 for `smtps`, and 587 for `smtp+startls`
* `ENABLE_HELO`, `HELO_HOSTNAME`: reverse `DISABLE_HELO` to `ENABLE_HELO`; default to false + system hostname
* `FORCE_TRUST_SERVER_CERT`: replace the unclear `SKIP_VERIFY`
* `CLIENT_CERT_FILE`, `CLIENT_KEY_FILE`, `USE_CLIENT_CERT`: clarify client certificates here
Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
2022-08-02 10:54:18 +05:30
if strings . HasPrefix ( addr , "[" ) && strings . HasSuffix ( addr , "]" ) {
addr = addr [ 1 : len ( addr ) - 1 ]
}
ip := net . ParseIP ( addr )
if ip != nil {
2023-05-17 02:25:51 +05:30
return [ ] net . IPAddr { { IP : ip } }
Rework mailer settings (#18982)
* `PROTOCOL`: can be smtp, smtps, smtp+startls, smtp+unix, sendmail, dummy
* `SMTP_ADDR`: domain for SMTP, or path to unix socket
* `SMTP_PORT`: port for SMTP; defaults to 25 for `smtp`, 465 for `smtps`, and 587 for `smtp+startls`
* `ENABLE_HELO`, `HELO_HOSTNAME`: reverse `DISABLE_HELO` to `ENABLE_HELO`; default to false + system hostname
* `FORCE_TRUST_SERVER_CERT`: replace the unclear `SKIP_VERIFY`
* `CLIENT_CERT_FILE`, `CLIENT_KEY_FILE`, `USE_CLIENT_CERT`: clarify client certificates here
Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
2022-08-02 10:54:18 +05:30
}
2023-05-17 02:25:51 +05:30
ctx , cancel := context . WithTimeout ( context . Background ( ) , 2 * time . Second )
defer cancel ( )
ips , err := net . DefaultResolver . LookupIPAddr ( ctx , addr )
Rework mailer settings (#18982)
* `PROTOCOL`: can be smtp, smtps, smtp+startls, smtp+unix, sendmail, dummy
* `SMTP_ADDR`: domain for SMTP, or path to unix socket
* `SMTP_PORT`: port for SMTP; defaults to 25 for `smtp`, 465 for `smtps`, and 587 for `smtp+startls`
* `ENABLE_HELO`, `HELO_HOSTNAME`: reverse `DISABLE_HELO` to `ENABLE_HELO`; default to false + system hostname
* `FORCE_TRUST_SERVER_CERT`: replace the unclear `SKIP_VERIFY`
* `CLIENT_CERT_FILE`, `CLIENT_KEY_FILE`, `USE_CLIENT_CERT`: clarify client certificates here
Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
2022-08-02 10:54:18 +05:30
if err != nil {
log . Warn ( "could not look up mailer.SMTP_ADDR: %v" , err )
2023-05-17 02:25:51 +05:30
return nil
Rework mailer settings (#18982)
* `PROTOCOL`: can be smtp, smtps, smtp+startls, smtp+unix, sendmail, dummy
* `SMTP_ADDR`: domain for SMTP, or path to unix socket
* `SMTP_PORT`: port for SMTP; defaults to 25 for `smtp`, 465 for `smtps`, and 587 for `smtp+startls`
* `ENABLE_HELO`, `HELO_HOSTNAME`: reverse `DISABLE_HELO` to `ENABLE_HELO`; default to false + system hostname
* `FORCE_TRUST_SERVER_CERT`: replace the unclear `SKIP_VERIFY`
* `CLIENT_CERT_FILE`, `CLIENT_KEY_FILE`, `USE_CLIENT_CERT`: clarify client certificates here
Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
2022-08-02 10:54:18 +05:30
}
return ips
}