Powershell Mail module, send all messages in Outbox (A. csv file represents an email)

Source: Internet
Author: User
Tags argumentlist create zip mail code mailmessage

Writes the creating mail code to the call, writes the Mailxxx.csv file to the Outbox, and records the main contents of the message in the face.

Write

#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, call send-avmail.ps1 send mail


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 |& $logtry {#Display "order ' t type" |& $log & $rem |& $log 1. 20|%{if ($hash ["$_"]-ne $null) {"$ ($_) ' t $ ($hash [" $_ "])" |& $log}}& $rem |& $log "echo the Depoly would start Immediately "|& $logsleep 1|& $log & $rem |& $log #hit by Order1. 20|%{if ($hash ["$_"]-ne $null) {if ($dh-ne $pwd. Path) {cd $DH} $key = $hash ["$_"] ".......... $key ................. "| & $log $cmd = "$ ($DH) \module\runsqlcmdbykey.ps1" $dh |& $logInvoke-expression-command $cmd |& $log}}}catch {" $ (get-date) error:$_. Exception "|& $log}finally{" finish deploying at $ (get-date) "|& $log $title =" Send mail "$message =" does you want to Sen D The logfile to iSoftStone? "$yes = New-object System.Management.Automation.Host.ChoiceDescription" &yes ", '" Send th e 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" 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 # Bodytry {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 10foreach ($f in $ (ls-path "$path \outbox\*.csv" |sort-object-propert Y lastwritetime-descending| Select-object-first) {& $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 Successmove-item-path $cureNtfile-destination "$path \sent\$ ($f. Name)" #Tabke a Breaksleep 1}}}catch {#log error message$_|& $log "error occur whe N Sending mail: $curentFile "|& $log #move IT to FOLDER error when Failmove-item-path $curentFile-destination" $path \err or\$ ($f. Name) "}finally {#mark with dateget-date|& $log #popcd $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)]);(d ir $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 Attachmentsforeach ($filename in $tmp. Attachments-split ', ') {if (Test-path-path $filename) {$attachment = New-object System.Net.Mail.Attachment ($filename) $mail. Attachments.Add ($attachment)}} #add to Arrayreturn $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 Attachmentsforeach ($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 = $PWDCD $Path}process {$param = import-csv ". \sm Tp.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

Download

http://download.csdn.net/detail/wangzhpwang/8268477
http://download.csdn.net/detail/wangzhpwang/8268511

Powershell Mail module, send all messages in Outbox (A. csv file represents an email)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.