-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmailsender.ps1
More file actions
75 lines (63 loc) · 2.53 KB
/
mailsender.ps1
File metadata and controls
75 lines (63 loc) · 2.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# Basic script to send a singular email with an attachment using SMTP2GO to a predefined recipient
#
# Written by Jonathan Bullock
# 2024 - 04 - 03
# Load assemblies
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
# SMTP2go configuration
$smtpServer = "mail.smtp2go.com"
$smtpPort = 2525
$smtpUser = "smtp2go-username"
$smtpPass = "smtp2go-password"
$fromEmail = "sender@domain.com" # SMTP2go email
$toEmail = "recipient@domain.com" # Pre-defined recipient email
# Create the actual Form
$form = New-Object System.Windows.Forms.Form
$form.Text = "Send File via Email"
$form.Size = New-Object System.Drawing.Size(400,200)
$form.StartPosition = "CenterScreen"
# Label
$label = New-Object System.Windows.Forms.Label
$label.Text = "Drop a file below and click Send"
$label.Location = New-Object System.Drawing.Point(10,10)
$label.Size = New-Object System.Drawing.Size(280,20)
$form.Controls.Add($label)
# Add a text box for file path display (read-only)
$textBox = New-Object System.Windows.Forms.TextBox
$textBox.Location = New-Object System.Drawing.Point(10,40)
$textBox.Size = New-Object System.Drawing.Size(360,20)
$textBox.ReadOnly = $true
$form.Controls.Add($textBox)
# Add a send button
$sendButton = New-Object System.Windows.Forms.Button
$sendButton.Location = New-Object System.Drawing.Point(10,70)
$sendButton.Size = New-Object System.Drawing.Size(75,23)
$sendButton.Text = "Send"
$form.Controls.Add($sendButton)
# File drop / attach
$form.AllowDrop = $true
$form.Add_DragEnter({
if ($_.Data.GetDataPresent([Windows.Forms.DataFormats]::FileDrop)) {
$_.Effect = [Windows.Forms.DragDropEffects]::Copy
}
})
$form.Add_DragDrop({
$files = $_.Data.GetData([Windows.Forms.DataFormats]::FileDrop)
$textBox.Text = $files[0] # Assuming only one file is dropped
})
# Send button click event
$sendButton.Add_Click({
$attachment = $textBox.Text
if (Test-Path $attachment) {
$credentials = New-Object System.Management.Automation.PSCredential -ArgumentList $smtpUser, (ConvertTo-SecureString $smtpPass -AsPlainText -Force)
# Sending email
Send-MailMessage -From $fromEmail -To $toEmail -Subject "File sent via PowerShell" -Body "See attached file." -Attachments $attachment -SmtpServer $smtpServer -Port $smtpPort -Credential $credentials -UseSsl
[System.Windows.Forms.MessageBox]::Show("Email Sent Successfully", "Success")
}
else {
[System.Windows.Forms.MessageBox]::Show("Please drop a file first.", "Error")
}
})
# Show the Form
$form.ShowDialog()