PoshCode Logo PowerShell Code Repository

Sendmail for PoSh 2 CTP3 (modification of post by view diff)
embed code: <script type="text/javascript" src="http://PoshCode.org/embed/937"></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 psm1 and and execute import-module sendmail.psm1. After that simply use sendmail and enter the options

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

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