PowershellMailmodule,发送outbox里的所有邮件(一个.csv文件代表一封邮件)(二)

2015-02-03 04:25:19 · 作者: · 浏览: 67
nBest Regards." # test ##clear if(Test-Path "$logfile.zip" ){ Remove-Item -Path "$logfile.zip" -Force|&$log } #create zip "zipping log.." export-Zip -sourcefiles "$logfile" -zipfilename "$logfile.zip"|&$log #add attachements $TMP.Attachments ="$logfile.zip,$($dh)\Mail\msg.txt" # #wirte-outbox,output mailxxx.csv ConvertTo-Mail -path "$($dh)\mail\outbox\MAIL$t.CSV" -mail $tmp #start sending "praparing to send" sleep 10 "sending email..." ."$($dh)\Mail\Send-AvEmail.ps1" |&$log } 1 {} } } send-avmail.ps1

?

#cls;
#paths
$dh = $pwd.path
$pv = $psversiontable.psversion.major
$strDate = Get-Date -Format "yyyyMMdd"
#log
$log_file = "$($dh)\mail\log\log$($strDate).txt"
$log = {$input|Tee-Object -FilePath $log_file -Append}
$filename = ''
$rem = {$i = '';1..75|%{$i += '-'};$i} 

################################START######################################

#remember
$curentFile = ''
$pop = $pwd
$path = "$($dh)\Mail"
CD $path
#body
try {
	if(Test-Path -Path $path ){ 
		Import-Module "$path\MailModule.psm1" -force 
		$t = Get-Date -Format 'yyyyMMdd_hh_mm_ss' 
		$smtp = Get-Smtp -Path $path 
		Get-Date|&$log
		#MAX 10
		foreach($f in $(ls -Path "$path\outbox\*.csv"|sort-object -Property LastWriteTime -Descending|Select-Object -First 10)){
			&$rem|&$log
			$curentFile = "$($f.fullname)" 
			$curentFile|&$log
			#ONE CSV FILE PRESENT ON MAIL MESSAGE.
			$email = ConvertFrom-MailCsv -Path $curentFile
			"$($email.subject) is sending.."  
			#SEND IT
			$smtp.send($email) 
			"$($email.subject) is sent" 
			#MOVE IT TO FOLDER SENT WHEN SUCCESS
			Move-Item -Path $curentFile -Destination "$path\sent\$($f.name)"
			#Tabke a break
			sleep 1
		} 
	}
}
catch { 
	#log error message
	$_|&$log
	"error occur when sending mail:$curentFile"|&$log
	#MOVE IT TO FOLDER error WHEN fail
	Move-Item -Path $curentFile -Destination "$path\error\$($f.name)"
}
finally {
	#mark with date
	Get-Date|&$log
	#pop
	cd $pop
}

?

mailmodule.psm1

?

function import-Zip
{
 param([string]$zipfilename, [string] $destination)

if(test-path($zipfilename))
{
	$shellApplication = new-object -com shell.application
	$zipPackage = $shellApplication.NameSpace($zipfilename)
	$destinationFolder = $shellApplication.NameSpace($destination)
	$destinationFolder.CopyHere($zipPackage.Items())
}
}


function export-Zip
{
 param([string]$sourcefiles, [string]$zipfilename)

dir $sourcefiles | foreach-object {
	if(!$zipfilename) {
		$zipfile = $_.FullName + ".zip";
	}
	else {
		$zipfile = $zipfilename;
	}

	if(!(test-path($zipfile))) {
		set-content $zipfile ("PK" + [char]5 + [char]6 + ("$([char]0)" * 18));
		(			dir $zipfile).IsReadOnly = $false;
	}

	$shellApplication = new-object -com shell.application;
	$zipPackage = $shellApplication.NameSpace($zipfile);
	$zipPackage.CopyHere(($_.FullName));
}
}


function ConvertFrom-MailCsv{
 [CmdletBinding()]
param( 
	[System.String]$Path
) 

$Path = "$Path"
if(Test-Path -Path $Path){
	$tmp = Import-Csv -Path $Path
	$mail = New-Object System.Net.Mail.MailMessage

	#set the from addresses
	$MailAddr