Parsing a file of email adresses to set email recipients - powershell

I'm trying to write a PowerShell script which parses a list of email addresses and send a mail to them.
The file is formatted this way:
a.a#domain.com
b.b#domain.com
c.c#domain.com
...
I figured something like:
$recipients = Get-Content -Path MY_FILE.txt
$outlook = New-Object -ComObject Outlook.Application
$mail = $Outlook.CreateItem(0)
$mail.To = $recipients # here's the problem
$mail.Subject = "MY_SUBJECT"
$mail.HTMLBody = "MY_HTML_BODY"
$mail.Send()
My problem, as you can see is: how can I assign the addresses in $recipients to $mail.To?

When in doubt, read the documentation:
MailItem.To Property (Outlook)
Returns or sets a semicolon-delimited String list of display names for the To recipients for the Outlook item.
Read/write.
[...]
Remarks
This property contains the display names only. The To property corresponds to the MAPI property PidTagDisplayTo. The Recipients collection should be used to modify this property.
Emphasis mine.
To send one mail to all recipients change this line:
$mail.To = $recipients
into this:
foreach ($addr in $recipients) {
$mail.Recipients.Add($addr)
}
and the code should do what you want.

If you want to send every address in your file a separate email do it this way:
$recipients = Get-Content -Path MY_FILE.txt
$outlook = New-Object -ComObject Outlook.Application
ForEach ($recipient in $recipients) {
$mail = $Outlook.CreateItem(0)
$mail.To = $recipient
$mail.Subject = "MY_SUBJECT"
$mail.HTMLBody = "MY_HTML_BODY"
$mail.Send()
}
Also make sure you close the COM Object by adding the following to the end of your file:
$outlook.Quit()
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($outlook) | Out-Null

You could try something like this:
$file = "$PSScriptRoot\MY_FILE.txt"
# Add a List of recipients
$to = #()
foreach ($email in (Get-Content $file)) {
$to += "$email;"
}
Write-Host "Complete recipient-list: $to"
$outlook = New-Object -ComObject Outlook.Application
$mail = $Outlook.CreateItem(0)
$mail.To = "$to"
$mail.Subject = "MY_SUBJECT"
$mail.HTMLBody = "MY_HTML_BODY"
$mail.Send()

Related

Send mail to Myself using Powershell

I managed to send mail using powershell, but what i want is for the one who runs the script be the one who send it and receive the mail so it should be automatic. and I don't know how i can manage to do that, So I'm asking for your help please.
here is my code
$Outlook = New-Object -ComObject Outlook.Application
$Mail = $Outlook.CreateItem(0)
$Mail.To = #need to be the mail of the one who runs the script
$Mail.Subject = "send mail"
$Mail.Body = "This is my mail"
$Mail.Send()
Write-Host "Mail Sent Successfully"
Adding to Mathias comment, this is working for me;
$Outlook = New-Object -ComObject Outlook.Application
$Mail = $Outlook.CreateItem(0)
$Mail.Sender = $Outlook.GetNamespace("MAPI").Accounts.Item(1).SmtpAddress # or 'noreply#domain.com'
$Mail.To = $Outlook.GetNamespace("MAPI").Accounts.Item(1).SmtpAddress
$Mail.Subject = "send mail"
$Mail.Body = "This is my mail"
$Mail.Send()
This is working without a Sender address for me, it uses my default mail profile - but you could try manually specifying that if it's not working for you.
Well I managed to find something that worked for me. all I did is put the $Outlook.GetNamespace("MAPI").Accounts.Item(1).SmtpAddress in a variable and call it after that
$Outlook = New-Object -ComObject Outlook.Application
$Mail = $Outlook.CreateItem(0)
$email = $Outlook.GetNamespace("MAPI").Accounts.Item(1).SmtpAddress
$Mail.To = "$email"
$Mail.Subject = "send mail"
$Mail.Body = "This is my mail"
$Mail.Send()
Thank you again for your help !

Script to import information from csv to auto send email from outlook

Pretty new to this so bear with me.....
I am attempting to find a way to be able to import a CSV list including name, email, subject and a word (info) to the body of the email , and send it via Outlook and then go to the next line send that out, so on and so forth till all emails have been sent out. So the email may look like:
Email: $email
Subject: $subject
Body:
$name,
Hello! blah blah blah blah blah $info blah blah blah
Thank You,
end
I am looking to send out many from one list it will all be the same generic email but with certain words changed so looking to cut down on hand emailing time.
I have tried and successfully sent out one email with a script but that is it
Here is what I found to attempt to work off of
$Outlook = New-Object -ComObject Outlook.Application
$Mail = $Outlook.CreateItem(0)
$Mail.To = "todd#emconsultinginc.com"
$Mail.Subject = "test"
$Mail.Body ="test"
$Mail.Send()
This is where I got to. I was able to get it to pull from a CSV with each line. Now I am wondering if we can pull the content from a word doc, keeping formatting and send it. I tried to pull the Word Doc info but I do not think I am quite there. Any suggestions?
$Mail.Send()
$Outlook = New-Object -ComObject Outlook.Application
$csv = Import-Csv C:\code\test.csv
$wd = New-Object -ComObject Word.Application
$doc = $wd.Documents.Open("C:\code\test.html")
$doc.Range().text
foreach ($line in $csv){
$Mail = $Outlook.CreateItem(0)
$Mail.To = $line.Email
$Mail.Subject = $line.Subject
$Name = $Line.Name
$body = $doc.Text
$Mail.Body ="Hello $Name, $body
$Mail.Send()
}

Email multiple people individual attachments

I am using Send-MailMessage to email multiple different recipients each with an individual report. I have just been repeating the Send-Mail command with the different attachment paths for each recipient however the problem I am running into since I also have to use -UseSsl -credential I have to authenticate each time a new message is sent. Is there a way to authenticate once without having to do it each time?
Send-MailMessage is a wrapper for .net smtpclient. You can do your custom version of it, for example:
$client = New-Object System.Net.Mail.SmtpClient
$client.EnableSsl = $true
$client.Host = "smtp.server.com"
$client.Credentials = $creds
foreach ($r in $recipients) {
$from = "from#mail.com"
$to = $r
$msg = New-Object System.Net.Mail.MailMessage $from, $to
$msg.Subject = "subject"
$msg.Body = "body"
$msg.Attachments.Add("C:\temp\test.html")
$client.Send($msg)
}

Send mail with "Send As" or change sender

I'm making a script to automate Outlook 2016. I have one account with two different inboxes from clients.
At the end of the script, I need to send an email from the name of the inbox the script is run from. I'm authorized to send email in the name of both inboxes, but I can't get the script to do it. I post my actual code:
The code:
Function Send-Email {
param ([String]$desde,[String]$subject,[String]$buzon,[String]$inc)
$mail = $Outlook.CreateItem(0)
$firma ="
Textplan
"
$mail.subject = "Closed Ticket "+$subject
[String]$cuerpo =#"
Dear colleague,
bla bla bla
Thank you.
"#
$mail.sender = $buzon
$mail.body = $cuerpo+" "+$firma
$mail.To = $desde
$mail.Send()
Start-Sleep 3
}
$Outlook = New-Object -ComObject Outlook.Application
$desde = client2#mail.com
$buzon = inbox1#mail.com
$inc = 000000001
$subject = "Automat request"
Send-Email -desde $desde -subject $asunto -buzon $Buzon1 -inc $inc
Your issues look to be:
Object defined outside the function it's being used in:
$Outlook = New-Object -ComObject Outlook.Application
Unquoted strings in variables:
$desde = client2#mail.com
$buzon = inbox1#mail.com
$inc = 000000001
Mix and match of single and double quotes
I'd recommend reading about_quoting_rules to understand the difference as this is quite crucial when using variables within strings.
the function param $inc is defined has data but is not used for anything?
Recommend to remove this if it's not used.
Not specifically an issue, but I've changed the here string (#" "#) to a normal string with line breaks (`r`n`)
After resolving the issues and tidying the code:
Function Send-Email {
param (
[String]$desde,
[String]$subject,
[String]$buzon,
[String]$inc
)
$firma = 'Textplan'
$cuerpo = "Dear colleague,`r`nbla bla bla`r`nThank you."
$Outlook = New-Object -ComObject Outlook.Application
$mail = $Outlook.CreateItem(0)
$mail.subject = 'Closed Ticket ' + $subject
$mail.sender = $buzon
$mail.body = "$cuerpo $firma"
$mail.To = $desde
$mail.Send()
Start-Sleep 3
}
$desde = 'client2#mail.com'
$subject = 'Automat request'
$buzon = 'inbox1#mail.com'
$inc = '000000001'
Send-Email -desde $desde -subject $subject -buzon $buzon -inc $inc
I don't have Outlook to test the code, but from checking it over with the Outlook.Application documentation it now seems to be valid.
I finally used this lines and work well:
$mail.SentOnBehalfOfName = "inbox1d#mail.com"
$mail.SendUsingAccount = $Outlook.Session.Accounts | where {$_.DisplayName -eq $FromMail}
Function Send-Email {
param (
[String]$desde,
[String]$subject,
[String]$buzon,
[String]$inc
)
$firma = 'Textplan'
$cuerpo = "Dear colleague,`r`nbla bla bla`r`nThank you."
$Outlook = New-Object -ComObject Outlook.Application
$mail = $Outlook.CreateItem(0)
$mail.subject = 'Closed Ticket ' + $subject
$mail.sender = $buzon
$mail.body = "$cuerpo $firma"
$mail.SentOnBehalfOfName = "inbox1d#mail.com"
$mail.SendUsingAccount = $Outlook.Session.Accounts | where {$_.DisplayName -eq $FromMail}
$mail.Send()
Start-Sleep 3
}
$desde = 'client2#mail.com'
$subject = 'Automat request'
$buzon = 'inbox1#mail.com'
$inc = '000000001'
Send-Email -desde $desde -subject $subject -buzon $buzon -inc $inc

Powershell, send email with outlook as different email

We have some powershell automation in place which sends an email with outlook with one email account but we are looking for a way to be able to set the sender email address to a different outlook account we have access to.
I've tried googling and looking round on here and cant seem to find the way of doing it.
here is the code we are using.
$Outlook = New-Object -comObject Outlook.Application
$Mail = $Outlook.CreateItem(0)
start-sleep 5
$Mail.subject = ""
$mail.
$Mail.To = ""
$Mail.Cc = ""
$Mail.Body = "Test"
$Mail.Display()
$Mail.Send()
Just use the below Outlook function to send the email. You are actually doing the same over there. Is there any error you are getting ? Anyways, Use the below one:
Follow all the comments in the function for your reference.
Function Global:Send-Email {
[cmdletbinding()]
Param (
[Parameter(Mandatory=$False,Position=0)]
[String]$Address = "user2#domain.com",
[Parameter(Mandatory=$False,Position=1)]
[String]$Subject = "Mail Subject",
[Parameter(Mandatory=$False,Position=2)]
[String]$Body = "MailBody"
)
Begin {
Clear-Host
# Add-Type -assembly "Microsoft.Office.Interop.Outlook"
}
Process {
# Create an instance Microsoft Outlook
$Outlook = New-Object -ComObject Outlook.Application
$Mail = $Outlook.CreateItem(0)
$Mail.To = "$Address"
$Mail.Subject = $Subject
$Mail.Body =$Body
# $Mail.HTMLBody = "HTML BODY"
# $File = "D:\CP\timetable.pdf"
# $Mail.Attachments.Add($File)
$Mail.Send()
} # End of Process section
End {
# Section to prevent error message in Outlook
$Outlook.Quit()
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($Outlook)
$Outlook = $null
} # End of End section!
} # End of function
# Example of using this function
Send-Email #-Address User2#domain.com
Note: If you want to send email from someone's behalf then you have to enable anonymous mail from the connectors or the user should have the permission to send mail from someone's behalf. In that case, you can add one more object as
$mail.From=""
One sample example to send mail from GMAIl as reference.
$From = "YourEmail#gmail.com"
$To = "AnotherEmail#YourDomain.com"
$Cc = "YourBoss#YourDomain.com"
$Attachment = "C:\temp\Some random file.txt"
$Subject = "Email Subject"
$Body = "Insert body text here"
$SMTPServer = "smtp.gmail.com"
$SMTPPort = "587"
Send-MailMessage -From $From -to $To -Cc $Cc -Subject $Subject `
-Body $Body -SmtpServer $SMTPServer -port $SMTPPort -UseSsl `
-Credential (Get-Credential) -Attachments $Attachment
Hope it helps...