I’ll introduce the way to send mail in Perl. 私はこの仕組みを、サーバの状態を必要な時にメールで通知するスクリプトなどで利用しています。 (ref.: Server Monitoring Script made by me, a Perl beginner)
Environment
Computer which runs perl
I tried in 2 environments.
Case 1
- Ubuntu 15.04
- Perl 5.20.2
Case 2
- EC2 Amazon Linux 2015.03
- Perl 5.16.3
SMTP Server
- EC2 Amazon Linux 2015.03
- Postfix
- Authenticate with password in CRAM-MD5 way
Code
Here’s the most simple sample code.
If you don’t require password authentication, there’s no need to use Digest::MD5
or Authen::SASL
, or the line written auth
. But supposing CRAM-MD5 authentication, they are necessary. auth
method of Net::SMTP
requires Authen::SASL
, which requires Digest::MD5
.
Authen::SASL
が入ってないと auth
を実行しても認証がおこなわれません。 Digest::MD5
が入っていないと、 Error: No SASL mechanism found
と表示されます。
パスワードの認証方式は 自動で判別されています。 認証サーバで Digest-MD5 が有効であれば CRAM-MD5 よりも Digest-MD5 を優先的に使います。
そして、認証をするにはインストールしなければならないパッケージがあります。
- For Ubuntu
- libdigest-hmac-perl
- libauthen-sasl-perl
- For Amazon Linux
- perl-Authen-SASL
Amazon Linux の場合、 MD5 のモジュールが入っているパッケージ perl-Digest-MD5 はあらかじめインストールされています。
In many case, perl is installed in advance and Net::SMTP
module is available to use, but authentication requires additional module installation.