3
2

More than 5 years have passed since last update.

テキストファイルの内容を本文にしてメール送信する

Posted at

はじめに

  • アプリが生成するファイルをメールで送信したい
  • 生成されるファイルはいくつかあって、アプリの処理結果によって違う

こんな要件を満たす Powershell Script を作成しました。
生成されるファイルを配列で指定することで、ファイル数が増えても Script の処理を変更する必要がないのがポイントです。
Script は、タスクスケジューラ等で定期実行してください。

前提条件

  • ExecutionPolicy が RemoteSigned になっている
  • 送信元となる Script 実行ホストから Port587 で Office365 の SMTP サーバに接続可能な状態
  • Exchange Online 側で SMTP が有効になっている ※Office365 ではなく Gmail でも SMTP Server の設定を変更すれば同様の処理が可能です。

動作確認済み環境

OS $PSVersionTable の PSVersion
Windows Server 2012R2 4.0

処理内容

  • 生成されるファイルの有無を確認し、存在するファイル毎の内容を本文として改行付きで送信する
  • メール送信されたファイルは、削除する
    • ファイル毎にメール通知するため、都度ファイルを削除していきます

Script

コメントアウトの入力例を参考に設定してください。

MailNortifyPS.ps1
# メール通知用テキスト設定
## $data_path = "C:\Script\Mailnotify\"
## $nortifytext_ary = @("処理1結果.txt","処理2結果.txt","処理3結果.txt","処理4結果.txt","処理5結果.txt")
$data_path = "メール通知したいテキストの格納先パスをフルパスで入力(最後に¥を忘れずに)"
$nortifytext_ary = @("メール通知したいテキストのファイル名をコンマ区切りで指定(ファイル名の前後にダブルクォーテーションを忘れずに)")

# Script ログ設定
## $ps_log = "C:\Script\Mailnotify\log\ExecutionResult.log"
$ps_log = "本スクリプトのログファイルをフルパスで指定"

# メール送信設定
## $smtp_server = "smtp.office365.com"
## $auth_userid = "user01@hogehoge.com"
## $auth_userpwd = "user01$Pass"
## $from_addr = "user01@hogehoge.com"
## $dest_addr = "user01@hogehoge.com,user02@hogehoge.com"
$smtp_server = "smtp.office365.com"
$auth_userid = "認証ユーザ名"
$auth_userpwd = "認証ユーザのパスワードもしくはアプリパスワード"
$from_addr = "送信元アドレス(通常は認証ユーザのメールアドレス、共有メールボックス)"
$dest_addr = "メール通知先メールアドレス(複数ならコンマで区切る)"

# ログ出力関数
function write_log($log_msg)
{
  $timestamp = Get-Date;
  Add-Content $ps_log ($timestamp.ToString("yyyy/MM/dd HH:mm:ss.fff") + " " + $log_msg)
}

# メール通知関数
function send_mail($nortifytext_name)
{
    try{
        $mail_body = (Get-Content -Path ($data_path + $nortifytext_name) -Raw)
        $mail_title = ($nortifytext_name + " Mail notification")
        $smtp_client = New-Object Net.Mail.smtpclient($smtp_server, 587)
        $smtp_client.EnableSsl = $true
        $smtp_client.Credentials = New-Object System.Net.NetworkCredential($auth_userid, $auth_userpwd);
        $smtp_client.Send($from_addr, $dest_addr, $mail_title, $mail_body)
        write_log($nortifytext_name + " Mail notification completed")
        # メール通知用テキスト自動削除
        Get-ChildItem ($data_path + $nortifytext_name) | ForEach-Object { $_.Delete() }
    }
    catch{
        write_log($nortifytext_name + " Email notification failed")
    }
}

# メール通知処理呼び出し
if ($nortifytext_ary.Count -eq 0 ) {
    write_log("notification text unspecified")
} else {
    ForEach ($nortifytext_element in $nortifytext_ary) {
        if (Test-Path ($data_path + $nortifytext_element)) {
            write_log($nortifytext_element + " E-mail notification start")
            send_mail $nortifytext_element
        } else {
            write_log($nortifytext_element + " not found")
        }
    }
}
3
2
1

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
3
2