Sent email converts unicode characters into mojibake - powershell

I have a mail script sending automated messages to agents about tickets, but Swedish extended characters are being garbled in the script. The text is garbled even when I send it to console instead of emailing.
My research indicates that strings in PowerShell are in UTF16, so I expected things to be preserved... alas, mojibake.
I started with Net.Mail.SmtpClient and that didn't work. I switched to Send-MailMessage because of the -Encoding parameter, but that doesn't change anything for me. I've messed around with the $OutputEncoding variable in case that had something to do with something, but no luck.
So, for example (assuming this doesn't get mangled here either):
The extended characters from the Sweedish alphabet are ÅÄÖåäö
And they get rendered as ÅÄÖåäö
#$encod = ([System.Text.Encoding]::UTF8);
$encod = ([System.Text.Encoding]::Unicode);
$OutputEncoding = $encod;
[string]$message = 'ÅÄÖåäö';
$emailFrom = "Example <EXAMPLE#SAMPLE.COM>";
$emailTo = "Test <TEST#SAMPLE.COM)";
$subject="Långtidsspara";
$smtpserver="smtp.SAMPLE.COM";
#$smtp=New-Object Net.Mail.SmtpClient($smtpServer);
#$smtp.Send($emailFrom, $emailTo, $subject, $message) ;
Send-MailMessage -Body $message -Encoding $encod -From $emailFrom -SmtpServer $smtpserver -Subject $subject -To $emailTo
Edited to add: Previously when I was trying the Net.Mail.SmtpClient() I was doing the following:
$smtp=New-Object Net.Mail.SmtpClient($smtpServer);
$smtp.Send($emailFrom, $emailTo, $subject, $message);
but I couldn't find a way to adjust the encoding.
EDIT 2: thanks to #RemyLebeau I have tried setting the message up as a MailMessage object and using SmtpClient to send that, but no change in behavior.
$smtp=New-Object Net.Mail.SmtpClient($smtpServer);
$mail=New-Object Net.Mail.MailMessage($emailFrom, $emailTo, $subject, $message);
$mail.BodyEncoding = $encod;
$mail.HeadersEncoding = $encod;
$mail.SubjectEncoding = $encod;
$mail.BodyTransferEncoding = [System.Net.Mime.TransferEncoding]::EightBit;
$smtp.SendMailAsync($mail);
EDIT 3: After correcting from UTF16 to UTF8, my email headers say the following
Subject: =?utf-8?B?SW5zcGVsbmluZ3NkYXRhIChMw4PCpW5ndGlkc3NwYXJhKQ==?=
Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: 8bit
But it's all still garbled. I've got Outlook 365 here, but I get emails from Europe all the time, including previous emails from a customer with Å in their name, and Outlook seems to cope?

Related

Adding a variable to a -Body when sending an email

I'm creating a PowerShell script to make our starters and leavers process smoother. We have a separate team who needs to add certain accounts.
What I'd like to do is take the variable that is declared at the start of the script, the new users name, and put it in an email asking for this other department to set them up.
$username = "joe"
Send-MailMessage -SmtpServer smtp.office365.com -From "it#support.com" -To "other#department.com" -Subject 'Starter/Leaver ' -Body "Hi department, `n `nPlease can you add/remove" $username "from the necessary account please `n `nThanks"
I get an error saying:
Send-MailMessage : A positional parameter cannot be found that accepts argument "joe"
The issue here is that the string object sent to -Body is broken because of the quoting. You can just surround the entire body with one set of quotes to achieve the desired result.
$username = "joe"
Send-MailMessage -SmtpServer smtp.office365.com -From "it#support.com" -To "other#department.com" -Subject 'Starter/Leaver ' -Body "Hi department, `n`nPlease can you add/remove $username from the necessary account please `n`nThanks"
Neater Alternative:
I know this answer is not as concise, but it is more readable and adds some flexibility. It uses a here-string to create the body, which doesn't require having to add all of the line feed characters. It also uses splatting to execute the command. With splatting, you can just update the hash table when you need to change something.
$Body = #"
Hi department,
Please can you add/remove $username from the necessary account please
Thanks
"#
$Message = #{ SmtpServer = 'smtp.office365.com'
From = "it#support.com"
To = "other#department.com"
Subject = 'Starter/Leaver'
Body = $Body
}
Send-MailMessage #Message
When running a PowerShell command, parameters are space-delimited. In the case of Send-MailMessage positional parameters are also allowed, which means you don't have to provide the parameter name when passing in a value. In your attempt, your first quote pair was passed to the -Body parameter. After the first closing quote, a space followed by $username is interpreted. Because positional parameters are enabled for the command, PowerShell attempts to assign $username to a parameter and fails. Of course this also means that if you intend to include a space in your string, it must be surrounded by quotes.
Additional Reading:
See About Parameters for an overview of how parameters work.
See About Splatting for information on splatting.

How to fix encoding with send-MailMessage in powerShell?

I'm trying to send email message from a powershell script but in the email all accented characters (ó, é etc) get scrambled.
What I'm doing is the following:
I use Import-CSV to import an ANSI encoded file.
foreach($item in $inputfile) send-MailMessage -Body $item.body -bodyAsHTML
I'm not a PC-user and the whole encoding thingy gives me headache.
I tried all the different encoding options of send-MailMessage without getting the expected result.

Define To: field from from various values

In a CSV file I have various columns and two of those are for emails. Each row within these email columns have a group of emails.
I want to be able to send to these email addresses from a script.
I have everything setup and working, except for the TO:.
The idea of the script is that it loops each line of the csv and generates an email grabbing values from the cells of that row into various parts of the body. Then it sends of an email and loops back to the next line of the CSV to do the same, and so on until it reaches the end of the CSV file.
I'm having issues to plug a variable for the email columns, I'm guessing because the emails don't have "quotations".
How do I bring these in?
In a nutshell for the code
data is imported CSV
a loop is created foreach line of imported data
smtp, from, to, subject, attachments, body variables are defined
then the sendmail-message command is provided.
close the loop
##For the purpose of this, the emaildata.csv looks like this sample:
"NameGroup","emailGroupA","emailGroupB"
"Groupabc","a.b#b.com;c.a#b.com","xyv#b.com;xxd#b,com"
"Grouptrd","ca.r#b.com;as.b#b.com","aaa#a.com;bbb#b.com"
"Groupghd","dd.r#b.com;dd.b#b.com","dddaa#a.com;ddddddb#b.com"
$DataDir = "C:\Users\acastrellon\Documents"
$Data= import-csv $DataDir\emaildata.csv
foreach ($i in $Data) {
$NameGroup = $i.NameGroup
$TeamA = $i.emailGroupA.replace(';',"`n")
$TeamB = $i.emailGroupB.replace(';',"`n")
function send-email {
$smtpserver = " server.smtp"
$from = "myemail.com"
$to = $TeamA,$TeamB
send-MailMessage -From $from -To $to -Subject $subject -Body $body -SmtpServer $smtpServer
}
[string] $subject = "some text here: $NameGroup"
#[string] $attachment = "not here yet"
[string] $body = "
Text here that calls out $NameGroup
This also lists: $TeamA
This here lists $TeamB
Done"
send-email -subject $subject -attachment $attachment -body $body
}
#this should loop to get data from next line of csv and generate a new email with data.
Unlike a batch file where environment variables have a global impact to the current session, PowerShell isolates variables in different scopes.
You're referencing the two $TeamA & $TeamB variables inside a function but you set their values outside the function. Because the function scope (where they are read) is different to the script scope (where you set them) those variables will be empty inside the send-email function in your script.
Have a read on PowerShell scopes as you'll need to make some changes to your script functions; to either read the variables from the script scope ($script:TeamA) or to pass them into the function as a parameter

How to attach files to a Powershell function and send it through MS Outlook?

I've got this code:
Function Mailer ($MSubject, $MBody, $File){
$Outlook = New-Object -ComObject Outlook.Application
$Mail = $Outlook.CreateItem(0)
$Mail.To = "abc#contoso.com"
$Mail.Subject = $MSubject
$Mail.Body = $MBody
$Mail.Attachments.Add($File)
$Mail.Send()
}
I will work if I provide the exact path in the function by assigning it to the $File variable. However, I wish to make this universal for different subjects, bodies and paths. Shall I set the file path as global? What are your thoughts?
Thanks in advance :)
Piotr
This is the code I use to send emails out,
$from="ServerAdmin#Contoso.com"
$to="worker#contoso.com"
$cc=#("John.Doe#Contoso.com", "Jane.Doe#Contoso.com")
$subject="Weekly reports"
$attach="c:\SchTsk\Reports\Server_Inventory_$((Get-Date).ToString('MM-dd-yyyy')).csv","C:\SchTsk\Reports\VMPlatform_$((Get-Date).ToString('MM-dd-yyyy')).csv"
$body="This email contains weekly reports ran on the domain.<br>In an effort to reduce inbox spam, reports that generate separate files are now being attached to one weekly email."
Send-MailMessage -from $from -To $to -cc $cc -subject $subject -SmtpServer snmp.relay.contoso.com -Body $body -BodyAsHtml -Attachments $attach
Actually the function works fine. The problem was with the variable for the file path I was calling to.

Send-MailMessage -From - bad formatting

Quite simple question. I'm sending an email with send-mailmessage, which works fine. But i'm not entirely happy with the format of the sender.
Send-MailMessage -to "User01 <User01#example.com>" -from "USER02 <USER02#example.com>" -subject "Test mail" -SmtpServer 'mysmtp'
When receiving the Mail (IBM Lotus Notes) the sender is shown as "user02" in lower case, although i'm running the command with the sender in upper case - as shown above.
Is there by purpose any simple way to solve my little problem? Should be shown as "USER02" not as "user02". Reason of Powershell or cause of Lotus Notes?