2026-04-09

zo3検証ツール 簡易MIMEメール作成スクリプト New-MimeMessage.ps1

<#
.SYNOPSIS
    zo3検証ツール 簡易MIMEメール作成スクリプト
    New-MimeMessage.ps1

.DESCRIPTION
    指定したヘッダ情報・本文・添付ファイルから RFC 2822 準拠の EML ファイルを生成する。
    生成した EML は curl の --upload-file で SMTP 送信に使用できる。
    検証環境・クローズド環境での使用を前提とする。

.PARAMETER From
    送信者アドレス。例: user1@example.test

.PARAMETER To
    宛先アドレス(複数指定可)。例: user2@example.test, user3@example.test

.PARAMETER Subject
    件名。日本語可(RFC2047 Base64 エンコード)。

.PARAMETER Body
    本文。日本語可(UTF-8 / 8bit)。

.PARAMETER Attachments
    添付ファイルパスの配列(省略可)。日本語ファイル名可(RFC2231 エンコード)。

.PARAMETER OutFile
    出力する EML ファイルパス。デフォルト: mail.eml

.EXAMPLE
    .\New-MimeMessage.ps1 -From user1@example.test -To user2@example.test `
        -Subject "テスト" -Body "本文" -OutFile test.eml

.EXAMPLE
    .\New-MimeMessage.ps1 -From user1@example.test -To user2@example.test `
        -Subject "添付テスト" -Body "本文" `
        -Attachments @("C:\tmp\資料.pdf", "C:\tmp\image.png") -OutFile attach.eml

.NOTES
    対応エンコード : Subject=RFC2047 B, ファイル名=RFC2231, 本文=UTF-8/8bit
    非対応        : HTML メール, multipart/alternative, DKIM 署名, CC/BCC ヘッダ自動付与
    送信例        :
        curl.exe --url smtp://mailserver:25 `
            --mail-from user1@example.test `
            --mail-rcpt user2@example.test `
            --upload-file test.eml

    MIT License
    Copyright (c) 2026  yoshio@zo3
#>

param(
    [string]$From,
    [string[]]$To,
    [string]$Subject,
    [string]$Body,
    [string[]]$Attachments = @(),
    [string]$OutFile = "mail.eml"
)

# ===== 基本 =====
$CRLF = "`r`n"

# ===== RFC2047 Subject =====
function Encode-Subject( $string ) {
    $bytes = [System.Text.Encoding]::UTF8.GetBytes( $string )
    $b64data = [Convert]::ToBase64String( $bytes )
    return "=?UTF-8?B?${b64data}?="
}

# ===== Base64(76文字折返し)=====
function To-Base64Lines( $bytes ) {
    $b64data = [Convert]::ToBase64String( $bytes )
    return ( ${b64data} -split "(.{1,76})" | Where-Object { $_ }) -join $CRLF
}

# ===== RFC2231 filename* =====
function Encode-RFC2231( $string ) {
    $bytes = [System.Text.Encoding]::UTF8.GetBytes( $string )
    $enc = ""
    foreach ( $byte in $bytes ) {
        if (
            ( $byte -ge 0x30 -and $byte -le 0x39 ) -or
            ( $byte -ge 0x41 -and $byte -le 0x5A ) -or
            ( $byte -ge 0x61 -and $byte -le 0x7A ) -or
            $byte -in 0x2D, 0x2E, 0x5F, 0x7E
        ) {
            $enc += [char]$byte
        }
        else {
            $enc += "%" + $byte.ToString( "X2" )
        }
    }
    return "UTF-8''$enc"
}

# ===== Date / Message-ID =====
$now = Get-Date
#$dateStr = $now.ToString("ddd, dd MMM yyyy HH:mm:ss K", [System.Globalization.CultureInfo]::InvariantCulture)
$tz = $now.ToString("zzz").Replace(":", "")
$dateStr = $now.ToString("ddd, dd MMM yyyy HH:mm:ss", [System.Globalization.CultureInfo]::InvariantCulture) + " $tz"

$domain = ($From -split "@")[-1]
$msgid = "<" + [guid]::NewGuid().ToString() + "@$domain>"

# ===== boundary =====
$boundary = "----=_Boundary_" + [guid]::NewGuid().ToString("N")

# ===== ヘッダ =====
$headers = @()
$headers += "Date: $dateStr"
$headers += "Message-ID: $msgid"
$headers += "From: $From"
$headers += "To: " + ($To -join ", ")
$headers += "Subject: " + ( Encode-Subject $Subject )
$headers += "MIME-Version: 1.0"

if ($Attachments.Count -gt 0) {
    $headers += "Content-Type: multipart/mixed; boundary=""$boundary"""
}
else {
    $headers += "Content-Type: text/plain; charset=UTF-8"
    $headers += "Content-Transfer-Encoding: 8bit"
}

# ===== 本文 =====
$bodyLines = @()
# 修正(本文取り込み前に正規化)
$Body = $Body -replace "`r`n", "`n" -replace "`n", $CRLF

if ( $Attachments.Count -eq 0 ) {

    $bodyLines += $Body
}
else {
    # 本文パート
    $bodyLines += "--$boundary"
    $bodyLines += "Content-Type: text/plain; charset=UTF-8"
    $bodyLines += "Content-Transfer-Encoding: 8bit"
    $bodyLines += ""
    $bodyLines += $Body

    # 添付
    foreach ( $file in $Attachments ) {

        $bytes = [System.IO.File]::ReadAllBytes( $file )
        $name = [System.IO.Path]::GetFileName( $file )

        # ASCIIフォールバック
        $nameAscii = $name -replace '[^\x20-\x7E]', '_'

        # RFC2231
        $name2231 = Encode-RFC2231 $name

        $bodyLines += ""
        $bodyLines += "--$boundary"
        $bodyLines += "Content-Type: application/octet-stream; name=""$nameAscii"""
        $bodyLines += "Content-Transfer-Encoding: base64"
        $bodyLines += "Content-Disposition: attachment; filename=""$nameAscii""; filename*=$name2231"
        $bodyLines += ""
        $bodyLines += (To-Base64Lines $bytes)
    }
    # 終端
    $bodyLines += ""
    $bodyLines += "--$boundary--"

}


# ===== 結合 =====
$all = ( $headers -join $CRLF ) + $CRLF + $CRLF + ( $bodyLines -join $CRLF )

# ===== 出力(CRLF維持)=====
#[System.IO.File]::WriteAllText( $OutFile , $all , [System.Text.Encoding]::ASCII)
[System.IO.File]::WriteAllBytes($OutFile, [System.Text.Encoding]::UTF8.GetBytes($all))