LoginSignup
4
2

More than 3 years have passed since last update.

mailコマンドでメールを送信する

Posted at

はじめに

サーバーからメール送信するため、mailコマンドを使えるようにします。

環境

CentOS 8

パッケージのインストール

$ sudo dnf install mailx msmtp

mailxはmailコマンド、msmtpはSMTPクライアントです。

mailxの設定

mail.rcファイルの最後に以下の内容を追記します。

$ tail -n 1 /etc/mail.rc
set sendmail="/usr/bin/msmpt"

msmtpの設定

サンプルファイルをコピーしてパーミッションを変更します。

$ cp /usr/share/doc/msmtp/msmtprc-user.example ~/.msmtprc

GmailのSMTPサーバーを設定します。

$ cat ~/.msmtprc
# Set default values for all following accounts.
defaults
port 587
tls on
tls_trust_file /etc/ssl/certs/ca-bundle.crt

# Gmail
account gmail
host smtp.gmail.com  ←SMTPサーバー
from user@gmail.com  ←送信元
auth on
user user@gmail.com  ←ユーザー名
password password  ←パスワード

# Set a default account
account default : gmail

パスワードが含まれているのでパーミッションを変更します。

$ chmod 600 ~/.msmtprc

メールの送信テスト

$ echo "test mail" | mail -s "test mail" user@gmail.com

受信できることを確認します。
mailtest.png

4
2
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
4
2