- This topic has 1 reply, 2 voices, and was last updated 2 months, 1 week ago by
July 14, 2020 at 10:29 pm #242456ParticipantTopics: 1Replies: 0Points: -6Rank: Member
Hi, I am trying to develop an automation script that will send an Excel file (.xlsx) to our team. However, during my tests, it seems that the attachments are being converted into text file. As this is my first time trying to use PowerShell to send email, I am not sure whether this is due to our SMTP server setting, or this is the Send-MailMessage cmdlet default.
The test command I have been testing so far:
$mailcred = Get-Credential myusername
$myfile = "C:\Temp\testfile.xlsx"
Send-MailMessage -From "email@example.com" -to "firstname.lastname@example.org" -Subject "Test email from PowerShell cmdlet with attachment" -Body "This is a test email" -Attachments $myfile -Credential $mailcred -SmtpServer 'smtpserver.com' -Port 25 -UseSsl
Are there additional steps I need to do first to be able to send binary file attachments using the Send-MailMessage cmdlet? So far, what I have seen mostly examples in the web are attachments with .txt and .csv files.
Thanks for the advise in advance!
- This topic was modified 2 months, 1 week ago by Roose Chua.
July 15, 2020 at 9:50 am #242561ParticipantTopics: 15Replies: 1775Points: 3,214Rank: Community Hero
Roose, welcome to Powershell.org. Please take a moment and read the very first post on top of the list of this forum: Read Me Before Posting! You’ll be Glad You Did!.
When you post code, error messages, sample data or console output format it as code, please.
In the “Text” view you can use the code tags “PRE“, in the “Visual” view you can use the format template “Preformatted“. You can go back edit your post and fix the formatting – you don’t have to create a new one.
Thanks in advance.
The cmdlet doesn’t do any conversions. The code can be used to attach any type of file that I’m aware of just as you would do in a mail client. If the attachment is being stripped, then it’s most likely a SPAM filter or something. The examples of csv and txt are typically supplementing reporting information from Powershell. While you can use HTML to make a nice report email, if there is a list of 100 users, then you would typically attach that as a CSV or TXT.
- You must be logged in to reply to this topic.