Rexxer

Some tips for me and other

Powershell: send and read email

  1. In your Gmail account we have to turn the feature “Less secure app” on.
  2. To read mail I used the fine tool for PS: https://github.com/nikoblag/Gmail.ps

Script example:

#==============================================================
# Mail
function global:Send-Email ($recipient,$SSubject,$BBody) {
$email = “your.mail@gmail.com”
$pass = “password”
$smtpServer = “smtp.gmail.com”
$Credentials = New-Object System.Management.Automation.PSCredential -ArgumentList $email, $($pass | ConvertTo-SecureString -AsPlainText -Force)
$From = “your.mail@gmail.com”
$To = $recipient
$Cc = “YourBoss@YourDomain.com”
$Subject = $SSubject
$Body = $BBody
$SMTPServer = “smtp.gmail.com”
$SMTPPort = “587”
$encoding = [System.Text.Encoding]::UTF8
Send-MailMessage -From $From -to $To -Subject $Subject -Body $Body -SmtpServer $SMTPServer -port $SMTPPort -UseSsl -Credential $Credentials -Encoding $encoding -Attachments $Attachment
}
#==============================================================

 

$Credentials = New-Object System.Management.Automation.PSCredential -ArgumentList $email, $($pass | ConvertTo-SecureString -AsPlainText -Force)
$gmail = New-GmailSession -Credential $Credentials
$inbox = $gmail | Get-Mailbox
#$inbox | Get-Message -Unread

$message = $inbox | Get-Message -Unread -Subject “DeleteALL”
if ($message) {
if ($message.Subject -eq “DeleteALL”) {
# Do something
echo “The message was detected, deleting the message and start the process …”
Remove-Message -Session $gmail -Message $message
echo “Sending warning email …”
Send-Email $message.From “WARNING!!!” “Running”
# Delete ALL
echo “Deleting …”
Remove-Item -Recurse -Force D:\Kill
echo “Deleting …”
echo “Shredding …”
Start-Process “cipher W:d:\”
$text = Get-Content C:\Backup\report.txt | Out-String
Send-Email $message.From “Finita!!!” $text
}}
else {
echo “Fuhhh. No kill-messages”
$inbox | Get-Message -Unread
}

Remove-GmailSession $gmail

Comments are currently closed.