Skip to content

Instantly share code, notes, and snippets.

@taco-shellcode
Created February 27, 2018 15:54
Show Gist options
  • Save taco-shellcode/629fa799b3e1b7c4ec33e8df8dfafbca to your computer and use it in GitHub Desktop.
Save taco-shellcode/629fa799b3e1b7c4ec33e8df8dfafbca to your computer and use it in GitHub Desktop.
function New-EmailTemplate {
param(
[Parameter(Mandatory=$true)]
[Alias('To')]
[String] $toField,
[Alias('Subject')]
[String] $subjectField,
[Parameter(Mandatory=$true)]
[Alias('Body')]
[String] $emailBody,
[Alias('Attachment')]
[System.Net.Mail.Attachment] $emailAttachment,
[Alias('Bcc')]
[String] $bccField,
[Alias('BodyAsHtml')]
[Switch] $isBodyHtml,
[Alias('From')]
[String] $fromField
)
$message = New-Object Net.Mail.MailMessage
if ($isBodyHtml) {
$message.IsBodyHtml = $true
}
$message.To.Add($toField)
$message.Subject = $subjectField
$message.Body = $emailBody
$message.From = $fromField
$message.ReplyTo = $fromField
if (-not($emailAttachment -eq $null)) {
$message.Attachments.Add($emailAttachment)
}
if (-not([String]::IsNullOrWhiteSpace($bccField))) {
$message.Bcc.Add($bccField)
}
return $message
}
function New-InlineAttachment {
param(
[Parameter(Mandatory=$true)]
[Alias('Path')]
[String] $filePath,
[Parameter(Mandatory=$true)]
[Alias('MediaType')]
[String] $attachmentMediaType
)
$attachment = Get-ChildItem $filePath
$mailAttachment = New-Object System.Net.Mail.Attachment -ArgumentList $filePath
$mailAttachment.ContentDisposition.Inline = $True
$mailAttachment.ContentDisposition.DispositionType = 'Inline'
$mailAttachment.ContentType.MediaType = $attachmentMediaType
$mailAttachment.ContentId = $attachment.Name
return $mailAttachment
}
function Send-Email {
param(
[Parameter(Mandatory=$true)]
[Alias('SmtpServer')]
[String] $mailServer,
[Parameter(Mandatory=$true)]
[Alias('Email')]
[Net.Mail.MailMessage] $mailMessage
)
$smtp = New-Object Net.Mail.SmtpClient($mailServer)
$smtp.Send($mailMessage)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment