「Msmtp」の版間の差分

提供: ArchWiki
ナビゲーションに移動 検索に移動
(→‎msmtp を使って PHP でメールを送信: add ''Using XOAUTH2 Authentication for Gmail")
(英語版の最新の内容に更新)
 
(3人の利用者による、間の6版が非表示)
1行目: 1行目:
 
{{Lowercase title}}
 
{{Lowercase title}}
 
[[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]] と完全な互換性があります。
   
== インストール ==
+
==インストール==
   
 
{{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|
28行目: 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''
38行目: 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
59行目: 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}} ファイルをメールを送信したい全てのユーザーのホームディレクトリに配置する必要があります。
74行目: 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
   
 
もしくは、ファイルにアドレスを書いて使用するには:
 
もしくは、ファイルにアドレスを書いて使用するには:
92行目: 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 ユニットを編集してください:
113行目: 154行目:
 
それから cronie あるいは msmtp にメールアドレスを設定してください:
 
それから cronie あるいは msmtp にメールアドレスを設定してください:
   
* {{ic|/etc/msmtprc}} に追加: {{bc|aliases /etc/aliases}} それから {{ic|/etc/aliases}} を作成: {{bc|your_username: email@address.com}}
+
*{{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}}
+
*crontab の {{ic|MAILTO}} 行に追加: {{bc|MAILTO&#61;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===
   
 
[[GNOME Keyring]] にパスワードを保存することができます。Wiki ページに従ってキーリングを設定したら {{Pkg|libsecret}} をインストールしてください。以下のコマンドでパスワードを保存できます:
 
[[GNOME Keyring]] にパスワードを保存することができます。Wiki ページに従ってキーリングを設定したら {{Pkg|libsecret}} をインストールしてください。以下のコマンドでパスワードを保存できます:
128行目: 169行目:
 
msmtp は自動的にパスワードを確認します。
 
msmtp は自動的にパスワードを確認します。
   
=== GnuPG ===
+
===GnuPG===
   
 
{{Ic|password}} ディレクティブは省略できます。省略した場合、アカウントの {{Ic|auth}} を {{Ic|off}} 以外の値に設定することで、インタラクティブシェルから msmtp を起動したときにメールの送信前にパスワードが要求されるようになります。[[Mutt]] などの他のアプリケーションから呼び出されたときは msmtp はパスワードを要求しません。そのようなときは {{ic|--passwordeval}} パラメータを使って [[GnuPG]] などの外部のキーリングツールを使用することができます。
 
{{Ic|password}} ディレクティブは省略できます。省略した場合、アカウントの {{Ic|auth}} を {{Ic|off}} 以外の値に設定することで、インタラクティブシェルから msmtp を起動したときにメールの送信前にパスワードが要求されるようになります。[[Mutt]] などの他のアプリケーションから呼び出されたときは msmtp はパスワードを要求しません。そのようなときは {{ic|--passwordeval}} パラメータを使って [[GnuPG]] などの外部のキーリングツールを使用することができます。
144行目: 185行目:
 
{{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| この場合、再びメールの受信を開始するには gpg-agent がパスワードをクリアした後 mutt を再起動する必要があります。}}
{{Note| If you do this, you will have to restart mutt after gpg-agent clears the password to start sending emails again}}
 
   
 
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 をオフラインで使う===
166行目: 235行目:
   
 
===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
176行目: 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
190行目: 259行目:
 
}}
 
}}
   
  +
/etc/msmtprc のパーミションをユーザーが読み書きできる(600)ようにしない限り、{{ic|php-fpm}} はメール送信に失敗し、以下の警告をログに出すでしょう: {{ic|PHP Warning: mail(mail.log): failed to open stream}}
===Using XOAUTH2 Authentication for Gmail===
 
msmtp currently does not support OAUTH2 authentication. To use XOAUTH2 authentication with Gmail (see [https://developers.google.com/gmail/imap/xoauth2-protocol official information]), you can install the {{AUR|msmtp-oauth2}} package in AUR. The package did a small hack so that the plain authentication method will send the {{ic|AUTH XOAUTH2 password}} instead of the {{ic|AUTH PLAIN ...}}, effectively disabling plain authentication and enabling XOAUTH2. An example config is as follows:
 
{{bc|
 
account gmail
 
host smtp.gmail.com
 
port 587
 
from your@gmail_login_email
 
tls on
 
tls_starttls on
 
tls_certcheck off
 
auth plain
 
user any_thing_here
 
passwordeval "get-gmail-token"
 
logfile ~/.msmtp.log
 
}}
 
The {{ic|get-gmail-token}} script can be found from the source files of the AUR package. See more information on [https://www.bytereef.org/howto/oauth2/getmail.html getmail link] about how this works. And see [https://developers.google.com/gmail/api/quickstart/python Gmail API quickstart] for instruction on registering a Gmail APP and authorizing it to access emails.
 
   
 
==トラブルシューティング==
 
==トラブルシューティング==
 
===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
   
227行目: 290行目:
   
 
===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 サーバ ====
上記の設定で 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 の問題===
242行目: 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 にあります):

ヒント: 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

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