PoshCode Logo PowerShell Code Repository

Sendmail for PoSh V1.0 by Patrick 35 months ago
embed code: <script type="text/javascript" src="http://PoshCode.org/embed/936"></script>download | new post

Sending Mails via Powershell with Text Edit, Signature Support and Encryption.
Send via Outlook or SMTP (Outlook needs extra plugin -> outlook redemption (google it))
Save as ps1 and call the file. After that simply use sendmail and enter the options

  1. #region vars
  2. $global:maileditor = "C:\Programme\vim\vim72\vim.exe"
  3. $global:encryption = "C:\Programme\GNU\GnuPG\gpg.exe"
  4. $global:enckey = "s.patrick1982@gmail.com"
  5. $global:tempmail = "C:\temp\psmail.txt"
  6. $global:sigmail = "C:\temp\halten\sig.txt"
  7. $global:mailbody = ""
  8. #endregion vars
  9.  
  10. #region initMail
  11. function global:initHeader {
  12.         $global:mailTo = ($global:mailTo).Split(',') | % { $_.Trim() }
  13.  
  14.         if (Test-Path $global:sigmail) {
  15.                 (Get-Content $global:sigmail | Out-String) | Out-File $global:tempmail
  16.         }
  17.  
  18.         & $global:maileditor $global:tempmail
  19.  
  20. }
  21.  
  22. function global:initMail {
  23.         $smtpserver = "yoursmtpserver"
  24.         $myuser = "user"
  25.         $mypass = "pass"
  26.         $myAddress = "Patrick<s.patrick1982@gmail.com>"
  27.        
  28.         $global:mail = New-Object System.Net.Mail.MailMessage
  29.         $global:srv = New-Object System.Net.Mail.SmtpClient
  30.         $global:srv.Host = $smtpserver
  31.         $global:srv.Credentials = New-Object System.Net.NetworkCredential($myuser, $mypass)
  32.        
  33.         $global:mail.from = $myAddress
  34.         foreach ($rcpt in $global:mailTo) {
  35.                 if ($rcpt -ne "") {
  36.                         $global:mail.To.Add($rcpt)
  37.                 }
  38.         }
  39.         $global:mail.subject = $global:subject
  40. }
  41.  
  42. function global:initMailol {
  43.         $global:outlook = New-Object -ComObject Outlook.Application
  44.         $global:srv = New-Object -ComObject Redemption.SafeMailItem
  45.         $global:omail = $outlook.CreateItem("olMailItem")
  46.         $global:srv.Item = $global:omail
  47.  
  48.         #region check
  49.         #User Input
  50.         foreach ($rcpt in $global:mailTo) {
  51.                 $global:srv.Recipients.Add($rcpt) | Out-Null
  52.         }
  53.  
  54.         #check recipients
  55.         $check = $global:srv.Recipients.ResolveAll()
  56.        
  57.         if ($check -eq $False) {
  58.                 for ($i=0; $i -gt $global:sitem.Recipients.Count;$i++) {
  59.                         $global:srv.Recipients.Remove($i)
  60.                         exit(-1)
  61.                 }
  62.         }
  63.         #endregion
  64.  
  65.         $global:srv.item.Subject = $global:subject
  66.  
  67.         if ($global:debug -eq $true) {
  68.                 Write-Host "Mail an - " $global:sitem.Recipients
  69.                 Write-Host "Betreff - " $global:subject
  70.         }
  71. }
  72. #endregion initMail
  73.  
  74. #region sendmail
  75. function global:sendmail {
  76.         Param (
  77.                 [string]$global:client,
  78.                 [string]$global:mailTo,
  79.                 [string]$global:subject,
  80.                 [string]$global:mtype
  81.         )
  82.  
  83.         #Parameter Check
  84.         if (!$global:client) {
  85.                 $global:client = Read-Host -Prompt "Which Client (srv,ol): "
  86.         }
  87.        
  88.         if (!$global:mailto) {
  89.                 $global:mailTo = Read-Host -Prompt "E-Mail To: "
  90.         }
  91.        
  92.         if (!$global:subject) {
  93.                 $global:subject = Read-Host -Prompt "Subject: "
  94.         }
  95.        
  96.         initheader($global:mailTo, $global:subject)
  97.        
  98. #region encrypt
  99.         switch ($global:mtype) {
  100.                 s {
  101.                         & $global:encryption -a -r $global:enckey --clearsign $global:tempmail
  102.                         break;
  103.                 }
  104.                
  105.                 e {
  106.                         & $global:encryption -a -r $global:enckey --encrypt $global:tempmail
  107.                         break;
  108.                 }
  109.         }
  110. #endregion encrypt
  111.  
  112. #body
  113.         if ($global:mtype -ne "") {
  114.                 $global:mailbody = (Get-Content $global:tempmail".asc" | Out-String)
  115.         } else {
  116.                 $global:mailbody = (Get-Content $global:tempmail | Out-String)
  117.         }              
  118.        
  119. #cleaning
  120.         if (test-path $global:tempmail) { Remove-Item $global:tempmail -Confirm:$false }
  121.         if (test-path $global:tempmail".asc" ) { Remove-Item $global:tempmail".asc" -Confirm:$false }
  122.        
  123.         switch ($global:client) {
  124.                 srv {
  125.                         initMail
  126.                         $global:mail.Body = $global:mailbody
  127.                         $global:srv.Send($global:mail)
  128.                         break;
  129.                 }
  130.                
  131.                 ol {
  132.                         initMailol
  133.                         $global:srv.Body = $global:mailbody
  134.                         $global:srv.Send()
  135.                         break;
  136.                 }
  137.         }
  138.  
  139. }

Submit a correction or amendment below (
click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:


Remember me