PoshCode Archive  Artifact [570aff7edf]

Artifact 570aff7edfb423068db867e4f8a61534a4b59cd4222d9fb0e509278863c0aec1:

  • File Sendmail-for-PoSh-2-CTP3.ps1 — part of check-in [3aee1b70a3] at 2018-06-10 14:27:14 on branch trunk — Sending Mails via Powershell with Text Edit, Signature Support and Encryption. (user: Patrick size: 3895)

# encoding: ascii
# api: powershell
# title: Sendmail for PoSh 2 CTP3
# description: Sending Mails via Powershell with Text Edit, Signature Support and Encryption.
# version: 0.1
# type: script
# author: Patrick
# license: CC0
# x-poshcode-id: 937
# x-archived: 2016-05-20T16:15:08
# x-published: 2009-03-12T09:00:00
#
# 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
#
#region vars
$script:maileditor = "C:\Programme\vim\vim72\vim.exe"
$script:encryption = "C:\Programme\GNU\GnuPG\gpg.exe"
$script:enckey = "s.patrick1982@gmail.com"
$script:tempmail = "C:\temp\psmail.txt"
$script:sigmail = "C:\temp\halten\sig.txt"
$script:mailbody = ""
#endregion vars

#region initMail
function initHeader {
	$script:mailTo = ($script:mailTo).Split(',') | % { $_.Trim() }

	if (Test-Path $script:sigmail) {
		(Get-Content $script:sigmail | Out-String) | Out-File $script:tempmail
	}

	& $script:maileditor $script:tempmail

}

function initMail {
	$smtpserver = "smtpserver"
	$myuser = "yoursmtp"
	$mypass = "password"
	$myAddress = "Patrick <s.patrick1982@gmail.com>"
	
	$script:mail = New-Object System.Net.Mail.MailMessage
	$script:srv = New-Object System.Net.Mail.SmtpClient
	$script:srv.Host = $smtpserver
	$script:srv.Credentials = New-Object System.Net.NetworkCredential($myuser, $mypass)
	
	$script:mail.from = $myAddress
	foreach ($rcpt in $script:mailTo) {
		if ($rcpt -ne "") {
			$script:mail.To.Add($rcpt)
		}
	}
	$script:mail.subject = $script:subject
}

function initMailol {
	$script:outlook = New-Object -ComObject Outlook.Application
	$script:srv = New-Object -ComObject Redemption.SafeMailItem
	$script:omail = $outlook.CreateItem("olMailItem")
	$script:srv.Item = $script:omail

	#region check
	#User Input
	foreach ($rcpt in $script:mailTo) {
		$script:srv.Recipients.Add($rcpt) | Out-Null
	}

	#check recipients
	$check = $script:srv.Recipients.ResolveAll()
	
	if ($check -eq $False) {
		for ($i=0; $i -gt $script:sitem.Recipients.Count;$i++) {
			$script:srv.Recipients.Remove($i)
			exit(-1)
		}
	}
	#endregion

	$script:srv.item.Subject = $script:subject

	if ($script:debug -eq $true) {
		Write-Host "Mail an - " $script:sitem.Recipients
		Write-Host "Betreff - " $script:subject
	}
}
#endregion initMail

#region sendmail
function sendmail {
	Param (
		[Parameter(Position=0,Mandatory=$True,HelpMessage="Sending Type (srv,ol)"]
		[string]$script:client=$false,
		[Parameter(Position=1,Mandatory=$True,Helpmessage="Mail To"]
		[string]$script:mailTo=$false,
		[Parameter(Position=2,Mandatory=$True,HelpMessage="Subject"]
		[string]$script:subject=$false,
		[Parameter(Position=3,Mandatory=$false,Helpmessage="Signed/Encrypted"]
		[string]$script:mtype=$false
	)

	initheader($script:mailTo, $script:subject)
	
#region encrypt
	switch ($script:mtype) {
		s {
			& $script:encryption -a -r $script:enckey --clearsign $script:tempmail
			break;
		}
		
		e {
			& $script:encryption -a -r $script:enckey --encrypt $script:tempmail 
			break;
		}
	}
#endregion encrypt

#body
	if ($script:mtype -ne "") {
		$script:mailbody = (Get-Content $script:tempmail".asc" | Out-String)
	} else {
		$script:mailbody = (Get-Content $script:tempmail | Out-String)
	}		
	
#cleaning
	if (test-path $script:tempmail) { Remove-Item $script:tempmail -Confirm:$false }
	if (test-path $script:tempmail".asc" ) { Remove-Item $script:tempmail".asc" -Confirm:$false }
	
	switch ($script:client) {
		srv {
			initMail
			$script:mail.Body = $script:mailbody
 			$script:srv.Send($script:mail)
			break;
		}
		
		ol {
			initMailol
			$script:srv.Body = $script:mailbody
			$script:srv.Send()
			break;
		}
	}

}