Powershell Outlook e-mail, attachements, subject
So I got a folder with different PDF files ("RG 330526.pdf", "RG 330527.pdf", "RG 330528.pdf") etc.
Now, I want to create a new e-mail, attach the first pdf from a folder (C:\1_PDF) and name the subject just like the pdf file. Save the e-mail in drafts and start over with the next e-mail / next pdf file.
This is what i got so far, i need some help with the loops:
$Outlook = New-Object -ComObject Outlook.Application
$namespace = $Outlook.GetNameSpace("MAPI")
$namespace.Logon($null, $null, $false, $true);
$EmailFrom = ("[email protected]")
$Mail = $Outlook.CreateItem(0)
$Mail.To = $EmailTo
$Mail.Subject = $EmailSubject
#$signature = $Mail.HtmlBody
#$Mail.HtmlBody = $EmailHtmlBody + $signature
$account = $outlook.Session.Accounts.Item($EmailFrom)
function Invoke-SetProperty {
param(
[__ComObject] $Object,
[String] $Property,
$Value
)
[Void] $Object.GetType().InvokeMember($Property,"SetProperty",$NULL,$Object,$Value)
}
Invoke-SetProperty -Object $mail -Property "SendUsingAccount" -Value $account
#$Mail.GetInspector.Activate()
$Mail.Save()
Solution 1:
Got it done!
This works for me:
$WorkDir = "C:\1_PDF"
$RGMailList = Get-ChildItem -Path $WorkDir -Filter *.pdf
ForEach ($RGMail in $RGMailList)
{
$Outlook = New-Object -ComObject Outlook.Application
$namespace = $Outlook.GetNameSpace("MAPI")
$namespace.Logon($null, $null, $false, $true);
$EmailFrom = ("[email protected]")
$Mail = $Outlook.CreateItem(0)
$Mail.To = "[email protected]"
$Mail.Subject = "Subject " + "$RGMail".substring(3)
$account = $outlook.Session.Accounts.Item($EmailFrom)
function Invoke-SetProperty {
param(
[__ComObject] $Object,
[String] $Property,
$Value
)
[Void] $Object.GetType().InvokeMember($Property,"SetProperty",$NULL,$Object,$Value)
}
Invoke-SetProperty -Object $mail -Property "SendUsingAccount" -Value $account
$Mail.GetInspector.Activate()
$Mail.Attachments.Add("$WorkDir\$RGMail")
$Mail.Send()}