【发布时间】:2020-04-03 12:10:59
【问题描述】:
要求:通过发送网格帐户(Send Grid API)附加多个附件发送电子邮件。
描述:我能够创建 json 有效负载,并能够通过硬编码附件值来发送单个附件。我正在打开窗口表单对话框并能够选择需要附加的单个/多个文件。
代码:
$FileBrowser = New-Object System.Windows.Forms.OpenFileDialog -Property @{
InitialDirectory = [Environment]::GetFolderPath('Desktop')
#Filter = 'Documents (*.docx)|*.docx|SpreadSheet (*.xlsx)|*.xlsx'
Filter = 'All files (*.*)| *.*'
Title = 'Select File(s) for Attachments'
Multiselect = $true
}
$FileBrowser.ShowDialog() | Out-Null
$FilesEncodedContents = New-Object System.Collections.ArrayList
$AttachmentsjsonRequest = @()
if ($FileBrowser.FileNames.Count -gt 0) {
foreach ($file in $FileBrowser.FileNames) {
[string] $filerawContent = $null
$filedetails = Get-Item $file
$filerawContent = ConvertToBase64Encode $file
if (![string]::IsNullOrWhitespace($filerawContent)) {
$FilesEncodedContents.Add($filerawContent)
$obj = New-Object -TypeName PSObject
$obj | Add-Member -MemberType NoteProperty -Name filename -Value (Get-Item $file).Name
$obj | Add-Member -MemberType NoteProperty -Name content_id -Value (Get-Item $file).Name
$obj | Add-Member -MemberType NoteProperty -Name content -Value $filerawContent
$obj | Add-Member -MemberType NoteProperty -Name disposition -Value 'attachment'
$AttachmentsjsonRequest += $obj
}
}
}
Write-Host "$AttachmentsjsonRequest"
$headers = @{ }
$headers.Add("Authorization", "Bearer $ApiKey")
$headers.Add("Content-Type", "application/json")
$jsonRequest = [ordered]@{
personalizations = @(@{to = @(@{email = "$MailTo" })
subject = "$Subject"
})
from = @{email = "no-reply@xxx.com" }
attachments = "$AttachmentsjsonRequest"
content = @( @{ type = "text/plain"
value = "Sample Mail Body"
}
)
} | ConvertTo-Json -Depth 100
Write-Host $jsonRequest | ConvertTo-Json -Depth 100
Invoke-RestMethod -Uri "https://api.sendgrid.com/v3/mail/send" -Method Post -Headers
$headers -Body $jsonRequest
Write-Host "Mail Sent"
#region ConvertToBase64Encode
Function ConvertToBase64Encode([string] $AttachementFile) {
[string] $fileContentEncoded = $null
if (Test-Path $AttachementFile -PathType leaf) {
$fileContent = get-content $AttachementFile
$fileContentBytes = [System.Text.Encoding]::UTF8.GetBytes($fileContent)
$fileContentEncoded = [System.Convert]::ToBase64String($fileContentBytes)
$fileContentEncoded | set-content ((Get-Item -Path $AttachementFile).Name + ".b64")
}
else {
$fileContentEncoded = $null
Write-Host "File : $FileAttachment not exists,skipping and continue to add if any other
attachments uploaded"
}
return $fileContentEncoded
}
#endregion
问题: [已更新]
尝试上传单个或多个附件后出现以下错误
{"errors":[{"message":"Invalid type. Expected: array, given: string.","field":"attachments","help":"http://sendgrid.com/docs/API_Reference/Web_API_v3/Mail/errors.html#message.attachments"}]} .
参考链接: 发送网格 API 文档:
【问题讨论】:
-
尝试使用数组对象。更新了代码,仍然出现错误。还有什么问题?
-
您没有将 $jsonRequest 变量传递给 Invoke-RestMethod 的 -Body 参数:
Invoke-RestMethod -Uri "https://api.sendgrid.com/v3/mail/send" -Method Post -Headers $headers -Body $jsonRequest -
@Zachafer,抱歉,在代码出现之前更新代码时似乎遗漏了它。但如果我们尝试使用更新的代码,我们在调用 API 后会出现错误,除非我们按照我回答的解决方案修改代码.
-
如何在
to部分添加多个邮箱地址? -
@Pradeep 创建一个数组并添加电子邮件并分配。个性化 = @(@{to = @($validMails) $validMails 是数组类型
标签: json powershell email sendgrid sendgrid-api-v3