LoginSignup
1

More than 1 year has passed since last update.

powershellでメール送信

Last updated at Posted at 2022-08-26

自分用のテンプレ。明日休みたいときに。
雑多な内容が含まれていますがご了承ください。

※以下の記事を大いに参考にさせて頂きました。ありがとうございます!
https://miajimyu.com/docs/powershell/powershell-tips/how-to-create-outlook-mail/

mail.ps1
# 明日の日付を取得
$tommorow = (Get-Date).AddDays(1)

# 年月日を取得。Get-Date | Get-Memberも参照。
$yyyy = $tommorow.Year # $tommorow.ToString("yy"), $tommorow.ToString("yyyy")でも可
$mm = $tommorow.Month # $tommorow.ToString("M"), $tommorow.ToString("MM")でも可
$dd = $tommorow.Day # $tommorow.ToString("d"), tommorow.ToString("dd")でも可
$DayOfWeek = $tommorow.ToString("ddd") # $tommorow.DayOfWeekにすると英語になってしまう...

# 現在のパスを取得
$scriptPath = $MyInvocation.MyCommand.Path
$scriptDir = $(Split-Path -Parent $scriptPath)

# メッセージを表示
function ShowMessage($message){
    Add-Type -Assembly System.Windows.Forms
    [System.Windows.Forms.MessageBox]::Show($message)
}

# 入力ボックスを表示
function GetInputFromInputBox(){
    [void][System.Reflection.Assembly]::Load("Microsoft.VisualBasic, Version=8.0.0.0, Culture=Neutral, PublicKeyToken=b03f5f7f11d50a3a")
    $inputFronInputBox = [Microsoft.VisualBasic.Interaction]::InputBox("一言コメントをどうぞ", "コメント入力","※理由:急に温泉に行きたくなったため。")
    return $inputFronInputBox
}

function CreateMailByOutlook {
    $Outlook = New-Object -ComObject Outlook.Application
    $Mail = $Outlook.CreateItem(0)
    $Mail.To = $To
    $Mail.CC = $CC
    $Mail.Subject = $Subject
    $Mail.Body = $Body
    $inspector = $Mail.GetInspector
    $inspector.Display() 
}
 
function Main {
    CreateMailByOutlook
}

#ここから ##################################

# メッセージを表示
ShowMessage "休暇取得メールを作成します"

# コメントを入力
$comment = GetInputFromInputBox

# メール本文
$To = "sample1@example.com"
$CC = "sample1@example.com; sample1@example.com"
$Subject = "[勤怠連絡]明日休みます"
$Body = @"
皆さま

お疲れ様です。XXです。
突然ですが、明日$mm/$dd($DayOfWeek)はお休みを頂きます。

$comment

お手数おかけしますが、何卒よろしくお願いいたします。
"@

Main

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
1