「Msmtp」の版間の差分
Kusakata.bot (トーク | 投稿記録) 細 (文字列「http://www.mail-archive.com/」を「https://www.mail-archive.com/」に置換) |
(英語版の最新の内容に更新) |
||
| (3人の利用者による、間の8版が非表示) | |||
| 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 クライアントです。[[ |
+ | [http://msmtp.sourceforge.net/ msmtp] はとてもシンプルで使いやすい SMTP クライアントです。[[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 |
+ | 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 |
||
| 60行目: | 72行目: | ||
}} |
}} |
||
| + | === OAuth2 設定 === |
||
| − | == mail コマンドを使う == |
||
| + | [[Wikipedia:OAuth2|OAuth2]] は、基本的なユーザー名/パスワード認証がサイト構成によってサポートされていない場合、または他の点で望ましくない場合に、msmtp を安全に認証するために使用できます。 |
||
| − | {{ic|mail}} コマンドを使ってメールを送信するには {{Pkg|s-nail}} パッケージをインストールする必要があります。それに加えて {{ic|sendmail}} 互換の MTA が必要です。{{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 ==== |
||
| + | |||
| + | msmtp だけでは OAuth2 資格情報を更新・認証することができません。包括的な解決方法は、[https://github.com/pdobsan/oama oama] ユーティリティを使うことです。これは IMAP/SMTP クライアントに OAuth2 資格情報の更新能力と認証を提供します。 |
||
| + | |||
| + | 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}} ファイルをメールを送信したい全てのユーザーのホームディレクトリに配置する必要があります。 |
システム共通の {{ic|/etc/msmtprc}} ファイルを使用するか、{{ic|.msmtprc}} ファイルをメールを送信したい全てのユーザーのホームディレクトリに配置する必要があります。 |
||
| 75行目: | 113行目: | ||
{{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 |
||
もしくは、ファイルにアドレスを書いて使用するには: |
もしくは、ファイルにアドレスを書いて使用するには: |
||
| 93行目: | 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|Gmail を使用する場合、以下のどちらかの設定が必要です: |
||
| + | |||
| − | * ''Settings'' > ''Security'' で "Less Secure Apps" を許可する。他の Gmail アカウントはサインアウトする必要があります。Google アカウントのセキュリティ設定では複数のアカウントの設定を同時に管理することはできません。 |
||
| + | ==Cronie のデフォルトメールクライアント== |
||
| − | * 2段階認証を有効にしてアプリパスワードを作成する。 |
||
| − | }} |
||
| − | {{Tip|''-a default'' の代わりに ''--read-envelope-from'' を使うことで送信するメッセージの ''From:'' に使われるアカウントが自動的に選択されます。}} |
||
| + | {{Out of date|Arch は cronie の代わりに [[systemd/タイマー]] を使います。}} |
||
| − | == Cronie のデフォルトメールクライアント == |
||
{{Pkg|cronie}} で sendmail ではなく msmtp を使うようにするには、{{Pkg|msmtp-mta}} をインストールするか、{{ic|cronie.service}} systemd ユニットを編集してください: |
{{Pkg|cronie}} で sendmail ではなく msmtp を使うようにするには、{{Pkg|msmtp-mta}} をインストールするか、{{ic|cronie.service}} systemd ユニットを編集してください: |
||
| 114行目: | 154行目: | ||
それから cronie あるいは msmtp にメールアドレスを設定してください: |
それから cronie あるいは msmtp にメールアドレスを設定してください: |
||
| − | * |
+ | *{{ic|/etc/msmtprc}} に追加: {{bc|aliases /etc/aliases}} それから {{ic|/etc/aliases}} を作成: {{bc|your_username: email@address.com}}または: |
| − | * |
+ | *crontab の {{ic|MAILTO}} 行に追加: {{bc|MAILTO=email@address.com}} |
| − | == |
+ | ==パスワード管理== |
msmtp のパスワードはプレーンテキスト・暗号化ファイル・キーリングのどれかに保存できます [http://msmtp.sourceforge.net/doc/msmtp.html#Authentication]。 |
msmtp のパスワードはプレーンテキスト・暗号化ファイル・キーリングのどれかに保存できます [http://msmtp.sourceforge.net/doc/msmtp.html#Authentication]。 |
||
| − | === |
+ | ===GNOME Keyring=== |
[[GNOME Keyring]] にパスワードを保存することができます。Wiki ページに従ってキーリングを設定したら {{Pkg|libsecret}} をインストールしてください。以下のコマンドでパスワードを保存できます: |
[[GNOME Keyring]] にパスワードを保存することができます。Wiki ページに従ってキーリングを設定したら {{Pkg|libsecret}} をインストールしてください。以下のコマンドでパスワードを保存できます: |
||
| 129行目: | 169行目: | ||
msmtp は自動的にパスワードを確認します。 |
msmtp は自動的にパスワードを確認します。 |
||
| − | === |
+ | ===GnuPG=== |
| + | {{Ic|password}} ディレクティブは省略できます。省略した場合、アカウントの {{Ic|auth}} を {{Ic|off}} 以外の値に設定することで、インタラクティブシェルから msmtp を起動したときにメールの送信前にパスワードが要求されるようになります。[[Mutt]] などの他のアプリケーションから呼び出されたときは msmtp はパスワードを要求しません。そのようなときは {{ic|--passwordeval}} パラメータを使って [[GnuPG]] などの外部のキーリングツールを使用することができます。 |
||
| − | 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]]. For such cases, the {{ic|--passwordeval}} parameter |
||
| − | can be used to call an external keyring tool like [[GnuPG]]. |
||
| + | [[GnuPG]] と [[GnuPG#gpg-agent|gpg-agent]] を設定してパスワードを毎回入力しなくてもよいようにしてください。それから、以下のように msmtp 用に暗号化したパスワードファイルを作成してください。{{ic|700}} パーミッションで [[tmpfs]] 上にディレクトリを作成することで、ディスクに暗号化されていないパスワードが書き込まれないようにすることができます。ディレクトリの中にはメールアカウントのパスワードを記述したプレーンテキストファイルを作成してください。そして、秘密鍵を使ってファイルを暗号化してください: |
||
| − | To do this, set up [[GnuPG]], including [[GnuPG#gpg-agent|gpg-agent]] to avoid having to enter the password every time. Then, create an encrypted password file for msmtp, as follows. Create a secure directory with {{ic|700}} permissions located on a [[tmpfs]] to avoid writing the unencrypted password to the disk. In that directory create a plain text file with the mail account password. Then, encrypt the file with your private key: |
||
$ gpg --default-recipient-self -e ''/path/to/plain/password'' |
$ gpg --default-recipient-self -e ''/path/to/plain/password'' |
||
| + | プレーンテキストファイルを削除して、暗号化したファイルを適切なディレクトリに移動してください (例: {{ic|~/.mail/.msmtp-credentials.gpg}})。{{ic|~/.msmtprc}} に以下を追加: |
||
| − | Remove the plain text file and move the encrypted file to the final location, e.g. {{ic|~/.mail/.msmtp-credentials.gpg}}. In {{ic|~/.msmtprc}} add: |
||
{{hc|~/.msmtprc|passwordeval "gpg --quiet --for-your-eyes-only --no-tty --decrypt ~/.mail/.msmtp-credentials.gpg"}} |
{{hc|~/.msmtprc|passwordeval "gpg --quiet --for-your-eyes-only --no-tty --decrypt ~/.mail/.msmtp-credentials.gpg"}} |
||
| + | [[Mutt]] などからメールを送信するときに GUI のパスワードプロンプトを表示するだけなら上記で十分です。gpg によってパスフレーズのプロンプトが表示されない場合、先に [[GPG#gpg-agent|gpg-agent]] を起動してください。muttrc に {{ic| ` command ` }} のようにバッククォートを使って外部コマンドを指定することでエージェントを起動できます。例: |
||
| − | Normally this is sufficient for a GUI password prompt to appear when, for example, sending a message from [[Mutt]]. If gpg prompt for the passphrase cannot be issued, then start the [[GPG#gpg-agent|gpg-agent]] before. A simple hack to start the agent is to execute a external command in your muttrc using the backtick {{ic| ` command ` }} syntax. For example, you can put something like the following in your muttrc |
||
{{hc|muttrc|set my_msmtp_pass=`gpg -d mypwfile.gpg`}} |
{{hc|muttrc|set my_msmtp_pass=`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). |
||
| − | + | 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. |
||
| − | + | スクリプトは {{ic|/usr/share/doc/msmtp/msmtpqueue}} にインストールされます。スクリプトはどこか便利な場所にコピーすると良いでしょう ({{ic|/usr/local/bin}} を推奨します)。 |
|
| − | + | そして MUA でメールを送信するときに msmtp のかわりに msmtp-enqueue.sh を使うようにしてください。デフォルトでは、キューに入ったメッセージは {{ic|~/.msmtpqueue}} に保存されます。保存場所を変更したい場合はスクリプトの {{ic|QUEUEDIR=$HOME/.msmtpqueue}} 行を変更してください (もしくは行を削除して、{{ic|.bash_profile}} で QUEUEDIR 変数をエクスポートしてください: {{ic|export QUEUEDIR="$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] をファイルの一番上に追加するのが簡単です: |
+ | msmtp のソースディストリビューションには [[Vim]] 用の {{ic|msmtprc}} のシンタックスハイライトスクリプトが含まれています。{{ic|/usr/share/vim/vimfiles/syntax/msmtp.vim}} で利用できます。ファイルタイプは自動的に認識されません。[http://vimdoc.sourceforge.net/htmldoc/options.html#modeline modeline] をファイルの一番上あるいは下に追加するのが簡単です: |
# vim:filetype=msmtp |
# vim:filetype=msmtp |
||
| 179行目: | 245行目: | ||
}} |
}} |
||
| + | 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 |
||
| 192行目: | 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 |
msmtp: TLS certificate verification failed: the certificate hasn't got a known issuer |
||
| − | おそらく tls_trust_file が正しくありません。 |
||
| + | msmtp: TLS certificate verification failed: The certificate is NOT trusted. The certificate issuer is unknown. |
||
| − | 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}}: |
||
| + | |||
| + | おそらく設定ファイルの {{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 [https://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 |
||
| 213行目: | 290行目: | ||
===Server sent empty reply=== |
===Server sent empty reply=== |
||
| − | "server sent empty reply" エラーが表示される場合、 |
+ | "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 サーバ ==== |
||
| − | 上記の設定で msmtp で STARTTLS (ポート 587) のかわりに SSL/TLS (ポート 465) が使われるようになります [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 の問題=== |
||
| 228行目: | 311行目: | ||
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 にあります):
~/.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
ユーザーの設定ファイルは所有者が読み書きできるように明示的に設定しないと msmtp がエラーを吐きます:
$ chmod 600 ~/.msmtprc
設定ファイルに平文でパスワードを保存したくない場合、passwordeval を使って外部プログラムを起動するか、あるいは下のパスワード管理セクションを見てください。以下は GnuPG を使ってパスワードを暗号化する例です:
$ echo -e "password\n" | gpg --encrypt -o .msmtp-gmail.gpg # enter id (email...)
~/.msmtprc
passwordeval "gpg --quiet --for-your-eyes-only --no-tty --decrypt ~/.msmtp-gmail.gpg"
OAuth2 設定
OAuth2 は、基本的なユーザー名/パスワード認証がサイト構成によってサポートされていない場合、または他の点で望ましくない場合に、msmtp を安全に認証するために使用できます。
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
Cronie のデフォルトメールクライアント
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 ディレクティブは省略できます。省略した場合、アカウントの auth を off 以外の値に設定することで、インタラクティブシェルから msmtp を起動したときにメールの送信前にパスワードが要求されるようになります。Mutt などの他のアプリケーションから呼び出されたときは msmtp はパスワードを要求しません。そのようなときは --passwordeval パラメータを使って GnuPG などの外部のキーリングツールを使用することができます。
GnuPG と gpg-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 は喜んでメールを送れるようになります。
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.ini の sendmail_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
他の方法で認証を有効化して下さい。