🐖

【Go】dial failed: STARTTLS mode set to: "TLSMandatory" but ...

2023/12/27に公開

はじめに

https://blog.tkzwtks.net/entry/2023/02/17/171905

を元にメール送信機能を実装していたときに下記のエラーに遭遇したので、その対処法を共有します

dial failed: STARTTLS mode set to: "TLSMandatory" but target host does not support STARTTLS
  • 使用しているのはmailhogです

https://github.com/mailhog

対処法

  • TLSOpportunisticをsetしてあげる(もしくはNoTLS)
c, err := mail.NewClient(host, mail.WithPort(port))
c.SetTLSPolicy(mail.TLSOpportunistic)

https://pkg.go.dev/github.com/wneessen/go-mail#TLSMandatory

const (
	// TLSMandatory requires that the connection cto the server is
	// encrypting using STARTTLS. If the server does not support STARTTLS
	// the connection will be terminated with an error
	TLSMandatory TLSPolicy = iota

	// TLSOpportunistic tries cto establish an encrypted connection via the
	// STARTTLS protocol. If the server does not support this, it will fall
	// back cto non-encrypted plaintext transmission
	TLSOpportunistic

	// NoTLS forces the transaction cto be not encrypted
	NoTLS
)

原因

  • エラーを直訳するとSTARTTLSモードに「TLSMandatory」をsetされたけど、Mailhogはそのモードに対応してないよといった具合でしょうか
  • TLSポリシーについては下記にまとめてみました
TLSポリシー 説明 使用シナリオ
TLSMandatory 通信が必ずSTARTTLSを使用して暗号化されることを要求します。 個人情報や機密情報を含むメールの送信など、セキュリティが最優先事項の場合
TLSOpportunistic STARTTLSを試み、サポートされていない場合は平文にフォールバックします。 セキュリティが望ましいが、非暗号化でも許容される場合
NoTLS トランザクションが暗号化されないことを強制します。 開発環境や内部ネットワークなど、セキュリティが問題にならない環境
  • 下記のissueが残ったままなのでTLSMandatory接続はサポートされていないみたいですね
  • というか、そもそもテスト用を想定されているアプリケーションなので厳重なセキュリティ下での使用は想定されていないのではと思いました

https://github.com/mailhog/MailHog/issues/296

Discussion