Good Morning,
I am volunteering to help a school create a bulk email tool so they can send emails directly to all via powershell ise, Im sorry if this is a duplicate issue, I tried searching for an answer.
THIS IS CURRENTLY SET UP TO JUST SEND TO ONE PERSON AND IS NOT SET UP TO RUN OFF OF CSV, IM STILL IN THE TESTING PHASE
here is my current code:
## Written 5/17/18 ##Dudzy
Write-Host "=======================================================================" -ForegroundColor Green
Write-Host "============================= Email tool ==============================" -ForegroundColor Green
Write-Host "=======================================================================" -ForegroundColor Green
#Menu Actions Below
do
{
#1 send weekend update
$SMTPServer = "smtp.gmail.com"
$SmtpServerPort = "587"
#Desktop Path
$DesktopPath = [Environment]::GetFolderPath("Desktop")
#line items of order
$Principal = read-host -Prompt 'Principal'
$Preschool = read-host -Prompt 'Pre-School'
$first = read-host -Prompt '1st Grade'
$second = read-host -Prompt '2nd Grade'
$third = read-host -Prompt '3rd Grade'
$forth = read-host -Prompt '4th Grade'
$fifth = read-host -Prompt '5th Grade'
$sixth = read-host -Prompt '6th Grade'
$seventh = read-host -Prompt '7th Grade'
$eighth = read-host -Prompt '8th Grade'
$sister = read-host -Prompt 'Sister Nancy'
$Father = read-host -Prompt 'Father'
$sports = read-host -Prompt 'Sports'
$to = read-host -Prompt 'To'
$from = "EMAIL ADDRESS HERE"
#Subject
$Subject = "Weekend Update"
#Email Body
$EmailBody = @"
Hello from Holy Trinity School
Weekend Update:
From the Principal: $Principal
Pre-School: $preschool
1st Grade: $first
2nd Grade: $second
3rd Grade; $thrid
4th Grade: $forth
5th Grade: $fifth
6th Grade: $sixth
7th Grade: $seventh
8th Grade: $eighth
Sister Nancy: $sister
From Father: $father
Sports: $sports
"@
#Sending Approval Email
Send-MailMessage -SmtpServer "smtp.gmail.com" -To $To -From $from -Subject "$subject" -Body "$EmailBody" -BodyAsHtml -Priority High -ErrorAction Stop
}
Write-host "emails sent"
pause
until ($input -eq 'q')
Here is the error im getting (however I cannot figure out to fix STARTTLS) :
Send-MailMessage : The SMTP server requires a secure connection or the client
was not authenticated. The server response was: 5.7.0 Must issue a STARTTLS
command first. 7-v6sm6360408iou.36 - gsmtp
At F:\HTS Email tool\email toolbox.ps1:65 char:1
Send-MailMessage -SmtpServer “smtp.gmail.com ” -To $To -From $from -S …
CategoryInfo : InvalidOperation: (System.Net.Mail.SmtpClient:Smtp
Client) [Send-MailMessage], SmtpException
FullyQualifiedErrorId : SmtpException,Microsoft.PowerShell.Commands.SendMa
ilMessage
bdevogt
September 10, 2018, 9:11am
2
This is how I normally send mail
#region Mail
$login = ""
$password = "" | Convertto-SecureString -AsPlainText -Force
$credentials = New-Object System.Management.Automation.Pscredential -Argumentlist $login,$password
$smtpServer = "smtp.outlook.com"
$smtpFrom = ""
$smtpTo = ""
$MailSubject = ""
#############
#Send Email
Send-MailMessage -to $smtpTo -from $smtpFrom -subject $MailSubject -credential $credentials -body $OutFull -SmtpServer $smtpServer -Priority Normal -UseSsl
sleep 5
#endregion
Use -usessl and authenticate
updated, I have had a few tips, here is where I am at now:
you are correct, I had UseSSL missing, however once I input it, I now have a new error, here is the updated code
Thank you all for the help but still not there yet
## Written 5/17/17 ##Dudzy
Write-Host "=======================================================================" -ForegroundColor Green
Write-Host "============================= Email tool ==============================" -ForegroundColor Green
Write-Host "=======================================================================" -ForegroundColor Green
#Menu Actions Below
#1 send weekend update
$login = ""
$password = "" | Convertto-SecureString -AsPlainText -Force
$credentials = New-Object System.Management.Automation.Pscredential -Argumentlist $login,$password
$SMTPServer = "smtp.gmail.com"
#Desktop Path
$DesktopPath = [Environment]::GetFolderPath("Desktop")
#line items of order
$Principal = read-host -Prompt 'Principal'
$Preschool = read-host -Prompt 'Pre-School'
$first = read-host -Prompt '1st Grade'
$second = read-host -Prompt '2nd Grade'
$third = read-host -Prompt '3rd Grade'
$forth = read-host -Prompt '4th Grade'
$fifth = read-host -Prompt '5th Grade'
$sixth = read-host -Prompt '6th Grade'
$seventh = read-host -Prompt '7th Grade'
$eighth = read-host -Prompt '8th Grade'
$sister = read-host -Prompt 'Sister Nancy'
$Father = read-host -Prompt 'Father'
$sports = read-host -Prompt 'Sports'
$to = read-host -Prompt 'To'
$from = "Email here"
#Subject
$Subject = "Weekend Update"
#Email Body
$EmailBody = @"
<font color="335bff">Hello from Holy Trinity School
<p class="small">
Weekend Update:
From the Principal: $Principal
Pre-School: $preschool
1st Grade: $first
2nd Grade: $second
3rd Grade; $thrid
4th Grade: $forth
5th Grade: $fifth
6th Grade: $sixth
7th Grade: $seventh
8th Grade: $eighth
Sister Nancy: $sister
From Father: $father
Sports: $sports
"@
#Sending Approval Email
Send-MailMessage -to $To -from $From -subject $Subject -credential $credentials -body $EmailBody -SmtpServer $smtpServer -Priority Normal -UseSsl
pause
New error (I am not sure how to create a secure connection) :
Send-MailMessage : The SMTP server requires a secure connection or the client was not
authenticated. The server response was: 5.5.1 Authentication Required. Learn more at
At F:\HTS Email tool\email toolbox.ps1:67 char:1
Send-MailMessage -to $To -from $From -subject $Subject -credential $c …
CategoryInfo : InvalidOperation: (System.Net.Mail.SmtpClient:SmtpClien
t) [Send-MailMessage], SmtpException
FullyQualifiedErrorId : SmtpException,Microsoft.PowerShell.Commands.SendMailMes
sage
bdevogt
September 11, 2018, 2:52am
4
Sorry don’t have an Gmail account so I cannot test this.
But it tells you that you are trying to send without the proper authentication.
donj
September 11, 2018, 8:56am
5
Yeah, you have to BOTH use a secure connection AND provide valid credentials to the server. It can’t just “inherit” your Windows logon credentials for an SMTP connection. You’ll need to see what kind of credential format Gmail wants, and there’s no guarantee their service is compatible with the kind of credential Send-MailMessage can provide.
Honestly, Gmail hates being used as a mail relay; you may check to ensure your plan even permits you to do this because if it doesn’t, they’ll shut you down.