如何使用PowerShell将图表作为电子邮件正文发送

时间:2015-01-30 13:37:18

标签: email powershell graph charts

我有以下代码。这用于比较数据的两个日期,然后将其保存在3D图表中。

     [void][Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms.DataVisualization")
$scriptpath = Split-Path -parent $MyInvocation.MyCommand.Definition

# chart object
 $chart1 = New-object System.Windows.Forms.DataVisualization.Charting.Chart
 $chart1.Width = 1300
 $chart1.Height = 600
 $chart1.BackColor = [System.Drawing.Color]::White

# title 
 [void]$chart1.Titles.Add("Order Rate")
 $chart1.Titles[0].Font = "Arial,13pt"
 $chart1.Titles[0].Alignment = "topLeft"

# chart area 

$chartarea3D = New-Object  System.Windows.Forms.DataVisualization.Charting.ChartArea3DStyle
# $chartarea.Name = "ChartArea1"
$chartarea3D.Enable3D = $true
$ChartArea = $chart1.ChartAreas.Add('ChartArea');
$ChartArea.Area3DStyle = $chartarea3D
$chartarea.AxisY.Title = "Orders"
$chartarea.AxisX.Title = "Hour"
$chartarea.AxisY.Interval = 200
$chartarea.AxisX.Interval = 1

# legend 
$legend = New-Object system.Windows.Forms.DataVisualization.Charting.Legend
$legend.name = "Legend1"
$chart1.Legends.Add($legend)

# data source

#region Invoke sql
function Invoke-Sqlcmd2 
<some code>

 #endregion 
 $datasource = Invoke-Sqlcmd2 -ServerInstance yyyyyy -Database     internetordering -Query "SELECT  DATEPART(hh,createdon) AS hour, Count(*)Orders from dbo.OrderHeader (nolock ) 
    where isimported = 1 and createdon between '01/27/2015 0:00:00' and '01/27/2015 23:59:59'
    group by DATEPART(hh,createdon)"

   $datasource2 = Invoke-Sqlcmd2 -ServerInstance zzzzzz -Database internetordering -Query "SELECT  DATEPART(hh,createdon) AS hour, Count(*)Orders from InternetOrdering.dbo.OrderHeader (nolock ) 
    where isimported = 1 and createdon between '01/28/2015 0:00:00' and '01/28/2015 23:59:59'
    group by DATEPART(hh,createdon)"


# data series
[void]$chart1.Series.Add("OrdersLastMonthEOM")
#$chart1.Series["OrdersLastMonthEOM"].ChartType = "Column"
$chart1.Series["OrdersLastMonthEOM"].ChartType = "Line"
$chart1.Series["OrdersLastMonthEOM"].IsVisibleInLegend = $true
$chart1.Series["OrdersLastMonthEOM"].BorderWidth  = 3
#$chart1.Series["OrdersLastMonthEOM"].chartarea = "ChartArea1"
$chart1.Series["OrdersLastMonthEOM"].Legend = "Legend1"
$chart1.Series["OrdersLastMonthEOM"].color = "#62B5CC"
$datasource | ForEach-Object  {$chart1.Series["OrdersLastMonthEOM"].Points.addxy( $_.Hour , $_.Orders) }

# data series
[void]$chart1.Series.Add("OrdersThisMonthEOM")
#$chart1.Series["OrdersThisMonthEOM"].ChartType = "Column"
$chart1.Series["OrdersThisMonthEOM"].ChartType = "Line"
$chart1.Series["OrdersThisMonthEOM"].IsVisibleInLegend = $true
$chart1.Series["OrdersThisMonthEOM"].BorderWidth  = 3
#$chart1.Series["OrdersThisMonthEOM"].chartarea = "ChartArea1"
$chart1.Series["OrdersThisMonthEOM"].Legend = "Legend1"
$chart1.Series["OrdersThisMonthEOM"].color = "#E3B64C"
$datasource2 | ForEach-Object {$chart1.Series["OrdersThisMonthEOM"].Points.addxy( $_.Hour , $_.Orders) }

# save chart
$chart1.SaveImage("$scriptpath\SplineArea2.png","png")

这会在ISplineArea.png上创建一个很酷的3D线图。我需要一种方法在电子邮件正文中发送此图表。任何人都可以帮忙。

1 个答案:

答案 0 :(得分:0)

From Here

简而言之,您必须以HTML格式发送消息,附加图片,为HTML中显示的每张图片设置ContentID(cid),并在img标记<img src="cid:content-id-you-assigned" >

中对其进行处理
$EmailFrom = "NoReply@siteone.ok"
$EmailSubject = "Email including images in HTML"
$smtpServer = "mx1.siteone.ok"
$SendTo = "mailto@sitetwo.ok"

# Working with attachment and HTML code 
$Image = "C:\scripts\MailScript\TestImage.png"
$att1 = new-object Net.Mail.Attachment($Image)
$att1.ContentType.MediaType = "image/png"
$att1.ContentId = "MyAttachment"
$mailmessage = New-Object system.net.mail.mailmessage 
$mailmessage.Attachments.Add($att1)
$body = "<p style='font-family: Calibri, sans-serif'>
     <img src='cid:MyAttachment' /><br />
     This is a demonstration mail. 
     </p>"

$mailmessage.from = $emailfrom
$mailmessage.To.add($sendto)
$mailmessage.Subject = $emailsubject
$mailmessage.Body = $body
$mailmessage.IsBodyHTML = $true
$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 25)
$SMTPClient.Send($mailmessage)
$att1.dispose()