Почтовая рассылка с помощью PowerShell c GUI интерфейсом

На дня потребовалось сделать систему почтовой рассылки для маркетинга , можно воспользоваться готовыми решениями, но они стоят денег =)

Интересно , Добро пожаловать под кат:

Сам скрипт

Function Get-FileName($InitialDirectory,$Title)
{   
	$OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog
	$OpenFileDialog.InitialDirectory = $initialDirectory
	$OpenFileDialog.Filter = "All files (*.*)| *.*"
	$OpenFileDialog.Title = $Title
	$OpenFileDialog.ShowDialog() | Out-Null
	$OpenFileDialog.Filename
}
 
Function Check {
	$subject = $richTextBox1.Text -match "\w"
	$addr = Test-Path -LiteralPath $richTextBox2.Text
	$body = Test-Path -LiteralPath $richTextBox3.Text
	if ($subject -and $addr -and $body) {
		$button3.Enabled = $true
	}
	else {
		$button3.Enabled = $false
	}
}
 
Add-Type -Assembly System.Windows.Forms
Add-Type -Assembly System.Drawing
#Generated Form Function
function GenerateForm {
 
#region Generated Form Objects
$form1 = New-Object System.Windows.Forms.Form
$button3 = New-Object System.Windows.Forms.Button
$button2 = New-Object System.Windows.Forms.Button
$richTextBox3 = New-Object System.Windows.Forms.RichTextBox
$button1 = New-Object System.Windows.Forms.Button
$richTextBox2 = New-Object System.Windows.Forms.RichTextBox
$label1 = New-Object System.Windows.Forms.Label
$richTextBox1 = New-Object System.Windows.Forms.RichTextBox
$InitialFormWindowState = New-Object System.Windows.Forms.FormWindowState
#endregion Generated Form Objects
 
#----------------------------------------------
#Generated Event Script Blocks
#----------------------------------------------
#Provide Custom Code for events specified in PrimalForms.
$button3_OnClick= 
{
	$Encoding = [System.Text.Encoding]::UTF8
	$Subject = $richTextBox1.Text
	$Addr = Get-Content $richTextBox2.Text
	$Body = Get-Content $richTextBox3.Text | Out-String
 
	Send-Mailmessage -To $Addr -From "Веселый молочник <bc-newsletter@domain.com>" `
		-Encoding $Encoding -Subject $Subject -Body $Body -BodyAsHTML `
			-Priority High -DeliveryNotificationOption OnSuccess -SMTPServer 10.8.254.7
}
 
$button1_OnClick= 
{
	$richTextBox2.Text = Get-FileName -Title "Файл расслыки адресов в формате txt"
	Check
}
 
$button2_OnClick= 
{
	$richTextBox3.Text = Get-FileName -Title "Файл тела письма в формате html"
	Check
}
 
$handler_label1_Click= 
{
#TODO: Place custom script here
 
}
 
$handler_form1_Load= 
{
#TODO: Place custom script here
 
}
 
$OnLoadForm_StateCorrection=
{#Correct the initial state of the form to prevent the .Net maximized form issue
	$form1.WindowState = $InitialFormWindowState
}
 
#----------------------------------------------
#region Generated Form Code
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 239
$System_Drawing_Size.Width = 512
$form1.ClientSize = $System_Drawing_Size
$form1.DataBindings.DefaultDataSourceUpdateMode = 0
$form1.FormBorderStyle = 2
$form1.Name = "form1"
$form1.Text = "ООО«НДМ» - Cистема рассылки писем"
$form1.add_Load($handler_form1_Load)
 
 
$button3.DataBindings.DefaultDataSourceUpdateMode = 0
$button3.Enabled = $False
$button3.Font = New-Object System.Drawing.Font("Microsoft Sans Serif",14,1,3,0)
 
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 184
$System_Drawing_Point.Y = 194
$button3.Location = $System_Drawing_Point
$button3.Name = "button3"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 33
$System_Drawing_Size.Width = 118
$button3.Size = $System_Drawing_Size
$button3.TabIndex = 6
$button3.Text = "Отправить"
$button3.UseVisualStyleBackColor = $True
$button3.add_Click($button3_OnClick)
 
$form1.Controls.Add($button3)
 
 
$button2.DataBindings.DefaultDataSourceUpdateMode = 0
 
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 412
$System_Drawing_Point.Y = 146
$button2.Location = $System_Drawing_Point
$button2.Name = "button2"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 23
$System_Drawing_Size.Width = 80
$button2.Size = $System_Drawing_Size
$button2.TabIndex = 5
$button2.Text = "Обзор"
$button2.UseVisualStyleBackColor = $True
$button2.add_Click($button2_OnClick)
 
$form1.Controls.Add($button2)
 
$richTextBox3.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 20
$System_Drawing_Point.Y = 148
$richTextBox3.Location = $System_Drawing_Point
$richTextBox3.Multiline = $False
$richTextBox3.Name = "richTextBox3"
$richTextBox3.ReadOnly = $True
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 23
$System_Drawing_Size.Width = 373
$richTextBox3.Size = $System_Drawing_Size
$richTextBox3.TabIndex = 4
$richTextBox3.Text = "Выберите файл для тела письма в формате html"
 
$form1.Controls.Add($richTextBox3)
 
 
$button1.DataBindings.DefaultDataSourceUpdateMode = 0
 
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 412
$System_Drawing_Point.Y = 98
$button1.Location = $System_Drawing_Point
$button1.Name = "button1"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 23
$System_Drawing_Size.Width = 80
$button1.Size = $System_Drawing_Size
$button1.TabIndex = 3
$button1.Text = "Обзор"
$button1.UseVisualStyleBackColor = $True
$button1.add_Click($button1_OnClick)
 
$form1.Controls.Add($button1)
 
$richTextBox2.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 20
$System_Drawing_Point.Y = 98
$richTextBox2.Location = $System_Drawing_Point
$richTextBox2.Multiline = $False
$richTextBox2.Name = "richTextBox2"
$richTextBox2.ReadOnly = $True
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 23
$System_Drawing_Size.Width = 373
$richTextBox2.Size = $System_Drawing_Size
$richTextBox2.TabIndex = 2
$richTextBox2.Text = "Выберите файл для рассылки адресов в формате txt"
 
$form1.Controls.Add($richTextBox2)
 
$label1.DataBindings.DefaultDataSourceUpdateMode = 0
$label1.Font = New-Object System.Drawing.Font("Microsoft Sans Serif",14,1,3,0)
 
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 184
$System_Drawing_Point.Y = 9
$label1.Location = $System_Drawing_Point
$label1.Name = "label1"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 23
$System_Drawing_Size.Width = 118
$label1.Size = $System_Drawing_Size
$label1.TabIndex = 1
$label1.Text = "Тема"
$label1.TextAlign = 32
$label1.add_Click($handler_label1_Click)
 
$form1.Controls.Add($label1)
 
$richTextBox1.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 20
$System_Drawing_Point.Y = 45
$richTextBox1.Location = $System_Drawing_Point
$richTextBox1.Multiline = $False
$richTextBox1.Name = "richTextBox1"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 19
$System_Drawing_Size.Width = 480
$richTextBox1.Size = $System_Drawing_Size
$richTextBox1.TabIndex = 0
$richTextBox1.Text = ""
$richTextBox1.Add_TextChanged({
	Check
})
$form1.Controls.Add($richTextBox1)
 
#endregion Generated Form Code
 
#Save the initial state of the form
$InitialFormWindowState = $form1.WindowState
#Init the OnLoad event to correct the initial state of the form
$form1.add_Load($OnLoadForm_StateCorrection)
#Show the Form
$form1.ShowDialog()| Out-Null
 
} #End Function
 
#Call the Function
GenerateForm

Далее у Вас должно быть два файла и красивая тема письма

1 Файл в формате TXT в столбик почтовые адреса
2 Письмо в формате HTML
3 Указать тему письма

Нажать Send , как кнопка становится активной вся рассылка закончена =)

Спасибо Kazun

Добавить комментарий

Ваш e-mail не будет опубликован. Обязательные поля помечены *