-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpwsh-zerosmtp.ps1
More file actions
75 lines (65 loc) · 2.17 KB
/
Copy pathpwsh-zerosmtp.ps1
File metadata and controls
75 lines (65 loc) · 2.17 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
# pwsh-zerosmtp.ps1
# PowerShell 7.5+ Send-MailKitMessage 2.2 - ZeroSMTP mx.msgwing.com:587 STARTTLS
# Production-ready | Let's Encrypt | PSCredential, params block
#Requires -Version 7.5
#Requires -Module Send-MailKitMessage
param(
[Parameter(Mandatory=$false)]
[string]$Username = $env:USERNAME,
[Parameter(Mandatory=$false)]
[string]$Password = $env:PASSWORD,
[Parameter(Mandatory=$false)]
[string]$From = $env:FROM,
[Parameter(Mandatory=$false)]
[string]$To = $env:TO,
[Parameter(Mandatory=$false)]
[string]$Subject = $env:SUBJECT,
[Parameter(Mandatory=$false)]
[string]$HtmlBody = @"
<html><body><h1>Hello from ZeroSMTP!</h1><p>This is an HTML email sent via mx.msgwing.com:587 STARTTLS</p></body></html>
"@
)
# Defaults
if (-not $Username) { $Username = 'your-username' }
if (-not $Password) { $Password = 'your-password' }
if (-not $From) { $From = 'sender@example.com' }
if (-not $To) { $To = 'recipient@example.com' }
if (-not $Subject) { $Subject = 'Test Email from ZeroSMTP' }
# Create PSCredential
$credential = [PSCredential]::new(
$Username,
(ConvertTo-SecureString $Password -AsPlainText -Force)
)
try {
# Prepare parameters for Send-MailKitMessage
$sendMailKitSplat = @{
SMTPServer = 'mx.msgwing.com'
Port = 587
UseSsl = $false # STARTTLS requires $false
Credential = $credential
From = $From
To = $To
Subject = $Subject
TextBody = 'Hello from ZeroSMTP! This is plain text.'
HtmlBody = $HtmlBody
BodyEncoding = [System.Text.Encoding]::UTF8
ErrorAction = 'Stop'
Verbose = $false
}
# Send email
Send-MailKitMessage @sendMailKitSplat
Write-Host "Email sent successfully via ZeroSMTP" -ForegroundColor Green
exit 0
}
catch [System.Net.Mail.SmtpException] {
Write-Error "SMTP error: $_" -ErrorAction Continue
exit 1
}
catch [System.UnauthorizedAccessException] {
Write-Error "Authentication failed: $_" -ErrorAction Continue
exit 1
}
catch {
Write-Error "Unexpected error: $_" -ErrorAction Continue
exit 1
}