LoginSignup
5
6

More than 5 years have passed since last update.

【Django】postfixを使ってメールを送信する設定

Last updated at Posted at 2017-03-23

postfixのインストール

sendmailの停止

$ sudo service sendmail stop
Shutting down sm-client:                                   [  OK  ]
Shutting down sendmail:                                    [  OK  ]

postfixのインストール

$ sudo yum install postfix

以下でバージョン確認

$ postconf  mail_version
mail_version = 2.6.6

今回は2.6.6が入った

MTA切り替え設定、自動起動の設定

$ sudo alternatives --config mta

2 プログラムがあり 'mta' を提供します。

  選択       コマンド
-----------------------------------------------
*+ 1           /usr/sbin/sendmail.sendmail
   2           /usr/sbin/sendmail.postfix

Enter を押して現在の選択 [+] を保持するか、選択番号を入力します

2を入力し、postfixに切り替える

$ sudo chkconfig sendmail off
$ chkconfig --list sendmail
sendmail        0:off   1:off   2:off   3:off   4:off   5:off   6:off
$ sudo chkconfig --add postfix
$ sudo chkconfig postfix on
$ chkconfig --list postfix
postfix         0:off   1:off   2:on    3:on    4:on    5:on    6:off

sendmailを自動起動無効、postfixを自動起動有効にする

メール送信のテスト

$ sendmail foo@foo.co.jp
From:bar@bar.com
To:foo@foo.co.jp
Subject:テスト送信
テスト送信です
.

foo@foo.co.jpにメールが送信されているかを確認する
仮にpostdrop: warning: unable to look up public/pickup: No such file or directoryといったエラーが出た場合は下記で対処

$ sudo service postfix restart
Shutting down postfix:                                     [FAILED]
Starting postfix:                                          [  OK  ]


Djangoの設定

setting.pyの中で以下を追記する

setting.py
....
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'localhost'
EMAIL_PORT = 25
EMAIL_HOST_USER = ''
EMAIL_HOST_PASSWORD = ''
EMAIL_USE_TLS = False
DEFAULT_FROM_EMAIL = '送信者名 <test@example.com>'
....


/etc/postfix/main.cfの編集

$ sudo vi /etc/postfix/main.cf

下記を追記する

myhostname = mybox.example.com
mydestination = localhost.server.com, localhost, example.com

example.comの部分は送信メールアドレスのドメイン名を記載する

参考

Postfixをインストールしてメール送信してみる | 本日も乙
python - How do you configure Django to send mail through Postfix? - Stack Overflow

5
6
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
5
6