大量に送るわけでもないけどサーバから送られる自動通知メールがゴミ箱や迷惑フォルダに入らないといいなと思ってちょっとやってみたので備忘録です。(大量というかある一定以上に送ると送信制限かかるときいた)
SendGridをつかった理由
- Azure(※)とGCPはAWSみたいなメールの上限緩和申請ができないのでメール送りたいならそのSaaS使うのが一般的らしいと聞いた。(※実はAzureは契約次第で制限無いか申請可能だった模様)
- MTA(メールトランスファーエージェントつまりメールサーバ)たてても外部への通信時にOP25Bということでちょっとなら届くようだけど大量だと制限されてとどかなくなる。スパマー対策ならしょうがない。
- 国内の携帯キャリアのシビアなブラックリストを唯一突破できるらしいと聞いて。
- 職場ですでに使ってる人がいたので便乗
→(※)その後教えていただいた情報、Azure VMのOP25Bですが、EA契約はデフォルトで制限無しでCSPとMOSPはサービスプロバイダーからSRを上げれば審査の上で解除とのことでした。(そうだったのか)
https://blogs.technet.microsoft.com/jpaztech/2017/11/16/smtp-block-announcement-november-2017/
AzureのSendGridのリソース登録をする
AzureのSendGridのリソース登録方法は以下のあたりからたどれるとおもいます
https://sendgrid.kke.co.jp/blog/?p=2621
今回はさきに共用のがつくられてたのでそこに向かって飛ばすためのサーバ側ですることを備忘録します。
SendGridへの認証情報を取得する
AzureのSendGridリソースの左上のあたりのManageと書かれたリンクから管理画面に入りAPIキーを作成する
https://sendgrid.kke.co.jp/docs/Tutorials/A_Transaction_Mail/manage_api_key.html
https://app.sendgrid.com/settings/api_keys
メールMTAからSendGridにSaml認証SMTPリレーの設定をする
つくったAPIキーをPostfixに設定する際に以下のような項目を追加します。
(APIキーにするとオーナーアカウント知らなくても大丈夫)
https://sendgrid.kke.co.jp/docs/Integrate/Mail_Servers/postfix.html
inet_protocols = ipv4
myhostname = 仮想サーバのホスト名
mydomain = 利用するドメイン名
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = static:apikey:***APIkey***
smtp_sasl_security_options = noanonymous
smtp_tls_security_level = encrypt
header_size_limit = 4096000
relayhost = [smtp.sendgrid.net]:587
アカウント使うなら
smtp_sasl_password_maps = static:account:password
というかんじです。
inet_protocolsなどを更新する場合reloadだと怒られるのでrestartするとよいようです。
$ sudo systemctl status postfix
$ sudo systemctl restart postfix
$ sudo systemctl status postfix
$ echo mail-send-test|mail -s mail-test-subject $mailaddress
$ sudo tail -f /var/log/maillog
設定反映してメールだしてみて届いてるか確認をします。手動はこれでおわり。
何台も刺身タンポポ撲滅に備えてansibleのタスクいちおう以下の感じに。(localhost適用例)
---
- name: install postfix apt
apt:
name: postfix
state: present
when:
- ansible_os_family == 'Debian'
tags: install-postfix
- name: install postfix yum
yum:
name: postfix
state: present
when:
- ansible_os_family == 'RedHat'
tags: install-postfix
- name: set main.cf for postfix send only sendgrid client
lineinfile:
dest: '/etc/postfix/main.cf'
state: present
backup: yes
regexp: '{{ item.regexp }}'
line: '{{ item.line }}'
notify:
- restart postfix
with_items:
- regexp: '^inet_protocols.*'
line: 'inet_protocols = ipv4'
- regexp: '^myhostname.*'
line: 'myhostname = {{ postfix_myhostname }}'
- regexp: '^mydomain.*'
line: 'mydomain = {{ postfix_mydomain }}'
- regexp: '^smtp_sasl_auth_enable.*'
line: 'smtp_sasl_auth_enable = {{ postfix_sendgrid_enable }}'
- regexp: '^smtp_sasl_password_maps.*'
line: 'smtp_sasl_password_maps = static:{{ postfix_sendgrid_user }}:{{ postfix_sendgrid_password }}'
- regexp: '^smtp_sasl_security_options.*'
line: 'smtp_sasl_security_options = {{ postfix_sendgrid_security_options }}'
- regexp: '^smtp_tls_security_level.*'
line: 'smtp_tls_security_level = {{ postfix_sendgrid_tls_security_level }}'
- regexp: '^header_size_limit.*'
line: 'header_size_limit = {{ postfix_sendgrid_header_size_limit }}'
- regexp: '^relayhost.*'
line: 'relayhost = {{ postfix_sendgrid_relayhost }}'
when: postfix_role == 'sendonly_sendgrid_client'
tags: set-config-postfix-sendonly-sendgrid-client
複数行ブロック追加はlineinfileじゃなくてblockinfileとかでもいいっぽい。
---
# handlers file for postfix
- name: restart postfix
service:
name: postfix
state: restarted
---
# if postfix_role == sendonly_sendgrid_client, then set follow lines.
postfix_sendgrid_enable: ''
postfix_sendgrid_user: ''
postfix_sendgrid_password: ''
postfix_sendgrid_security_options: ''
postfix_sendgrid_tls_security_level: ''
postfix_sendgrid_header_size_limit: ''
postfix_sendgrid_relayhost: ''
# ex)
# postfix_role: sendonly_sendgrid_client
# postfix_sendgrid_enable: 'yes'
# postfix_sendgrid_user: 'apikey'
## postfix_sendgrid_password: 'use vault.yml'
# postfix_sendgrid_security_options: 'noanonymous'
# postfix_sendgrid_tls_security_level: 'encrypt'
# postfix_sendgrid_header_size_limit: '4096000'
# postfix_sendgrid_relayhost: '[smtp.sendgrid.net]587'
postfix_myhostname: myhost1
postfix_mydomain: mydomain1
postfix_role: sendonly_sendgrid_client
postfix_sendgrid_enable: 'yes'
postfix_sendgrid_user: 'apikey'
# postfix_sendgrid_password: 'use vault.yml'
postfix_sendgrid_security_options: 'noanonymous'
postfix_sendgrid_tls_security_level: 'encrypt'
postfix_sendgrid_header_size_limit: '4096000'
postfix_sendgrid_relayhost: '[smtp.sendgrid.net]:587'
$ ansible-vault --help
$ ansible-vault create group_vars/vault.yml --ask-vault-pass
$ ansible-vault edit group_vars/vault.yml --ask-vault-pass
$ ansible-vault view group_vars/vault.yml --ask-vault-pass
postfix_sendgrid_password: '***APIkey***'
$ cat group_vars/vault.yml
$ vi hosts
[hostgroup1]
localhost
$ vi site.yml
- name: Playbook sample
hosts: hostgruop1
vars_files:
- group_vars/hostgruop1.yml
- group_vars/vault.yml
become: true
#gather_facts: no
roles:
- role: postfix
$ ansible-playbook -c local -i hosts site.yml -C -D -vv --ask-sudo-pass --ask-vault-pass --tags=set-config-postfix-sendonly-sendgrid-client
$ ansible-playbook -c local -i hosts site.yml -D -vv --ask-sudo-pass --ask-vault-pass --tags=set-config-postfix-sendonly-sendgrid-client
以上です。