Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
2
Help us understand the problem. What is going on with this article?
@DisneyAladdin

【Perl】E-maiを送信する

ちょっと廃れてきたと感じるPerlをあえて使って,E-mailを送信するプログラムを作ってみました.
送信用アドレスは,GoogleだとけっこうネットにのってるのであえてYahooで実験してみました.


use utf8;
use Encode;
use strict;
use warnings;
# メール送るのに必要となるモジュール
use Authen::SASL;
use MIME::Base64;
use Net::SMTP;

sub Notify{
    my $name  = shift;
    my $price = shift;
    my $url   = shift;
    my $store = shift;

#send E-mail
my $SMTP_CONF =
    {host=> 'smtp.mail.yahoo.co.jp', #yahooのsmtpサーバを送信用に指定
     port=> '587',   # ドメインによって違います
     from=> 'Username@yahoo.co.jp', #送信用ヤフーメールアドレスを入力
     return_path=> 'Username@yahoo.co.jp', #上に同じ
     auth_uid=> 'Username@yahoo.co.jp', #上に同じ
     auth_pw=> 'password' #Yahooアカウントのパスワード
    };

main($name,$price,$url,$store);

sub main {
    my $name  = shift;
    my $price = shift;
    my $url   = shift;
    my $store = shift;
    #以下のrecievingを替えて受信先のアドレスとして指定してください.
    publish_test_mail(['recieving@gmail.com'],$name,$price,$url,$store);
}


sub publish_test_mail {
    my ($mailto ) = shift; #default
    my $name     = shift;
    my $price    = shift;
    my $url      = shift;
    my $store    = shift;
    my $subject_org = '在庫を確認しました'; # メールのタイトル<img width="382" alt="email.png" src="https://qiita-image-store.s3.amazonaws.com/0/377231/1dc8d0bc-d073-fac6-bf4f-ec612665b989.png">

    my $subject = Encode::encode('MIME-Header-ISO_2022_JP', $subject_org);
    my $display_deduction = abs($deduction);

    #mailtoがない場合、送信は行いません. for debug                                                                                        
        if( ref($mailto) ne "ARRAY" or @$mailto < 1 ){
            return undef;
        }
        my $mailto_str = join(',', @$mailto );
        my $message =<<EOF;
  # 以下,メールの中身になるので適せん変えてください.変数もきちんと反映されます
  Hello! This E-mail is automatically sent from Perl program.
  Please check the item
  【NAME: $name
  【STORE: $store
  【PRICE :$price
  【URL : $url
  EOF

   #メールのヘッダーを構築                                                                                                               
   my $header = << "MAILHEADER_1";
   From: $SMTP_CONF->{from}
   Return-path: $SMTP_CONF->{return_path}
   Reply-To: $SMTP_CONF->{return_path}
   To: $mailto_str
   MAILHEADER_1
   $header .=<<"MAILHEADER_2";
   Subject: $subject
   Mime-Version: 1.0
   Content-Type: text/plain; charset = "ISO-2022-JP"
   Content-Transfer-Encoding: 7bit
   MAILHEADER_2
    $message = encode('iso-2022-jp',$message);
    my $smtp = Net::SMTP->new($SMTP_CONF->{host},
                              Hello=>$SMTP_CONF->{host},
                              Port=> $SMTP_CONF->{port},
                              Timeout=>20,
                             );
    unless($smtp){
        my $msg = "can't connect smtp server: $!";
        die $msg;
    }

    $smtp->auth($SMTP_CONF->{auth_uid}, $SMTP_CONF->{auth_pw}) or
        die "can't login smtp server";

    $smtp->mail($SMTP_CONF->{from});
    $smtp->to(@$mailto);
    $smtp->data();
    $smtp->datasend("$header\n");
    $smtp->datasend("$message\n");
    $smtp->dataend();
    $smtp->quit;
    }
}

# 今回はここで変数をあえて作って,関数に引数として渡します.
my $name  = "PlayStation4"
my $price = "30000"
my $url   = "yodobashi.com"
my $store = "YodobashiCamera"
&Notify($name,$price,$url,$store);

結果

変数もきっちち反映されているのがわかります.
email.png

2
Help us understand the problem. What is going on with this article?
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
DisneyAladdin
Pythonが大好物です.

Comments

No comments
Sign up for free and join this conversation.
Sign Up
If you already have a Qiita account Login
2
Help us understand the problem. What is going on with this article?