# Функция формирования тела письма и его отправки # Скрипт позволяет сфорировать письмо с вложением и требуемыми заголовками, с авторизацией на smtp сервере $sys = "C:\Service\Mail" $errlog = "$sys\error.log" $smtpserv = smtp.mail.ru $smtpport = 587 $ssl = $true $user = "user@mail.ru" $pwd = "paSSw0rd" $ecc = "copy@mail.ru" $from = "Служба уведомлений " $subj = "Тема письма" Function SendEmail { Param ( [parameter(Mandatory=$true, ValueFromPipeline=$false)] $addr, [parameter(Mandatory=$true, ValueFromPipeline=$false)] [String] $Body, [parameter(Mandatory=$true, ValueFromPipeline=$false)] $matt ) Begin { $smtp = new-object Net.Mail.SmtpClient $smtpserv, $smtpport $smtp.Enablessl = $ssl $smtp.Credentials = New-Object System.Net.NetworkCredential($user, $pwd) $smtp.Timeout = 10000 } Process { $EmailFrom = $from $EmailCC = $ecc $Subject = $subj $msg = New-Object system.net.mail.mailmessage ForEach ($EmailAddr in $addr) { $msg.To.Add($EmailAddr) } if ($matt) { ForEach($fatt in $matt) { $attach = new-object Net.Mail.Attachment($fatt) $msg.Attachments.Add($attach) Write-Output $fatt | out-file -append -filepath $toprint -Encoding "UTF8" } } else { Break } $msg.CC.Add($EmailCC) $msg.DeliveryNotificationOptions = "OnSuccess" $msg.Priority = "high" $msg.From = $EmailFrom $msg.Subject = $Subject $msg.body = $Body $msg.IsBodyHTML = $true $msg.BodyEncoding = [System.Text.Encoding]::UTF8 $smtp.Send($msg) if ($error) { $erdt = get-date -f "yyyy.MM.dd HH:mm:ss" Write-Output $Body | out-file -append -filepath $errlog -Encoding "UTF8" Write-Output "$erdt $error" | out-file -append -filepath $errlog -Encoding "UTF8" } $error.clear() } } # "asdasd" | ConvertTo-SecureString -AsPlainText -Force | ConvertFrom-SecureString