Import-CSV | Export-HTML Embed in mail

Welcome Forums General PowerShell Q&A Import-CSV | Export-HTML Embed in mail

  • This topic has 5 replies, 3 voices, and was last updated 1 month ago by
    Participant
    .
Viewing 5 reply threads
  • Author
    Posts
    • #234550
      Participant
      Topics: 1
      Replies: 2
      Points: 19
      Rank: Member

      Hi
      i`m pretty newbie in this field,
      I need to Import-Csv pipe it to ConvertTo-Html (RTL Hebrew table) Send-MailMessage it
      Embedd as body TO = multiple recipients

      Script location:
      c:\daily\lastfilebydate.csv

      Reciptient:
      [email protected]
      [email protected]
      [email protected]

      Exchange:
      172.16.20.15

      partial result achieved with the following syntax:

      Import-Csv -Path c:\daily\lastfilebydate.csv | ConvertTo-Html -CssUri 'C:\design.css' | Out-File lastfilebydate.html -Encoding utf8

      please help needed!

    • #234637
      Participant
      Topics: 5
      Replies: 2384
      Points: 6,066
      Helping Hand
      Rank: Community MVP

      What kind of help do you need? Did you read the help for Send-Mailmessage? The rest is pretty straight forward.

      Instead of outputting the converted CSV file to a file a assign it to a variable and use this variable to provide it to Send-MailMessage. The same for the list of your recipients.

    • #234685
      Participant
      Topics: 12
      Replies: 1651
      Points: 2,702
      Helping Hand
      Rank: Community Hero

      Also, you don’t need the Exchange server information, you specifically need SMTP Relay information, which is name, port and if it not anonymous, then credentials permitted to send mail through the relay.

    • #234778
      Participant
      Topics: 1
      Replies: 2
      Points: 19
      Rank: Member

       finally succeeded with the following syntax:

      $mycsv = Import-Csv -Path z:\daily\08062020.csv | ConvertTo-Html -Head $style
      $mailBody = 
      @"
      $mycsv
      "@
      
      $style = "<style>BODY{font-family: Arial; font-size: 10pt; direction:rtl;}"
      $style = $style + "TABLE{border: 1px solid black; border-collapse: collapse;}"
      $style = $style + "TH{border: 1px solid black; background: #dddddd; padding: 5px; }"
      $style = $style + "TD{border: 1px solid black; padding: 5px; }"
      $style = $style + "</style>"
      
      Send-MailMessage -Body $mailBody -BodyAsHtml `
      -From "[email protected]" -To "[email protected]" `
      -Subject "Report Summery" -Encoding $([System.Text.Encoding]::UTF8) `
      -SmtpServer "172.16.20.15"
      now, how i change font/cell color based on negative value?
      Thanks!
    • #234802
      Participant
      Topics: 5
      Replies: 2384
      Points: 6,066
      Helping Hand
      Rank: Community MVP

      now, how i change font/cell color based on negative value?

      Please take a look at the section “Free Resources” here on this site. There you find a free ebook “Creating HTML Reports in Windows PowerShell“. It has everything you need.

      BTW: You should not use backticks in your code. That’s considered a bad style.

      Here’s a suggestion for a little bit cleaner stlye:

      $mycsv = Import-Csv -Path z:\daily\08062020.csv | ConvertTo-Html -Head $style
      
      $style = @'
      <style>BODY{font-family: Arial; font-size: 10pt; direction:rtl;}
      TABLE{border: 1px solid black; border-collapse: collapse;}
      TH{border: 1px solid black; background: #dddddd; padding: 5px; }
      TD{border: 1px solid black; padding: 5px; }
      </style>
      '@
      
      $SendMailMessageParams = @{
          Body       = $mycsv
          BodyAsHtml = $true
          From       = '[email protected]'
          To         = '[email protected]'
          Subject    = 'Report Summery'
          Encoding   = 'utf8'
          SmtpServer = '172.16.20.15'
      }
      Send-MailMessage @SendMailMessageParams
    • #235021
      Participant
      Topics: 1
      Replies: 2
      Points: 19
      Rank: Member

      thanks!

      $mycsv = Import-Csv -Path z:\daily\08062020.csv | ConvertTo-Html -Head $style

      get error till add the | Out-String

      $mycsv = Import-Csv -Path z:\daily\08062020.csv | ConvertTo-Html -Head $style | Out-String
      • This reply was modified 1 month ago by drlamer.
Viewing 5 reply threads
  • You must be logged in to reply to this topic.