内容
EC2からDockerで立ち上げたredmineで、OFFICE365へのmail送信が出来ず、ついにpythonでリレーメールすることで解決した。
完成コード
一応、自宅のNASでは動作した。月曜に会社の環境で再テストする。
なぜRedmineから送れなかったのかは、原因不明のままであるが・・・
注意点
- NASはIPアドレスとすること(ホスト名では送れず)
- 1025はポート開放すること
- dockerホストのアドレスでは送信不可(localhost)
python.py
import smtpd
import asyncore
import smtplib
import pdb
class CustomSMTPServer(smtpd.SMTPServer):
def process_message(self, peer, mailfrom, rcpttos, data, **kwargs):
# Forward the message to another SMTP server
smtpObj = smtplib.SMTP('smtp.office365.com', 587)
smtpObj.starttls()
smtpObj.login("アドレス","パスワード")
smtpObj.sendmail("送信元", "宛先", data)
server = CustomSMTPServer(('192.168.1.40', 1025), None)
asyncore.loop()
configuration.yml
default:
# Outgoing emails configuration
# See the examples below and the Rails guide for more configuration options:
# http://guides.rubyonrails.org/action_mailer_basics.html#action-mailer-configuration
email_delivery:
# ==== Simple SMTP server at localhost
delivery_method: :smtp
smtp_settings:
address: "192.168.1.40"
port: 1025
はまったこと
・Redmineだけで解決しようとしていたが、問題はメールサーバー側であった。
・ドッカー環境、EC2クラウド環境、さんざん突き詰めて丸3日悩む。
・メールサーバーも、ポート開放やなんやらで悩み倒す。
・最後が自前と言うのは自分のアイデアで解決 → CHATGPTはひらめきは与えてくれないが、アイデアを具現化するには役立った。