Spaces:
Running
Running
| ```powershell | |
| <# | |
| .SYNOPSIS | |
| Automated installation and configuration script for hMailServer | |
| .DESCRIPTION | |
| This PowerShell script automates the installation and initial configuration of hMailServer, | |
| including creating a domain, user account, and configuring SMTP settings with SSL/TLS. | |
| .NOTES | |
| File Name : install-hmailserver.ps1 | |
| Prerequisite : PowerShell 5.1+, Windows 10/11 or Windows Server 2016+ | |
| #> | |
| # 1. Download the latest version of hMailServer from official website | |
| Write-Host "Downloading latest hMailServer installer..." | |
| $downloadUrl = "https://www.hmailserver.com/download" | |
| $tempPath = $env:TEMP | |
| $installerPath = "$tempPath\hmailserver-latest.exe" | |
| try { | |
| # Get the latest download URL from the website (this may need adjustment if they change their structure) | |
| $response = Invoke-WebRequest -Uri $downloadUrl -UseBasicParsing | |
| $downloadLink = ($response.Links | Where-Object { $_.href -like "*hMailServer*" -and $_.href -match "\.exe$" } | Select-Object -First 1).href | |
| if (-not $downloadLink) { | |
| throw "Could not find download link on hMailServer website" | |
| } | |
| # Complete the URL if it's relative | |
| if (-not $downloadLink.StartsWith("http")) { | |
| $downloadLink = "https://www.hmailserver.com$downloadLink" | |
| } | |
| Invoke-WebRequest -Uri $downloadLink -OutFile $installerPath -UseBasicParsing | |
| Write-Host "Download completed successfully." | |
| } | |
| catch { | |
| Write-Host "Error downloading hMailServer: $_" -ForegroundColor Red | |
| exit 1 | |
| } | |
| # 2. Install hMailServer silently with default settings | |
| Write-Host "Installing hMailServer..." | |
| try { | |
| $installArgs = "/VERYSILENT /SUPPRESSMSGBOXES /NORESTART /SP- /LOG=`"$tempPath\hmailserver_install.log`"" | |
| Start-Process -FilePath $installerPath -ArgumentList $installArgs -Wait | |
| # Wait for service to be installed and started | |
| Start-Sleep -Seconds 10 | |
| Write-Host "Installation completed successfully." | |
| } | |
| catch { | |
| Write-Host "Error installing hMailServer: $_" -ForegroundColor Red | |
| exit 1 | |
| } | |
| # 3-7. Configure hMailServer using COM automation | |
| Write-Host "Configuring hMailServer..." | |
| try { | |
| # Load hMailServer COM objects | |
| $hMailApp = New-Object -ComObject hMailServer.Application | |
| # Authenticate with the hMailServer administrator password (blank by default after install) | |
| $hMailApp.Authenticate("Administrator", "") | |
| # 3. Create a new domain example.com | |
| $domain = $hMailApp.Domains.Add() | |
| $domain.Name = "example.com" | |
| $domain.Active = $true | |
| $domain.Save() | |
| Write-Host "Created domain: example.com" | |
| # 4. Add a user test@example.com with password "Test1234" | |
| $account = $domain.Accounts.Add() | |
| $account.Address = "test@example.com" | |
| $account.Password = "Test1234" | |
| $account.Active = $true | |
| $account.MaxSize = 100 # 100MB mailbox size | |
| $account.Save() | |
| Write-Host "Created user: test@example.com" | |
| # 5. Configure SMTP to allow sending emails on port 587 with SSL/TLS | |
| $tcpPort = $hMailApp.Settings.TCPIPPorts.Add() | |
| $tcpPort.Address = "0.0.0.0" # Listen on all interfaces | |
| $tcpPort.PortNumber = 587 | |
| $tcpPort.Protocol = 1 # SMTP | |
| $tcpPort.ConnectionSecurity = 2 # SSL/TLS | |
| $tcpPort.Save() | |
| # Enable SMTP authentication | |
| $hMailApp.Settings.SMTP.EnableAUTH = $true | |
| # 6. Enable logging for SMTP activity | |
| $hMailApp.Settings.Logging.Enabled = $true | |
| $hMailApp.Settings.Logging.LogSMTP = $true | |
| # Save all settings | |
| $hMailApp.Settings.Save() | |
| Write-Host "SMTP configured on port 587 with SSL/TLS and logging enabled." | |
| # Restart hMailServer service to apply all changes | |
| Restart-Service -Name "hMailServer" -Force | |
| Write-Host "hMailServer service restarted to apply changes." | |
| } | |
| catch { | |
| Write-Host "Error configuring hMailServer: $_" -ForegroundColor Red | |
| exit 1 | |
| } | |
| Write-Host "`nInstallation and configuration completed successfully!" -ForegroundColor Green | |
| Write-Host "hMailServer is now running with:" -ForegroundColor Cyan | |
| Write-Host "- Domain: example.com" -ForegroundColor Cyan | |
| Write-Host "- User: test@example.com (password: Test1234)" -ForegroundColor Cyan | |
| Write-Host "- SMTP on port 587 with SSL/TLS" -ForegroundColor Cyan | |
| Write-Host "- SMTP logging enabled" -ForegroundColor Cyan | |
| ``` | |
| ___METADATA_START___ | |
| {"isNew":true,"userName":"testng"} | |
| ___METADATA_END___ |