将SQL结果输出为电子邮件中的表格

时间:2014-09-16 15:10:16

标签: php mysql sql email powershell

我已经整理了一个powershell脚本,该脚本在SQL数据库中查询特定日期范围内的项目,并在返回任何结果时通过电子邮件发送列表(四列数据)。脚本本身现在运行得很好,但是我试图弄清楚如何使用纯文本很好地格式化电子邮件中的结果。

在Powershell中,它很容易使用| format-table -autosize包含结果并在PS中显示,但我设法将结果输入电子邮件的唯一方法是使用out-string,无论我如何设置格式,他们都会#39 ;只是一种乱七八糟的混乱。有没有一个很好的方法来清理它?

我用来生成电子邮件正文的行是:

$msg.body = "Here are the items that need to be returned in the next 30 days:"+"`n"+$results | out-string

编辑:根据请求提供更多代码(我用来生成/发送电子邮件的整个功能):

function sendMail{
 Write-Host "Sending Email"
 #SMTP server name
 $smtpServer = "mailserver"
 #Creating a Mail object
 $msg = new-object Net.Mail.MailMessage
 #Creating SMTP server object
 $smtp = new-object Net.Mail.SmtpClient($smtpServer)
 #Email structure 
 $msg.From = "me@company.com"  #****Change Message Sender
 $msg.To.Add("every1else@company.com") #***Add Recipients 
 $msg.subject = "Lease Returns in Next 30 Days"  #***Change subject
 $msg.body = "Here are the items that need to be returned in the next 30 days:"+"`n"+$results | out-string
 #Sending email 
 $smtp.Send($msg)
 }

1 个答案:

答案 0 :(得分:0)

为什么要使用PowerShell?如果你正在查询sql数据库,为什么不用sql。 只要您知道要运行的查询,并且已配置数据库邮件,可以在此MSDN博客上了解如何执行此操作:Link

如果您需要了解如何设置数据库邮件:LinkLink

相关问题