0

0

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

php中文网

php中文网

发布时间:2016-06-07 16:11:18

|

1421人浏览过

|

来源于php中文网

原创

把creating mail代码写到调用处,往outbox写入 mailxxx.csv文件,入面记录了邮件的主要内容 写入 #template$TMP = IMPORT-CSV $($dh)\mail\TEMPLATES\MAIL.CSV #customization$TMP.CC = # $TMP.Subject =From Server $($( get-wmiobject Win32_ComputerSyste

把creating mail代码写到调用处,往outbox写入 mailxxx.csv文件,入面记录了邮件的主要内容

写入

 

斯摩派(smapie)企业建站程序
斯摩派(smapie)企业建站程序

一个让ASP程序轻松做最少的代码编写量,一般企业所需要的功能都有,参数设置,数据库管理,文件管理,数据初始化,生成HTML页面(这是为了某些客户需要静态页面的需求),页面管理(这里是为了网站中某些单页面需求而开发的,这里你在前台只要用sub_c.article(2) 这个2是这里的id号,也可以是在比如index.asp?id=2 是一样的效果)公告管理,友情链接,信息发布(这里有分类,分类是无限

下载
	#template
			$TMP = IMPORT-CSV "$($dh)\mail\TEMPLATES\MAIL.CSV" 
			#customization
			$TMP.CC ="" #  
			$TMP.Subject ="From Server $($( get-wmiobject Win32_ComputerSystem ).Name) By APOMS_TSQL_Deployment_Package $(get-date)." #  test1          
			$TMP.Body ="$msg`nPlease Check The attachement for details. `nThanks`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
发送,调用send-avmail.ps1发送邮件

 

 

."$($dh)\Mail\Send-AvEmail.ps1" 

sample

 

 

cls;
$dh = $pwd.path
#$PSScriptRoot =Split-Path -Parent $MyInvocation.MyCommand.Definition
$pv = $psversiontable.psversion.major
$strDate = Get-Date -Format "yyyyMMdd"
$logfile = "$($dh)\log\log$($strDate).txt"
$log = {$input|Tee-Object -FilePath $logfile -Append}
$filename = ''
$rem = {$i = '';1..75|%{$i += '-'};$i } 

#getting order
"start deploying `nat $(get-date)"|&$log
"reading configuration file..."|&$log
$content = ( Get-Content "$($dh)\module\config_order"| Out-String )
$hash = ( Invoke-Expression $content )
#$hash 

#sql parameters
$content = ( Get-Content "$($dh)\module\config_sys"| Out-String )
$hashsys = ( Invoke-Expression $content )
&$rem|&$log
try{ 
	#Display
	"order`t type"|&$log
	&$rem|&$log
	1..20|%{
		if($hash["$_"] -ne $null){ 
			"$($_)`t $($hash["$_"])"|&$log
		}
	}
	&$rem|&$log
	"echo The Depoly will start immediately" |&$log
	sleep 1|&$log
	&$rem|&$log
	#hit by order
	1..20|%{
		if($hash["$_"] -ne $null){

			if($dh -ne $pwd.path){
				cd $dh
			}
			$key = $hash["$_"] 
			"........................$key............." |&$log
			$cmd = "$($dh)\module\RunSqlcmdByKey.ps1 "
			$dh|&$log
			Invoke-Expression -Command $cmd |&$log
		}
	} 
}
catch {
	"$(Get-Date) error:$_.Exception"|&$log
}finally{
	"finish deploying at $(get-date)"|&$log
	$title = "Send mail"
	$message = "Do you want to Send the logfile to isoftstone?" 
	$yes = New-Object System.Management.Automation.Host.ChoiceDescription "&Yes", `
	"Send the logfile to isoftstone." 
	$no = New-Object System.Management.Automation.Host.ChoiceDescription "&No", `
	"Continue." 
	$options = [System.Management.Automation.Host.ChoiceDescription[]]($yes, $no) 
	$result = $host.ui.PromptForChoice($title, $message, $options, 1) 
	switch ($result)
	{
		0 {
			#import mail moudle
			"creating attachement"
			Import-Module "$($dh)\mail\MailModule.psm1" -force 
			$t = Get-Date -Format 'yyyyMMdd_hh_mm_ss' 
			."$($dh)\Mail\msg.ps1"
			
			"creating mail"
			$msg ="FYI."
			#template
			$TMP = IMPORT-CSV "$($dh)\mail\TEMPLATES\MAIL.CSV" 
			#customization
			$TMP.CC ="" #  
			$TMP.Subject ="From Server $($( get-wmiobject Win32_ComputerSystem ).Name) By APOMS_TSQL_Deployment_Package $(get-date)." #  test1          
			$TMP.Body ="$msg`nPlease Check The attachement for details. `nThanks`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
	$MailAddress = $tmp.From
	$mail.From = New-Object System.Net.Mail.MailAddress($MailAddress) 

	#set the destination addresses
	$MailtoAddress =$tmp.To
	$mail.To.Add($MailtoAddress)

	#set the CC addresses 
	foreach($cca in $tmp.CC -split ','){
		if($cca -ne "" -and $cca -ne $null){
			$copy =New-Object System.Net.Mail.MailAddress("$cca");
			$mail.CC.Add($copy); 
		}
	}
	#set the content
	$mail.Subject = $tmp.Subject

	$mail.Priority = $tmp.Priority
	$mail.Body = $tmp.Body

	#set the attachments
	foreach($filename in $tmp.attachments -split ','){
		if(Test-Path -Path $filename){
			$attachment = new-Object System.Net.Mail.Attachment($filename)
			$mail.Attachments.Add($attachment)
		}
	}
	#add to array
	return $mail;


}
}
function ConvertFrom-Mail{
 [CmdletBinding()]
param( 
	[System.String]$Path
) 
begin {
	$pop = $pwd 
	cd $Path
}
process {
	[System.Net.Mail.MailMessage[]]$mails = @()

	foreach($f in $(ls -Path $Path -Name "*.csv")){
		$tmp = Import-Csv -Path $f 
		$mail = New-Object System.Net.Mail.MailMessage

		#set the from addresses
		$MailAddress = $tmp.From
		$mail.From = New-Object System.Net.Mail.MailAddress($MailAddress) 

		#set the destination addresses
		$MailtoAddress =$tmp.To
		$mail.To.Add($MailtoAddress)

		#set the CC addresses 
		foreach($cca in $tmp.CC -split ','){
			if($cca -ne "" -and $cca -ne $null){
				$copy =New-Object System.Net.Mail.MailAddress("$cca");
				$mail.CC.Add($copy); 
			}
		}
		#set the content
		$mail.Subject = $tmp.Subject

		$mail.Priority = $tmp.Priority
		$mail.Body = $tmp.Body

		#set the attachments
		foreach($filename in $tmp.attachments -split ','){
			if(Test-Path -Path $filename){
				$attachment = new-Object System.Net.Mail.Attachment($filename)
				$mail.Attachments.Add($attachment)
			}
		}
		#add to array
		$mails += $mail;
	}

	return $mails;
}
end {
	cd $pop
}

}
function Get-Smtp{
 param( 
	[System.String]$Path
)
begin {
	$pop = $pwd
	cd $Path
}
process {
	$param = Import-Csv ".\smtp.csv"
	$smtpServer = $param.Server
	$smtpUser = $param.User
	$smtpPassword = $($param.Password|ConvertTo-SecureString)
	$smtp = New-Object System.Net.Mail.SmtpClient -argumentList $smtpServer
	#$smtp.Credentials = New-Object System.Net.NetworkCredential -argumentList $smtpUser,"Isoft1410"#$smtpPassword
	$smtp.Credentials = New-Object System.Net.NetworkCredential -argumentList $smtpUser, $smtpPassword 
	return $smtp;
}
end {
	cd $pop
}

}

function ConvertTo-Mail{
 [CmdletBinding()]
param(
	$mail,
	[System.String]$Path
)
$mail|Export-Csv -Path $Path
}

Export-ModuleMember -Function ConvertFrom-Mail, ConvertTo-Mail,Get-Smtp, ConvertFrom-MailCsv,import-Zip,export-Zip

相关专题

更多
Word 字间距调整方法汇总
Word 字间距调整方法汇总

本专题整合了Word字间距调整方法,阅读下面的文章了解更详细操作。

2

2025.12.24

任务管理器教程
任务管理器教程

本专题整合了任务管理器相关教程,阅读下面的文章了解更多详细操作。

2

2025.12.24

AppleID格式
AppleID格式

本专题整合了AppleID相关内容,阅读专题下面的文章了解更多详细教程。

0

2025.12.24

csgo视频观看入口合集
csgo视频观看入口合集

本专题整合了csgo观看入口合集,阅读下面的文章了知道更多入口地址。

29

2025.12.24

yandex外贸入口合集
yandex外贸入口合集

本专题汇总了yandex外贸入口地址,阅读下面的文章了解更多内容。

58

2025.12.24

添加脚注通用方法
添加脚注通用方法

本专题整合了添加脚注方法合集,阅读专题下面的文章了解更多内容。

1

2025.12.24

重启电脑教程汇总
重启电脑教程汇总

本专题整合了重启电脑操作教程,阅读下面的文章了解更多详细教程。

3

2025.12.24

纸张尺寸汇总
纸张尺寸汇总

本专题整合了纸张尺寸相关内容,阅读专题下面的文章了解更多内容。

5

2025.12.24

Java Spring Boot 微服务实战
Java Spring Boot 微服务实战

本专题深入讲解 Java Spring Boot 在微服务架构中的应用,内容涵盖服务注册与发现、REST API开发、配置中心、负载均衡、熔断与限流、日志与监控。通过实际项目案例(如电商订单系统),帮助开发者掌握 从单体应用迁移到高可用微服务系统的完整流程与实战能力。

1

2025.12.24

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
Node.js 教程
Node.js 教程

共57课时 | 7.2万人学习

Rust 教程
Rust 教程

共28课时 | 3.8万人学习

Vue 教程
Vue 教程

共42课时 | 5.4万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号