「Msmtp」の版間の差分

提供: ArchWiki
ナビゲーションに移動 検索に移動
(英語版の最新の内容に更新)
 
(4人の利用者による、間の11版が非表示)
1行目: 1行目:
 
{{Lowercase title}}
 
{{Lowercase title}}
 
[[Category:メールクライアント]]
 
[[Category:メールクライアント]]
[[Category:メールサーバー]]
+
[[Category:OpenPGP]]
 
[[en:Msmtp]]
 
[[en:Msmtp]]
 
{{Related articles start}}
 
{{Related articles start}}
 
{{Related|mutt}}
 
{{Related|mutt}}
 
{{Related|OfflineIMAP}}
 
{{Related|OfflineIMAP}}
  +
{{Related|SSMTP}}
  +
{{Related|S-nail}}
  +
{{Related|OpenSMTPD}}
 
{{Related articles end}}
 
{{Related articles end}}
   
[http://msmtp.sourceforge.net/ msmtp] はとてもシンプルで使いやすい SMTP クライアントです。[[Wikipedia:sendmail|sendmail]] と完全な互換性があります。
+
[http://msmtp.sourceforge.net/ msmtp] はとてもシンプルで使いやすい SMTP クライアントです。[[sendmail]] と完全な互換性があります。
   
== インストール ==
+
==インストール==
   
msmtp は {{Pkg|msmtp}} パッケージで[[インストール]]できます。さらに {{Pkg|msmtp-mta}} をインストールすることで msmtp に sendmail エイリアスが作成されます。
+
{{Pkg|msmtp}} パッケージで[[インストール]]できます。さらに {{Pkg|msmtp-mta}} をインストールすることで msmtp に sendmail エイリアスが作成されます。
   
== 基本設定 ==
+
==基本設定==
   
以下は msmtp の設定例です (パッケージに含まれている、{{ic|/usr/share/doc/msmtp/msmtprc-user.example}} にある通常ユーザーのサンプルが基になっています。システム設定ファイルは {{ic|/etc/msmtprc}} にあり、サンプルは {{ic|/usr/share/doc/msmtp/msmtprc-system.example}}) にあります):
+
msmtp バージョン 1.8.6 以降、設定ファイルを {{ic|~/.msmtprc}} か {{ic|$XDG_CONFIG_HOME/msmtp/config}} に配置することができます。以下は msmtp の設定例です (パッケージに含まれている、{{ic|/usr/share/doc/msmtp/msmtprc-user.example}} にある通常ユーザーのサンプルが基になっています。システム設定ファイルは {{ic|/etc/msmtprc}} にあり、サンプルは {{ic|/usr/share/doc/msmtp/msmtprc-system.example}} にあります):
  +
  +
{{Tip|Gmail を使う場合は、以下のどちらかが必要です:
  +
* [[#OAuth2 設定]] で OAuth を使う。
  +
* 二段階認証を使う場合は [https://myaccount.google.com/apppasswords アプリパスワードを作成します]。
  +
}}
   
 
{{hc|~/.msmtprc|
 
{{hc|~/.msmtprc|
29行目: 37行目:
 
account gmail
 
account gmail
 
host smtp.gmail.com
 
host smtp.gmail.com
port 587
+
port 465
 
from ''username''@gmail.com
 
from ''username''@gmail.com
 
user ''username''
 
user ''username''
39行目: 47行目:
 
from joe_smith@freemail.example
 
from joe_smith@freemail.example
 
...
 
...
  +
  +
# Accounts can inherit info from another account
  +
account postmasterfreemail : freemail
  +
from postmaster*@freemail.example
   
 
# Set a default account
 
# Set a default account
46行目: 58行目:
 
{{Note|SSL/TLS を使っていて "Server sent empty reply" エラーメッセージが表示される場合、[[#Server sent empty reply]] を見て下さい。}}
 
{{Note|SSL/TLS を使っていて "Server sent empty reply" エラーメッセージが表示される場合、[[#Server sent empty reply]] を見て下さい。}}
   
  +
ユーザーの設定ファイルは所有者が読み書きできるように明示的に設定しないと msmtp がエラーを吐きます:
The ''user'' configuration file must be explicitly readable/writeable to only it's owner or msmtp will fail:
 
   
 
$ chmod 600 ~/.msmtprc
 
$ chmod 600 ~/.msmtprc
   
  +
設定ファイルに平文でパスワードを保存したくない場合、''passwordeval'' を使って外部プログラムを起動するか、あるいは下の[[#パスワード管理|パスワード管理]]セクションを見てください。以下は [[GnuPG]] を使ってパスワードを暗号化する例です:
To avoid saving the password in plain text in the configuration file, use ''passwordeval'' to launch an external program. This example using Gnu PG is commonly used to perform decryption of a password:
 
   
echo -e "password\n" | gpg --encrypt -o .msmtp-gmail.gpg # enter id (email...)
+
$ echo -e "password\n" | gpg --encrypt -o .msmtp-gmail.gpg # enter id (email...)
   
  +
{{Warning|ほとんどのシェルはコマンド履歴を保存します (例: {{ic|.bash_history}} や {{ic|.zhistory}})。GPG でシェルの標準入力を使いたくない場合 {{ic|gpg --encrypt -o .msmtp-gmail.gpg -r <email> -}} コマンドを使ってください。最後のダッシュは打ち間違えではなく標準入力を使うために必要な文字です。コマンドを実行したら、パスワードを入力してから Control-d を押せば GPG によってパスワードが暗号化されます。}}
{{Warning |Most shells save command history(e.g. .bash_history .zhistory). To avoid this use gpg with shell stdin:
 
<code>gpg --encrypt -o .msmtp-gmail.gpg -r <email> -</code>. The ending dash is not a typo, rather it causes gpg to use stdin. After running that snippet of code, type in your password, press enter, and press Control-d so gpg can encrypt your password.}}
 
   
 
{{hc|~/.msmtprc|
 
{{hc|~/.msmtprc|
61行目: 72行目:
 
}}
 
}}
   
  +
=== OAuth2 設定 ===
== mail コマンドを使う ==
 
   
  +
[[Wikipedia:OAuth2|OAuth2]] は、基本的なユーザー名/パスワード認証がサイト構成によってサポートされていない場合、または他の点で望ましくない場合に、msmtp を安全に認証するために使用できます。
{{ic|mail}} コマンドを使ってメールを送信するには {{Pkg|s-nail}} パッケージをインストールする必要があります。{{Pkg|msmtp-mta}} をインストールするか {{ic|/etc/mail.rc}} を編集して sendmail クライアントを設定してください:
 
   
  +
{{Note|[https://superuser.com/questions/1723180/how-to-continue-using-msmtp-oauth-2-0-for-gmail-in-mutt-after-oob-deprecation この投稿] では、現在 Gmail に許可されている極めて制限されている機能について説明しています。}}
{{hc|/etc/mail.rc|2=set sendmail=/usr/bin/msmtp}}
 
   
  +
==== oama ====
A {{ic|.msmtprc}} file will need to be in the home of every user who want to send mail or alternatively the system wide {{ic|/etc/msmtprc}} can be used.
 
   
  +
msmtp だけでは OAuth2 資格情報を更新・認証することができません。包括的な解決方法は、[https://github.com/pdobsan/oama oama] ユーティリティを使うことです。これは IMAP/SMTP クライアントに OAuth2 資格情報の更新能力と認証を提供します。
msmtp also understands aliases. Add the following line to the defaults section of msmtprc or your local configuration file:
 
  +
  +
oama を使うには、{{AUR|oama-bin}} を [[インストール]] し、 msmtp を設定して使用できるようにします。
  +
  +
  +
# account at Google with oauth2 access
  +
account YOUR_EMAIL_NAME@gmail.com
  +
from YOUR_EMAIL_NAME@gmail.com
  +
user YOUR_EMAIL_NAME@gmail.com
  +
auth oauthbearer
  +
passwordeval oama access YOUR_EMAIL_NAME@gmail.com
  +
host smtp.gmail.com
  +
port 587
  +
tls on
  +
tls_trust_file /etc/ssl/certs/ca-certificates.crt
  +
  +
アクセストークン更新は、ユーザーには見えないバックグラウンドで自動的に行われます。
  +
  +
==mail コマンドを使う==
  +
  +
{{ic|mail}} コマンドを使ってメールを送信するには {{Pkg|s-nail}} パッケージをインストールする必要があります。これは、 {{ic|mailx}} コマンドも提供します。それに加えて {{ic|sendmail}} 互換の MTA が必要です。{{Pkg|msmtp-mta}} をインストールするか(これは {{ic|sendmail} を {{ic|msmtp}} へシンボリックリンクします) {{ic|/etc/mail.rc}} を編集して sendmail クライアントを設定してください:
  +
  +
{{hc|/etc/mail.rc|2=set mta=/usr/bin/msmtp}}
  +
  +
システム共通の {{ic|/etc/msmtprc}} ファイルを使用するか、{{ic|.msmtprc}} ファイルをメールを送信したい全てのユーザーのホームディレクトリに配置する必要があります。
  +
  +
msmtp ではエイリアスが使えます。以下の行を msmtprc のデフォルトセクションかローカルの設定ファイルに追加してください:
   
 
{{hc|/etc/msmtprc|2=aliases /etc/aliases}}
 
{{hc|/etc/msmtprc|2=aliases /etc/aliases}}
   
  +
そして {{ic|/etc}} にエイリアスファイルを作成してください:
and create an aliases file in {{ic|/etc}}
 
   
 
{{hc|/etc/aliases|2=# Example aliases file
 
{{hc|/etc/aliases|2=# Example aliases file
  +
 
 
# Send root to Joe and Jane
 
# Send root to Joe and Jane
 
root: joe_smith@example.com, jane_chang@example.com
 
root: joe_smith@example.com, jane_chang@example.com
  +
 
 
# Send everything else to admin
 
# Send everything else to admin
 
default: admin@domain.example}}
 
default: admin@domain.example}}
   
== 動作のテスト ==
+
==動作のテスト==
   
アカウントオプション ({{ic|1=--account=,-a}} で送信元として使用するアカウントを指定します:
+
アカウントオプション ({{ic|1=--account=,-a}}) で送信元として使用するアカウントを指定します:
   
 
$ echo "hello there username." | msmtp -a default ''username''@domain.com
 
$ echo "hello there username." | msmtp -a default ''username''@domain.com
  +
  +
もしくは、件名と本文の両方を送信するには:
  +
  +
$ printf "Subject: Test\n\nhello there username." | msmtp -a default ''username''@domain.com
   
 
もしくは、ファイルにアドレスを書いて使用するには:
 
もしくは、ファイルにアドレスを書いて使用するには:
94行目: 135行目:
 
From: ''username''@gmail.com
 
From: ''username''@gmail.com
 
Subject: A test
 
Subject: A test
  +
 
 
Hello there.
 
Hello there.
   
 
$ cat test.mail | msmtp -a default <username>@domain.com
 
$ cat test.mail | msmtp -a default <username>@domain.com
   
  +
{{Tip|''-a default'' の代わりに ''--read-envelope-from'' を使うことで送信するメッセージの ''From:'' に使われるアカウントが自動的に選択されます。また、 {{ic|--read-recipients,-t}} を使うことで、コマンドラインで指定した受信者に加えてメールの To、CC、Bcc ヘッダーから受信者のアドレスを読み込むことができます。}}
{{Tip|If using Gmail you'll need to allow "Less Secure Apps" in ''Settings'' > ''Security''. Make sure to sign out of your other Gmail accounts first because the security settings part of Google Accounts can not manage concurrent sessions of more than one account.}}
 
   
== Cronie のデフォルトメールクライアント ==
+
==Cronie のデフォルトメールクライアント==
  +
  +
{{Out of date|Arch は cronie の代わりに [[systemd/タイマー]] を使います。}}
   
 
{{Pkg|cronie}} で sendmail ではなく msmtp を使うようにするには、{{Pkg|msmtp-mta}} をインストールするか、{{ic|cronie.service}} systemd ユニットを編集してください:
 
{{Pkg|cronie}} で sendmail ではなく msmtp を使うようにするには、{{Pkg|msmtp-mta}} をインストールするか、{{ic|cronie.service}} systemd ユニットを編集してください:
109行目: 152行目:
 
ExecStart&#61;/usr/bin/crond -n -m '/usr/bin/msmtp -t'}}
 
ExecStart&#61;/usr/bin/crond -n -m '/usr/bin/msmtp -t'}}
   
  +
それから cronie あるいは msmtp にメールアドレスを設定してください:
Then you must tell cronie or msmtp what your email address is, either by:
 
  +
  +
*{{ic|/etc/msmtprc}} に追加: {{bc|aliases /etc/aliases}} それから {{ic|/etc/aliases}} を作成: {{bc|your_username: email@address.com}}または:
  +
*crontab の {{ic|MAILTO}} 行に追加: {{bc|MAILTO&#61;email@address.com}}
  +
  +
==パスワード管理==
  +
  +
msmtp のパスワードはプレーンテキスト・暗号化ファイル・キーリングのどれかに保存できます [http://msmtp.sourceforge.net/doc/msmtp.html#Authentication]。
  +
  +
===GNOME Keyring===
  +
  +
[[GNOME Keyring]] にパスワードを保存することができます。Wiki ページに従ってキーリングを設定したら {{Pkg|libsecret}} をインストールしてください。以下のコマンドでパスワードを保存できます:
  +
  +
$ secret-tool store --label=msmtp host ''smtp.your.domain'' service smtp user ''yourusername''
  +
  +
msmtp は自動的にパスワードを確認します。
  +
  +
===GnuPG===
   
  +
{{Ic|password}} ディレクティブは省略できます。省略した場合、アカウントの {{Ic|auth}} を {{Ic|off}} 以外の値に設定することで、インタラクティブシェルから msmtp を起動したときにメールの送信前にパスワードが要求されるようになります。[[Mutt]] などの他のアプリケーションから呼び出されたときは msmtp はパスワードを要求しません。そのようなときは {{ic|--passwordeval}} パラメータを使って [[GnuPG]] などの外部のキーリングツールを使用することができます。
# Add to {{ic|/etc/msmtprc}}: {{bc|aliases /etc/aliases}} and create {{ic|/etc/aliases}}: {{bc|your_username: email@address.com}}&mdash; OR &mdash;.
 
* Add a {{ic|MAILTO}} line to the crontab: {{bc|MAILTO&#61;email@address.com}}
 
   
  +
[[GnuPG]] と [[GnuPG#gpg-agent|gpg-agent]] を設定してパスワードを毎回入力しなくてもよいようにしてください。それから、以下のように msmtp 用に暗号化したパスワードファイルを作成してください。{{ic|700}} パーミッションで [[tmpfs]] 上にディレクトリを作成することで、ディスクに暗号化されていないパスワードが書き込まれないようにすることができます。ディレクトリの中にはメールアカウントのパスワードを記述したプレーンテキストファイルを作成してください。そして、秘密鍵を使ってファイルを暗号化してください:
== Tips and tricks ==
 
   
  +
$ gpg --default-recipient-self -e ''/path/to/plain/password''
=== 実践的なパスワードの管理 ===
 
   
  +
プレーンテキストファイルを削除して、暗号化したファイルを適切なディレクトリに移動してください (例: {{ic|~/.mail/.msmtp-credentials.gpg}})。{{ic|~/.msmtprc}} に以下を追加:
The {{Ic|password}} directive may be omitted. In that case, if the account in question has {{Ic|auth}} set to a legitimate value other than {{Ic|off}}, invoking msmtp from an interactive shell will ask for the password before sending mail. msmtp will not prompt if it has been called by another type of application, such as [[Mutt]].
 
There is a solution for such cases: the {{ic|--passwordeval}} parameter.
 
You can call msmtp to use an external keyring tool like gpg:
 
{{bc|msmtp --passwordeval 'gpg -d mypwfile.gpg'}}
 
If gpg prompt for the passphrase cannot be issued (e.g. when called from Mutt) then start the [[GPG#gpg-agent|gpg-agent]] before.
 
   
  +
{{hc|~/.msmtprc|passwordeval "gpg --quiet --for-your-eyes-only --no-tty --decrypt ~/.mail/.msmtp-credentials.gpg"}}
A simple hack to start the agent is to execute a external command in your muttrc.
 
{{Note| Mutt uses the backtick {{ic| ` command ` }} syntax to execute external commands}}
 
   
  +
[[Mutt]] などからメールを送信するときに GUI のパスワードプロンプトを表示するだけなら上記で十分です。gpg によってパスフレーズのプロンプトが表示されない場合、先に [[GPG#gpg-agent|gpg-agent]] を起動してください。muttrc に {{ic| ` command ` }} のようにバッククォートを使って外部コマンドを指定することでエージェントを起動できます。例:
For example, you can put something like the following in your muttrc
 
   
 
{{hc|muttrc|set my_msmtp_pass&#61;`gpg -d mypwfile.gpg`}}
 
{{hc|muttrc|set my_msmtp_pass&#61;`gpg -d mypwfile.gpg`}}
   
  +
Mutt は起動時にこれを実行し、gpg-agent はあなたのパスワードをキャッシュし、 msmtp は喜んでメールを送れるようになります。
Mutt will execute this when it starts, gpg-agent will cache your password, msmtp will be happy and you can send mail.
 
{{Note| If you do this, you will have to restart mutt after gpg-agent clears the password to start sending emails again}}
 
   
  +
{{Note| この場合、再びメールの受信を開始するには gpg-agent がパスワードをクリアした後 mutt を再起動する必要があります。}}
If you cannot use a keyring tool for any reason, you may want to use the password directly. There is a patched version {{AUR|msmtp-pwpatched}}{{Broken package link|{{aur-mirror|msmtp-pwpatched}}}} in the AUR that provides the {{ic|--password}} parameter. Note that it is a '''huge security flaw''', since any user connected to you machine can see the parameter of any command (in the /proc filesystem for example).
 
   
If this is not desired, an alternative is to place passwords in {{ic|~/.netrc}}, a file that can act as a common pool for msmtp, [[OfflineIMAP]], and associated tools.
+
An alternative is to place passwords in {{ic|~/.netrc}}, a file that can act as a common pool for msmtp, [[OfflineIMAP]], and associated tools.
  +
  +
代替案としては、 msmtp、[[OfflineIMAP]]及び関連ツールで共通して管理できる {{ic|~/.netrc}}にパスワードを置く方法があります。
  +
  +
=== pass ===
  +
  +
認証情報を [[pass]] パスワードマネージャ内に保存することができます。
  +
  +
メインパスワード(通常はpassファイルの1行目に格納)で SMTP サーバにログインする場合は、以下を {{ic|.msmptrc}} に追加します:
  +
  +
{{hc|~/.msmtprc|
  +
passwordeval "pass your_email_password_entry {{!}} head -n1"
  +
}}
  +
  +
[https://www.google.com/gmail/ Gmail] を使っていて[https://myaccount.google.com/apppasswords アプリパスワード]の設定が済んでいるのなら、以下の設定がふさわしいでしょう。アプリパスワードを {{ic|pass}} パスワードファイルに保存しますが、{{ic|msmtp:}} プレフィックスを付けて下さい。
  +
  +
{{hc|your_email_password_entry|
  +
your_main_password
  +
  +
login: your_username
  +
url: the_url_of_your_email
  +
'''msmtp: your_msmtp_app_password'''}}
  +
  +
そして、{{ic|.msmptrc}} に以下を追加して下さい。:
  +
{{hc|~/.msmtprc|
  +
passwordeval "pass your_email_password_entry {{!}} awk '/^msmtp:/ { print $2; }'"
  +
}}
  +
  +
どちらの場合も、 msmtpでメールを送信しようとすると {{ic|pass}} が起動します。最近入力していないのであれば、 {{ic|pass}} のマスターパスワードを求められるかもしれません。
  +
  +
==ヒントとテクニック==
   
 
===msmtp をオフラインで使う===
 
===msmtp をオフラインで使う===
   
  +
msmtp は素晴らしいツールですが、使用するにはオンラインになる必要があります。インターネットに常時接続することができないノートパソコンなどではあまり好ましくありません。この問題を解決するためのスクリプトが複数存在し、まとめて msmtpqueue と呼ばれます。
Although msmtp is great, it requires that you be online to use it. This isn't ideal for people on laptops with intermittent connections to the Internet or dialup users. Several scripts have been written to remedy this fact, collectively called msmtpqueue.
 
   
The scripts are installed under {{ic|/usr/share/doc/msmtp/msmtpqueue}}. You might want to copy the scripts to a convenient location on your computer, ({{ic|/usr/local/bin}} is a good choice).
+
スクリプトは {{ic|/usr/share/doc/msmtp/msmtpqueue}} にインストールされます。スクリプトはどこか便利な場所にコピーすると良いでしょう ({{ic|/usr/local/bin}} を推奨します)
   
Finally, change your MUA to use msmtp-enqueue.sh instead of msmtp when sending e-mail. By default, queued messages will be stored in {{ic|~/.msmtpqueue}}. To change this location, change the {{ic|QUEUEDIR&#61;$HOME/.msmtpqueue}} line in the scripts (or delete the line, and export the QUEUEDIR variable in {{ic|.bash_profile}} like so: {{ic|export QUEUEDIR&#61;"$XDG_DATA_HOME/msmtpqueue"}}).
+
そして MUA でメールを送信するときに msmtp のかわりに msmtp-enqueue.sh を使うようにしてください。デフォルトでは、キューに入ったメッセージは {{ic|~/.msmtpqueue}} に保存されます。保存場所を変更したい場合はスクリプトの {{ic|QUEUEDIR&#61;$HOME/.msmtpqueue}} 行を変更してください (もしくは行を削除して、{{ic|.bash_profile}} QUEUEDIR 変数をエクスポートしてください: {{ic|export QUEUEDIR&#61;"$XDG_DATA_HOME/msmtpqueue"}})
   
  +
作成したメールを送信したいときは以下のコマンドを実行:
When you want to send any mail that you've created and queued up run:
 
 
$ /usr/local/bin/msmtp-runqueue.sh
 
$ /usr/local/bin/msmtp-runqueue.sh
   
  +
{{ic|/usr/local/bin}} を PATH に追加することでキーストロークを少なくすることができます。スクリプトに付属している README ファイルには有用な情報が載っているため、読むことを推奨します。
Adding {{ic|/usr/local/bin}} to your PATH can save you some keystrokes if you're doing it manually. The README file that comes with the scripts has some handy information, reading it is recommended.
 
   
 
===Vim のシンタックスハイライト===
 
===Vim のシンタックスハイライト===
  +
msmtp のソースディストリビューションには [[Vim]] 用の {{ic|msmtprc}} のシンタックスハイライトスクリプトが含まれています。{{ic|/usr/share/vim/vimfiles/syntax/msmtp.vim}} で利用できます。ファイルタイプは自動的に認識されません。[http://vimdoc.sourceforge.net/htmldoc/options.html#modeline modeline] をファイルの一番上あるいは下に追加するのが簡単です:
The msmtp source distribution includes a {{ic|msmtprc}} highlighting script for [[Vim]]. Install it from {{ic|./scripts/vim/msmtp.vim}}.
 
  +
  +
# vim:filetype=msmtp
   
 
===msmtp を使って PHP でメールを送信===
 
===msmtp を使って PHP でメールを送信===
  +
{{ic|php.ini}} の ''sendmail_path'' オプションを以下のように編集してください:
Look for ''sendmail_path'' option in your {{ic|php.ini}} and edit like this:
 
 
{{bc|1=
 
{{bc|1=
 
sendmail_path = "/usr/bin/msmtp -C /path/to/your/config -t"
 
sendmail_path = "/usr/bin/msmtp -C /path/to/your/config -t"
 
}}
 
}}
   
  +
msmtp を php などで sendmail の代替として使う場合、ユーザーの設定ファイル(つまり ~/ 以下にあるもの)は'''使えない'''ことに注意して下さい。
Note that you '''can not''' use a user configuration file (ie: one under ~/) if you plan on using msmtp as a sendmail replacement with php or something similar.
 
  +
その場合は /etc/msmtprc を作成し、ユーザー設定ファイルを削除してください(他の用途で使う場合は削除しないで下さい)。また、使うもの(php、django等)で読み取り可能にしておいて下さい。
In that case just create /etc/msmtprc, and remove your user configuration (or not if you plan on using it for something else). Also make sure it's readable by whatever you're using it with (php, django, etc...)
 
   
  +
msmtp マニュアルより: ''ユーザー設定ファイルで定義されたアカウントはシステム設定ファイル'により上書きされます。ユーザー設定ファイルには、ユーザーが読み書きできる以上の権限を与えてはいけません。'
From the msmtp manual: ''Accounts defined in the user configuration file override accounts from the system configuration file. The user configuration file must have no more permissions than user read/write''
 
   
  +
そのため、conf ファイルを ~/ 以下 に置き、php ユーザーが読めるようにすることは不可能です。
So it's impossible to have a conf file under ~/ and have it still be readable by the php user.
 
   
  +
テストするにはこのファイルを php が有効なサーバが置くか、php-cli を使って下さい。
To test it place this file in your php enabled server or using php-cli.
 
 
{{bc|
 
{{bc|
 
<?php
 
<?php
173行目: 258行目:
 
?>
 
?>
 
}}
 
}}
  +
  +
/etc/msmtprc のパーミションをユーザーが読み書きできる(600)ようにしない限り、{{ic|php-fpm}} はメール送信に失敗し、以下の警告をログに出すでしょう: {{ic|PHP Warning: mail(mail.log): failed to open stream}}
   
 
==トラブルシューティング==
 
==トラブルシューティング==
 
===TLS の問題===
 
===TLS の問題===
 
以下のエラーメッセージが表示される場合:
 
以下のエラーメッセージが表示される場合:
msmtp: TLS certificate verification failed: the certificate hasn't got a known issuer
 
おそらく tls_trust_file が正しくありません。
 
   
  +
msmtp: TLS certificate verification failed: the certificate hasn't got a known issuer
Just follow the [http://msmtp.sourceforge.net/doc/msmtp.html#Transport-Layer-Security fine manual]. It explains you how to find out the server certificate issuer of a given smtp server. Then you can explore the {{ic|/usr/share/ca-certificates/}} directory to find out if by any chance, the certificate you need is there. If not, you will have to get the certificate on your own. If you are using your own certificate, you can make msmtp trust it by adding the following to your {{ic|~/.msmtprc}}:
 
  +
  +
msmtp: TLS certificate verification failed: The certificate is NOT trusted. The certificate issuer is unknown.
  +
  +
おそらく設定ファイルの {{ic|tls_trust_file}} パラメータが正しくありません。
  +
  +
[https://marlam.de/msmtp/msmtp.html#Transport-Layer-Security 素晴らしいマニュアル]に従って下さい。指定した SMTP サーバの証明書発行者を調べる方法が説明されています。そして、 {{ic|/usr/share/ca-certificates/}} ディレクトリを探索して、必要な証明書がひょっとしてそこにあるか調べることができます。ない場合は、自分で証明書を取得する必要があります。独自の証明書を使用している場合は、以下を {{ic|~/.msmtprc}}に追加することで msmtp にその証明書を信頼させることができます。
   
 
tls_fingerprint <SHA1 (recommended) or MD5 fingerprint of the certificate>
 
tls_fingerprint <SHA1 (recommended) or MD5 fingerprint of the certificate>
   
  +
ユーザーがサーバで使用されている証明書を [https://en.wikipedia.org/wiki/Privacy-Enhanced_Mail PEM] ファイルで持っている場合は、 {{ic|openssl}} を使うことによって保存されている証明書のフィンガープリントを見つけることができます:
If you are trying to send mail through GMail and are receiving this error, have a look at [http://www.mail-archive.com/msmtp-users@lists.sourceforge.net/msg00141.html this] thread or just use the second GMail example above.
 
  +
  +
openssl x509 -in ''CERT.pem'' -inform PEM -fingerprint
  +
  +
Gmail でメールを送信しようとしてこのエラーを受け取った場合、[https://www.mail-archive.com/msmtp-users@lists.sourceforge.net/msg00141.html この]スレッドを読むか、上記二番目の Gmail の例を使って下さい。
  +
  +
本当に困っているが、100% 正しいサーバに接続していると確信している場合は、いつでも証明書チェックを一時的に無効化することができます:
   
If you are completely desperate, but are 100% sure you are communicating with the right server, you can always temporarily disable the cert check:
 
 
$ msmtp --tls-certcheck off
 
$ msmtp --tls-certcheck off
   
  +
以下のメッセージが表示される場合:
If you see the following message:
 
 
msmtp: TLS handshake failed: the operation timed out
 
msmtp: TLS handshake failed: the operation timed out
  +
{{Bug|44994}} のバグが原因です。{{ic|1=--with-ssl=openssl}} を使って msmtp を再コンパイルしてください (デフォルトでは GnuTLS が有効になっています)。
You may be affected by this [https://bugs.archlinux.org/task/44994 bug]. Recompile with "--with-ssl=openssl" (msmtp is compiled with GnuTLS by default).
 
   
 
===Server sent empty reply===
 
===Server sent empty reply===
"server sent empty reply" エラーが表示される場合、以下{{ic|~/.msmtprc}} に追加してください:
+
"server sent empty reply" エラーが表示される場合、メールサーバーがポート 587 で STARTTLS 許可しておらず、非標準のポート 465 での SSL/TLS を必要としています [https://www.fastmail.com/help/technical/ssltlsstarttls.html]。
  +
  +
msmtp でポート 465 の SSL/TLS を使うには、以下の行を {{ic|~/.msmtprc}} に追加してください:
   
 
tls_starttls off
 
tls_starttls off
   
  +
==== Zoho SMTP サーバ ====
This allows msmtp to use SSL/TLS (port 465) in place of STARTTLS (port 587) [https://www.fastmail.com/help/technical/ssltlsstarttls.html].
 
  +
  +
Zoho SMTP サーバでも、メールヘッダと本文の間に空行な無い場合発生する場合があります(Debianのバグ [https://www.mail-archive.com/debian-bugs-dist@lists.debian.org/msg1650762.html #917260] を参照)。この解決法としては、間に余分なスペースを追加して下さい:
  +
  +
"test-header\n\ntest-body"
   
 
===GSSAPI の問題===
 
===GSSAPI の問題===
   
  +
以下のエラーが表示される場合:
If you get the following error
 
   
 
GNU SASL: GSSAPI error in client while negotiating security context in gss_init_sec_context() in SASL library. This is most likely due insufficient credentials or malicious interactions.
 
GNU SASL: GSSAPI error in client while negotiating security context in gss_init_sec_context() in SASL library. This is most likely due insufficient credentials or malicious interactions.
   
Try changing your auth setting to plain, instead of gssapi in your .msmtprc file [https://bbs.archlinux.org/viewtopic.php?id=138727]:
+
.msmtprc ファイルの auth 設定を gssapi から plain に変更してみてください [https://bbs.archlinux.org/viewtopic.php?id=138727]:
   
 
auth plain
 
auth plain
  +
  +
=== Envelope not accepted ===
  +
  +
以下のエラーが表示される場合:
  +
  +
msmtp: envelope from address mail@server not accepted by the server
  +
msmtp: server message: 530 5.5.1 Authentication Required.
  +
msmtp: could not send mail (account default from /etc/msmtprc)
  +
  +
以下で認証の有効化を試みるか、
  +
  +
auth on
  +
  +
他の方法で認証を有効化して下さい。

2025年6月6日 (金) 16:54時点における最新版

関連記事

msmtp はとてもシンプルで使いやすい SMTP クライアントです。sendmail と完全な互換性があります。

インストール

msmtp パッケージでインストールできます。さらに msmtp-mta をインストールすることで msmtp に sendmail エイリアスが作成されます。

基本設定

msmtp バージョン 1.8.6 以降、設定ファイルを ~/.msmtprc$XDG_CONFIG_HOME/msmtp/config に配置することができます。以下は msmtp の設定例です (パッケージに含まれている、/usr/share/doc/msmtp/msmtprc-user.example にある通常ユーザーのサンプルが基になっています。システム設定ファイルは /etc/msmtprc にあり、サンプルは /usr/share/doc/msmtp/msmtprc-system.example にあります):

ヒント: Gmail を使う場合は、以下のどちらかが必要です:
~/.msmtprc
# Set default values for all following accounts.
defaults
auth           on
tls            on
tls_trust_file /etc/ssl/certs/ca-certificates.crt
logfile        ~/.msmtp.log

# Gmail
account        gmail
host           smtp.gmail.com
port           465
from           username@gmail.com
user           username
password       plain-text-password

# A freemail service
account        freemail
host           smtp.freemail.example
from           joe_smith@freemail.example
...

# Accounts can inherit info from another account
account        postmasterfreemail : freemail
from           postmaster*@freemail.example

# Set a default account
account default : gmail
ノート: SSL/TLS を使っていて "Server sent empty reply" エラーメッセージが表示される場合、#Server sent empty reply を見て下さい。

ユーザーの設定ファイルは所有者が読み書きできるように明示的に設定しないと msmtp がエラーを吐きます:

$ chmod 600 ~/.msmtprc

設定ファイルに平文でパスワードを保存したくない場合、passwordeval を使って外部プログラムを起動するか、あるいは下のパスワード管理セクションを見てください。以下は GnuPG を使ってパスワードを暗号化する例です:

$ echo -e "password\n" | gpg --encrypt -o .msmtp-gmail.gpg # enter id (email...)
警告: ほとんどのシェルはコマンド履歴を保存します (例: .bash_history.zhistory)。GPG でシェルの標準入力を使いたくない場合 gpg --encrypt -o .msmtp-gmail.gpg -r <email> - コマンドを使ってください。最後のダッシュは打ち間違えではなく標準入力を使うために必要な文字です。コマンドを実行したら、パスワードを入力してから Control-d を押せば GPG によってパスワードが暗号化されます。
~/.msmtprc
passwordeval    "gpg --quiet --for-your-eyes-only --no-tty --decrypt ~/.msmtp-gmail.gpg"

OAuth2 設定

OAuth2 は、基本的なユーザー名/パスワード認証がサイト構成によってサポートされていない場合、または他の点で望ましくない場合に、msmtp を安全に認証するために使用できます。

ノート: この投稿 では、現在 Gmail に許可されている極めて制限されている機能について説明しています。

oama

msmtp だけでは OAuth2 資格情報を更新・認証することができません。包括的な解決方法は、oama ユーティリティを使うことです。これは IMAP/SMTP クライアントに OAuth2 資格情報の更新能力と認証を提供します。

oama を使うには、oama-binAURインストール し、 msmtp を設定して使用できるようにします。


   # account at Google with oauth2 access
   account YOUR_EMAIL_NAME@gmail.com
   from YOUR_EMAIL_NAME@gmail.com
   user YOUR_EMAIL_NAME@gmail.com
   auth oauthbearer
   passwordeval oama access YOUR_EMAIL_NAME@gmail.com
   host smtp.gmail.com
   port 587
   tls on
   tls_trust_file /etc/ssl/certs/ca-certificates.crt

アクセストークン更新は、ユーザーには見えないバックグラウンドで自動的に行われます。

mail コマンドを使う

mail コマンドを使ってメールを送信するには s-nail パッケージをインストールする必要があります。これは、 mailx コマンドも提供します。それに加えて sendmail 互換の MTA が必要です。msmtp-mta をインストールするか(これは {{ic|sendmail} を msmtp へシンボリックリンクします) /etc/mail.rc を編集して sendmail クライアントを設定してください:

/etc/mail.rc
set mta=/usr/bin/msmtp

システム共通の /etc/msmtprc ファイルを使用するか、.msmtprc ファイルをメールを送信したい全てのユーザーのホームディレクトリに配置する必要があります。

msmtp ではエイリアスが使えます。以下の行を msmtprc のデフォルトセクションかローカルの設定ファイルに追加してください:

/etc/msmtprc
aliases               /etc/aliases

そして /etc にエイリアスファイルを作成してください:

/etc/aliases
# Example aliases file

# Send root to Joe and Jane
root: joe_smith@example.com, jane_chang@example.com

# Send everything else to admin
default: admin@domain.example

動作のテスト

アカウントオプション (--account=,-a) で送信元として使用するアカウントを指定します:

$ echo "hello there username." | msmtp -a default username@domain.com

もしくは、件名と本文の両方を送信するには:

$ printf "Subject: Test\n\nhello there username." | msmtp -a default username@domain.com

もしくは、ファイルにアドレスを書いて使用するには:

To: username@domain.com
From: username@gmail.com
Subject: A test
Hello there.
$ cat test.mail | msmtp -a default <username>@domain.com
ヒント: -a default の代わりに --read-envelope-from を使うことで送信するメッセージの From: に使われるアカウントが自動的に選択されます。また、 --read-recipients,-t を使うことで、コマンドラインで指定した受信者に加えてメールの To、CC、Bcc ヘッダーから受信者のアドレスを読み込むことができます。

Cronie のデフォルトメールクライアント

この記事またはセクションは情報が古くなっています。
理由: Arch は cronie の代わりに systemd/タイマー を使います。 (Discuss)

cronie で sendmail ではなく msmtp を使うようにするには、msmtp-mta をインストールするか、cronie.service systemd ユニットを編集してください:

/etc/systemd/system/cronie.service.d/msmtp.conf
[Service]
ExecStart=
ExecStart=/usr/bin/crond -n -m '/usr/bin/msmtp -t'

それから cronie あるいは msmtp にメールアドレスを設定してください:

  • /etc/msmtprc に追加:
    aliases /etc/aliases
    それから /etc/aliases を作成:
    your_username: email@address.com
    または:
  • crontab の MAILTO 行に追加:
    MAILTO=email@address.com

パスワード管理

msmtp のパスワードはプレーンテキスト・暗号化ファイル・キーリングのどれかに保存できます [1]

GNOME Keyring

GNOME Keyring にパスワードを保存することができます。Wiki ページに従ってキーリングを設定したら libsecret をインストールしてください。以下のコマンドでパスワードを保存できます:

$ secret-tool store --label=msmtp host smtp.your.domain service smtp user yourusername

msmtp は自動的にパスワードを確認します。

GnuPG

password ディレクティブは省略できます。省略した場合、アカウントの authoff 以外の値に設定することで、インタラクティブシェルから msmtp を起動したときにメールの送信前にパスワードが要求されるようになります。Mutt などの他のアプリケーションから呼び出されたときは msmtp はパスワードを要求しません。そのようなときは --passwordeval パラメータを使って GnuPG などの外部のキーリングツールを使用することができます。

GnuPGgpg-agent を設定してパスワードを毎回入力しなくてもよいようにしてください。それから、以下のように msmtp 用に暗号化したパスワードファイルを作成してください。700 パーミッションで tmpfs 上にディレクトリを作成することで、ディスクに暗号化されていないパスワードが書き込まれないようにすることができます。ディレクトリの中にはメールアカウントのパスワードを記述したプレーンテキストファイルを作成してください。そして、秘密鍵を使ってファイルを暗号化してください:

$ gpg --default-recipient-self -e /path/to/plain/password

プレーンテキストファイルを削除して、暗号化したファイルを適切なディレクトリに移動してください (例: ~/.mail/.msmtp-credentials.gpg)。~/.msmtprc に以下を追加:

~/.msmtprc
passwordeval  "gpg --quiet --for-your-eyes-only --no-tty --decrypt ~/.mail/.msmtp-credentials.gpg"

Mutt などからメールを送信するときに GUI のパスワードプロンプトを表示するだけなら上記で十分です。gpg によってパスフレーズのプロンプトが表示されない場合、先に gpg-agent を起動してください。muttrc に ` command ` のようにバッククォートを使って外部コマンドを指定することでエージェントを起動できます。例:

muttrc
set my_msmtp_pass=`gpg -d mypwfile.gpg`

Mutt は起動時にこれを実行し、gpg-agent はあなたのパスワードをキャッシュし、 msmtp は喜んでメールを送れるようになります。

ノート: この場合、再びメールの受信を開始するには gpg-agent がパスワードをクリアした後 mutt を再起動する必要があります。

An alternative is to place passwords in ~/.netrc, a file that can act as a common pool for msmtp, OfflineIMAP, and associated tools.

代替案としては、 msmtp、OfflineIMAP及び関連ツールで共通して管理できる ~/.netrcにパスワードを置く方法があります。

pass

認証情報を pass パスワードマネージャ内に保存することができます。

メインパスワード(通常はpassファイルの1行目に格納)で SMTP サーバにログインする場合は、以下を .msmptrc に追加します:

~/.msmtprc
passwordeval   "pass your_email_password_entry | head -n1"

Gmail を使っていてアプリパスワードの設定が済んでいるのなら、以下の設定がふさわしいでしょう。アプリパスワードを pass パスワードファイルに保存しますが、msmtp: プレフィックスを付けて下さい。

your_email_password_entry
your_main_password

login: your_username
url: the_url_of_your_email
msmtp: your_msmtp_app_password

そして、.msmptrc に以下を追加して下さい。:

~/.msmtprc
passwordeval   "pass your_email_password_entry | awk '/^msmtp:/ { print $2; }'"

どちらの場合も、 msmtpでメールを送信しようとすると pass が起動します。最近入力していないのであれば、 pass のマスターパスワードを求められるかもしれません。

ヒントとテクニック

msmtp をオフラインで使う

msmtp は素晴らしいツールですが、使用するにはオンラインになる必要があります。インターネットに常時接続することができないノートパソコンなどではあまり好ましくありません。この問題を解決するためのスクリプトが複数存在し、まとめて msmtpqueue と呼ばれます。

スクリプトは /usr/share/doc/msmtp/msmtpqueue にインストールされます。スクリプトはどこか便利な場所にコピーすると良いでしょう (/usr/local/bin を推奨します)。

そして MUA でメールを送信するときに msmtp のかわりに msmtp-enqueue.sh を使うようにしてください。デフォルトでは、キューに入ったメッセージは ~/.msmtpqueue に保存されます。保存場所を変更したい場合はスクリプトの QUEUEDIR=$HOME/.msmtpqueue 行を変更してください (もしくは行を削除して、.bash_profile で QUEUEDIR 変数をエクスポートしてください: export QUEUEDIR="$XDG_DATA_HOME/msmtpqueue")。

作成したメールを送信したいときは以下のコマンドを実行:

$ /usr/local/bin/msmtp-runqueue.sh

/usr/local/bin を PATH に追加することでキーストロークを少なくすることができます。スクリプトに付属している README ファイルには有用な情報が載っているため、読むことを推奨します。

Vim のシンタックスハイライト

msmtp のソースディストリビューションには Vim 用の msmtprc のシンタックスハイライトスクリプトが含まれています。/usr/share/vim/vimfiles/syntax/msmtp.vim で利用できます。ファイルタイプは自動的に認識されません。modeline をファイルの一番上あるいは下に追加するのが簡単です:

# vim:filetype=msmtp

msmtp を使って PHP でメールを送信

php.inisendmail_path オプションを以下のように編集してください:

sendmail_path = "/usr/bin/msmtp -C /path/to/your/config -t"

msmtp を php などで sendmail の代替として使う場合、ユーザーの設定ファイル(つまり ~/ 以下にあるもの)は使えないことに注意して下さい。 その場合は /etc/msmtprc を作成し、ユーザー設定ファイルを削除してください(他の用途で使う場合は削除しないで下さい)。また、使うもの(php、django等)で読み取り可能にしておいて下さい。

msmtp マニュアルより: ユーザー設定ファイルで定義されたアカウントはシステム設定ファイル'により上書きされます。ユーザー設定ファイルには、ユーザーが読み書きできる以上の権限を与えてはいけません。'

そのため、conf ファイルを ~/ 以下 に置き、php ユーザーが読めるようにすることは不可能です。

テストするにはこのファイルを php が有効なサーバが置くか、php-cli を使って下さい。

<?php
mail("your@email.com", "Test email from PHP", "msmtp as sendmail for PHP");
?>

/etc/msmtprc のパーミションをユーザーが読み書きできる(600)ようにしない限り、php-fpm はメール送信に失敗し、以下の警告をログに出すでしょう: PHP Warning: mail(mail.log): failed to open stream

トラブルシューティング

TLS の問題

以下のエラーメッセージが表示される場合:

msmtp: TLS certificate verification failed: the certificate hasn't got a known issuer
msmtp: TLS certificate verification failed: The certificate is NOT trusted. The certificate issuer is unknown.

おそらく設定ファイルの tls_trust_file パラメータが正しくありません。

素晴らしいマニュアルに従って下さい。指定した SMTP サーバの証明書発行者を調べる方法が説明されています。そして、 /usr/share/ca-certificates/ ディレクトリを探索して、必要な証明書がひょっとしてそこにあるか調べることができます。ない場合は、自分で証明書を取得する必要があります。独自の証明書を使用している場合は、以下を ~/.msmtprcに追加することで msmtp にその証明書を信頼させることができます。

tls_fingerprint <SHA1 (recommended) or MD5 fingerprint of the certificate>

ユーザーがサーバで使用されている証明書を PEM ファイルで持っている場合は、 openssl を使うことによって保存されている証明書のフィンガープリントを見つけることができます:

openssl x509 -in CERT.pem -inform PEM -fingerprint

Gmail でメールを送信しようとしてこのエラーを受け取った場合、このスレッドを読むか、上記二番目の Gmail の例を使って下さい。

本当に困っているが、100% 正しいサーバに接続していると確信している場合は、いつでも証明書チェックを一時的に無効化することができます:

$ msmtp --tls-certcheck off

以下のメッセージが表示される場合:

 msmtp: TLS handshake failed: the operation timed out

FS#44994 のバグが原因です。--with-ssl=openssl を使って msmtp を再コンパイルしてください (デフォルトでは GnuTLS が有効になっています)。

Server sent empty reply

"server sent empty reply" エラーが表示される場合、メールサーバーがポート 587 での STARTTLS を許可しておらず、非標準のポート 465 での SSL/TLS を必要としています [2]

msmtp でポート 465 の SSL/TLS を使うには、以下の行を ~/.msmtprc に追加してください:

tls_starttls off

Zoho SMTP サーバ

Zoho SMTP サーバでも、メールヘッダと本文の間に空行な無い場合発生する場合があります(Debianのバグ #917260 を参照)。この解決法としては、間に余分なスペースを追加して下さい:

"test-header\n\ntest-body"

GSSAPI の問題

以下のエラーが表示される場合:

GNU SASL: GSSAPI error in client while negotiating security context in gss_init_sec_context() in SASL library.  This is most likely due insufficient credentials or malicious interactions.

.msmtprc ファイルの auth 設定を gssapi から plain に変更してみてください [3]:

auth plain

Envelope not accepted

以下のエラーが表示される場合:

msmtp: envelope from address mail@server not accepted by the server
msmtp: server message: 530 5.5.1 Authentication Required.
msmtp: could not send mail (account default from /etc/msmtprc)

以下で認証の有効化を試みるか、

auth on

他の方法で認証を有効化して下さい。