4
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

PerlからgmailでメールをSMTP over TLSで送信する

Last updated at Posted at 2017-02-27

distributed.netのstatsをメールで通知するでは、PerlからNet::SMTPモジュールを使ってメール送信していたのですが、認証もされてないメールはspam判定されるようになってしまいました。仕方なく、今頃になってですがSMTP over TLSで送信するように修正したのでメモ。とりあえず以下のスクリプトでgmailから送信することが出来ています。

# !/usr/local/bin/perl

use strict;
use warnings;

use Net::SMTP;

my $smtp = 'smtp.gmail.com';
my $port = 587;
my $user = '(your acount)@gmail.com';
my $pass = '(your password)';
my $from = '(your acount)@gmail.com';
my $to = '(target mail address)';

my $mail = Net::SMTP->new( $smtp,
    Hello => $smtp,
    Port => $port
);
$mail->starttls();
$mail->auth( $user, $pass );
$mail->mail( $from );
$mail->to( $to );
$mail->data();
$mail->datasend( 'This is test mail.' );
$mail->dataend();
$mail->quit;

ほんとは全権限が付与されるパスワード認証ではなく、OAuthで送信権限のみを付与するのがいいのですが、それはまた今度調べます。

それにしても、昔はpopenしてsendmailコマンドに送り込むだけでメール送信できてたのにねぇ。時代は変わった(とじじいは思うのであった

(2017/2/28追記)

gmailの場合、パスワード認証を通すためには安全性の低いアプリがアカウントにアクセスするのを許可しないといけません。デフォルトでは不許可になっていますので設定を変更しておきましょう。自分のメインアカウントを許可するのは危険ですので、メール送信用に別アカウントを取得するのがいいと思います。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?