在CSV文件中,我有各种列,其中两列用于电子邮件。这些电子邮件列中的每一行都有一组电子邮件。
我希望能够从脚本发送到这些电子邮件地址。
我有一切设置和工作,TO:
。
脚本的想法是它循环csv的每一行并生成一个电子邮件,从该行的单元格中获取值到身体的各个部分。然后它发送一封电子邮件并循环回到CSV的下一行以执行相同的操作,依此类推,直到它到达CSV文件的末尾。
我在插入电子邮件列的变量时遇到问题,我猜是因为电子邮件没有引用"引用"。
我如何把它们带入?
简而言之,代码
关闭循环
##For the purpose of this, the emaildata.csv looks like this sample:
"NameGroup","emailGroupA","emailGroupB"
"Groupabc","a.b@b.com;c.a@b.com","xyv@b.com;xxd@b,com"
"Grouptrd","ca.r@b.com;as.b@b.com","aaa@a.com;bbb@b.com"
"Groupghd","dd.r@b.com;dd.b@b.com","dddaa@a.com;ddddddb@b.com"
$DataDir = "C:\Users\acastrellon\Documents"
$Data= import-csv $DataDir\emaildata.csv
foreach ($i in $Data) {
$NameGroup = $i.NameGroup
$TeamA = $i.emailGroupA.replace(';',"`n")
$TeamB = $i.emailGroupB.replace(';',"`n")
function send-email {
$smtpserver = " server.smtp"
$from = "myemail.com"
$to = $TeamA,$TeamB
send-MailMessage -From $from -To $to -Subject $subject -Body $body -SmtpServer $smtpServer
}
[string] $subject = "some text here: $NameGroup"
#[string] $attachment = "not here yet"
[string] $body = "
Text here that calls out $NameGroup
This also lists: $TeamA
This here lists $TeamB
Done"
send-email -subject $subject -attachment $attachment -body $body
}
#this should loop to get data from next line of csv and generate a new email with data.
答案 0 :(得分:0)
与环境变量对当前会话具有全局影响的批处理文件不同,PowerShell隔离了不同范围内的变量。
您引用了两个$TeamA
& $TeamB
变量 in 一个函数,但是你将它们的值设置在函数之外。因为函数范围(它们被读取的位置)与脚本范围(您设置它们的位置)不同,所以这些变量在脚本中的send-email
函数内将为空。
请阅读PowerShell scopes,因为您需要对脚本功能进行一些更改;从脚本范围($script:TeamA
)读取变量或传递它们into the function as a parameter