0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Rubyを使ってメール通知をしたいだけなのにどハマりした結果

Last updated at Posted at 2022-08-25

はじめに

新しくサーバーを構築して、自作Webアプリケーションにメール通知機能を追加したいと思い、簡単にできるだろうと考えていたらハマりました。

環境

Ubuntu 22.x.x
Ruby 3.0.x
Postfix 3.6.x

ソースコード

mailtest.rb
#!/usr/bin/env ruby

require 'mail'

mail = Mail.new do
  from    "xxxx@xxxx.com"
  to      "yyyy@yyyy.com"
  subject "test mail"
  body    "this is a test mail."
end

mail.delivery_method(:smtp,
  address: "localhost",
  port:    25,
  domain:  "localdomain",
)

mail.deliver

エラーメッセージ

$ ./mailtest.rb
/usr/lib/ruby/3.0.0/net/smtp.rb:1015:in `check_auth_response': 503 5.5.1 Error: authentication not enabled (Net::SMTPAuthenticationError)
	from /usr/lib/ruby/3.0.0/net/smtp.rb:776:in `auth_plain'
	from /usr/lib/ruby/3.0.0/net/smtp.rb:768:in `public_send'
	from /usr/lib/ruby/3.0.0/net/smtp.rb:768:in `authenticate'
	from /usr/lib/ruby/3.0.0/net/smtp.rb:604:in `do_start'
	from /usr/lib/ruby/3.0.0/net/smtp.rb:557:in `start'
	from /var/lib/gems/3.0.0/gems/mail-2.7.1/lib/mail/network/delivery_methods/smtp.rb:109:in `start_smtp_session'
	from /var/lib/gems/3.0.0/gems/mail-2.7.1/lib/mail/network/delivery_methods/smtp.rb:100:in `deliver!'
	from /var/lib/gems/3.0.0/gems/mail-2.7.1/lib/mail/message.rb:2159:in `do_delivery'
	from /var/lib/gems/3.0.0/gems/mail-2.7.1/lib/mail/message.rb:262:in `deliver'
	from ./mailtest.rb:18:in `<main>'

試したこと

以下の設定を追加するといいよと教えてもらって追記したりしました。

mail.delivery_method(:smtp,
  ...
  ssl: false,
  enable_starttls_auto: false,
  openssl_verify_mode: 'none',
)

...ダメでした。

結果

Postfixの設定を変更して解決しました。

/etc/postfix/main.cf

以下のようにTLS関連の設定をコメントアウト。

# TLS parameters
#smtpd_tls_cert_file=/etc/ssl/certs/ssl-cert-snakeoil.pem
#smtpd_tls_key_file=/etc/ssl/private/ssl-cert-snakeoil.key
#smtpd_tls_security_level=may

#smtp_tls_CApath=/etc/ssl/certs
#smtp_tls_security_level=may
#smtp_tls_session_cache_database = btree:${data_directory}/smtp_scache

変更後にPostfixを再起動

$ sudo systemctl restart postfix

再度スクリプトを実行。

$ ./mailtest.rb

できた!!

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?