0

我正在尝试以 noreply@domain.com 发送带有 excel 附件的电子邮件。发送有效,但问题在于附件。

这就是脚本的样子。它的工作原理是它被发送但附件被损坏。在此之前,我使用的是完美运行的 Outlook.application 方法。唯一的问题是我想隐藏我的电子邮件,这样我就不会得到回复。但我不能在那里这样做,所以我使用的是 sendgrid。

EDIT1:从https://github.com/tsrob50/SendGridFunction/blob/master/SendGridFunction.ps1获得初始骨架代码 附件内容来自 u/Pandapokeman 发送给我:https ://dzone.com/articles/how -to-send-an-email-with-attachment-powershell-sendgrid-api

function Send-SendGridEmail {
  param(
    [Parameter(Mandatory = $true)]
    [String] $destEmailAddress,
    [Parameter(Mandatory = $true)]
    [String] $fromEmailAddress,
    [Parameter(Mandatory = $true)]
    [String] $subject,
    [Parameter(Mandatory = $false)]
    [string]$contentType = 'text/plain',
    [Parameter(Mandatory = $true)]
    [String] $contentBody,

    #Added after edit1
    [parameter(Mandatory = $false)]
    [string]$FileName,
    [parameter(Mandatory = $false)]
    [string]$FileNameWithFilePath,
    [parameter(Mandatory = $false)]
    [string]$AttachementType
  )

  <#
.Synopsis
Function to send email with SendGrid
.Description
A function to send a text or HTML based email
See https://sendgrid.com/docs/API_Reference/api_v3.html for API details
This script provided as-is with no warranty. Test it before you trust it.
www.ciraltos.com
.Parameter apiKey
The SendGrid API key associated with your account
.Parameter destEmailAddress
The destination email address
.Parameter fromEmailAddress
The from email address
.Parameter subject
Email subject
.Parameter type
The content type, values are “text/plain” or “text/html”.  "text/plain" set by default
.Parameter content
The content that you'd like to send
.Example
Send-SendGridEmail
#>
  ############ Update with your SendGrid API Key ####################
  $apiKey = "myKey"

  $headers = @{
    'Authorization' = 'Bearer ' + $apiKey
    'Content-Type'  = 'application/json'
  }
  #Convert File to Base64
  $FileContent = get-content $FileNameWithFilePath
  $ConvertToBytes = [System.Text.Encoding]::UTF8.GetBytes($FileContent)
  $EncodedFile = [System.Convert]::ToBase64String($ConvertToBytes)

  $body = @{
    personalizations = @(
      @{
        to = @(
          @{
            email = $destEmailAddress
          }
        )
      }
    )
    from             = @{
      email = $fromEmailAddress
    }
    subject          = $subject
    content          = @(
      @{
        type  = $contentType
        value = $contentBody
      }
    )
    attachments = @(
        @{
            content=$EncodedFile
            filename=$FileName
            type= $AttachementType
            disposition="attachment"
        }
    )
  }

  try {
    $bodyJson = $body | ConvertTo-Json -Depth 4
  }
  catch {
    $ErrorMessage = $_.Exception.message
    write-error ('Error converting body to json ' + $ErrorMessage)
    Break
  }

  try {
    Invoke-RestMethod -Uri https://api.sendgrid.com/v3/mail/send -Method Post -Headers $headers -Body $bodyJson 
  }
  catch {
    $ErrorMessage = $_.Exception.message
    write-error ('Error with Invoke-RestMethod ' + $ErrorMessage)
    Break
  }

}
$htmlBody = @"
//STUFF
"@
$splat2 = @{
  destEmailAddress = 'dest'
  fromEmailAddress = 'noreply@domain.com'
  subject          = 'Test Email'
  contentType      = 'text/html'
  contentBody      = $htmlBody
  FileName         = "name.xlsx"
  FileNameWithFilePath = "path/name.xlsx"
  AttachementType ="text/xlsx"
}

来自 reddit 的某人建议使用:“您链接到的示例代码使用 html 文件作为示例,但您正在发送二进制文件。您将希望使用 Get-Content -Encoding Byte 来获取文件的再见,否则您可能会遇到问题由于 Get-Content 的默认行为,换行序列变得混乱。”

我替换了:

$FileContent = get-content $FileNameWithFilePath
with: $FileContent = Get-Content -Encoding Byte $FileNameWithFilePath

这也不起作用。

EDIT2:试过:

AttachementType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"

仍然损坏

4

1 回答 1

0

这一个班轮是为我解决的问题。该文件未损坏。连同HAL9256一起给了我。

$base64string = [Convert]::ToBase64String([IO.File]::ReadAllBytes($FileNameWithFilePath))
AttachmentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"

没有其他编码。它全部被删除/注释掉

于 2021-08-24T14:13:57.290 回答