Skip to content

Instantly share code, notes, and snippets.

@nordineb
Last active March 15, 2018 15:39
Show Gist options
  • Save nordineb/5bd055993020269401a3499aac83bccd to your computer and use it in GitHub Desktop.
Save nordineb/5bd055993020269401a3499aac83bccd to your computer and use it in GitHub Desktop.
Sending email with a terminal

Sending emails with powershell

Securely send emails with attachments

$credential = Get-Credential
[System.Net.ServicePointManager]::ServerCertificateValidationCallback = { return $true }

Send-MailMessage `
-SmtpServer 172.XXX.XXX.XXX `
-port 587 `
-UseSsl `
-Credential $credential `
-From "Nordine Ben Bachir <[email protected]>" `
-To "xxxxxx" `
-Bcc "xxxxxxxxxx"`
-Subject "Don t forget today s meeting!" `
-Body "Test email with Ssl (port58)7" 
-Attachments @(Get-ChildItem  "C:\nordinetest\" | % { $_.FullName })

Swiss Army Knife for SMTP (swaks)

All attachment will have the mimetype application/octet-stream

$ swaks -s "${smtpserver}" -p "${smtpport}" -t "$to" -f "$from" --header "Subject: $subject" -S \
      --protocol ESMTP -a -au "$user" -ap "$password" --body "$body" \
      --attach foo.pdf  --attach bar.jpg

Or manually specifiy MIME type

$ swaks -s "${smtpserver}" -p "${smtpport}" -t "$to" -f "$from" --header "Subject: $subject" -S \
      --protocol ESMTP -a -au "$user" -ap "$password" --body "$body" \
      --attach-type "$(get_mimetype foo.pdf)" --attach foo.pdf \
      --attach-type "$(get_mimetype bar.jpg)" --attach bar.jpg

the mimetype MUST be before the file name.

ToDo

  • To do SSL/TLS, see the various --tls* options
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment