Sending email via ahk with emoji produces issues
Asked Answered
S

2

7

I use an ahk script to send regular emails that works fine (will post the entire script at end of question). I am now setting up a script that will send HTML formatted emails. The only change you need to make to send HTML formatted emails is changing:

pmsg.TextBody

to:

pmsg.HtmlBody

I have an emoji in the .html file name and the subject:

MailSubject = ✌️ Welcome to Vinewood Studios!
FileRead, MailBody, J:\ONLINE_STORAGE\GOOGLE_DRIVE\Gmail Templates\VSA\VSA-PRE-ORDER\✌️ Welcome to Vinewood Studios!.html

The script reads the html file fine and sends the email. The issue is that wherever there is an emoji or special character, e,g, ™ or •, it produces question marks or symbols. Check out the subject line and footer here:

enter image description here

enter image description here

I tried using the Unicode of the emoji, like this (without the colon):

MailSubject = {U+1F601} "Welcome to Vinewood Studios!"

but it didn't read it at all (email subject says "no subject"): enter image description here

All of these ways won't even send, I get an error about "Missing "key" in object literal.: (I understand WHY I get an error, because {U+1F601} is not being seen as a "value" but rather a string. However, this is how it's used when you want to replace text with an emoji so I don't know why it would be different in this case)

MailSubject := {U+1F601} "Welcome to Vinewood Studios!"
MailSubject := {U+1F601} . "Welcome to Vinewood Studios!"

emoji := {U+1F601}️
MailSubject := emoji . "Welcome to Vinewood Studios!"

When using :=

MailSubject := ✌️ Welcome to Vinewood Studios!

The email subject just reads the number "1".

When testing a simple msgbox with the Unicode of the emoji and pasting to Google, it works fine:

enter image description here

I also can't send Hebrew HTML files, comes out Gibrrish:

enter image description here

Any ideas on how to send an email with emojis and special characters?

UPDATE: adding pmsg.BodyPart.Charset := "utf-8" solved the issue in sending the correct emoji in the subject line. Didn't work to display Hebrew chars in the email body.

Here's the script:

MailSubject := ✌️ Welcome to Vinewood Studios!
FileRead, MailBody, J:\ONLINE_STORAGE\GOOGLE_DRIVE\Gmail Templates\VSA\VSA-PRE-ORDER\✌️ Welcome to Vinewood Studios!.html

SendEmail(MailBody, MailSubject)

SendEmail(MailBody, MailSubject, SendTo:="[email protected]")
{
pmsg := ComObjCreate("CDO.Message")
pmsg.From := """VSA Management"" <[email protected]>"
pmsg.To := SendTo
pmsg.BCC := "" ; Blind Carbon Copy, Invisable for all, same syntax as CC
pmsg.CC := ""
pmsg.Subject := MailSubject
pmsg.HtmlBody := MailBody
pmsg.BodyPart.Charset := "utf-8" ;Displays emoji correctly in subject only
;sAttach := ; "Path_Of_Attachment" ; can add multiple attachments
                                   ; the delimiter is |
fields := Object()
fields.smtpserver := "smtp.gmail.com" ; specify your SMTP server
fields.smtpserverport := 465 ; 25
fields.smtpusessl := True ; False
fields.sendusing := 2 ; cdoSendUsingPort
fields.smtpauthenticate := 1 ; cdoBasic
fields.sendusername := "[email protected]"
fields.sendpassword := "PASSWORD"
fields.smtpconnectiontimeout := 60
schema := "http://schemas.microsoft.com/cdo/configuration/"
pfld := pmsg.Configuration.Fields

For field,value in fields
pfld.Item(schema . field) := value
pfld.Update()

Loop, Parse, sAttach, |, %A_Space%%A_Tab%
pmsg.AddAttachment(A_LoopField)
pmsg.Send()

msgbox, Email sent.
}
Slovenly answered 24/1, 2021 at 11:3 Comment(4)
Whats your command to create the email itself inside Gmail?Angelitaangell
I'll edit the question and add the entire ahk script that gets the HTML from a file and sends the email.Slovenly
There are 3 code boxes here that appear without the quotes around the MailSubject assignment, like @0x464e suggested. Can you update your code? Mainly because it leaves me wondering if that idea was understood.Eject
@Eject My original script was without quotes (MailSubject = ✌️ Welcome to Vinewood Studios!) I only started trying with quotes when nothing else was working. Also, using [pmsg.BodyPart.Charset := "utf-8"] (as 0x464e wrote) solved the emoji in the subject (even with quotes) and I'm just stuck with trying to display Hebrew characters.Slovenly
W
4

This is maybe not necessarily going to be a full answer, but it's too long to be a comment and there's a few issues that need to be fixed with your script.
I don't have/use a gmail SMTP server so I can't test.

So onto the problems.
Out of all your attempts to specify a string containing an emoji, this is only correct one:
MailSubject = ✌️ Welcome to Vinewood Studios!
I italicized correct, because it's legacy syntax, in modern expression syntax, it should look like this:
MailSubject := "✌️ Welcome to Vinewood Studios!"
This is going to be correct, if you save your script file with encoding that supports the Unicode character (your emoji) and run the script with an Unicode AHK version (you should be fine on these fronts though, since you managed to get an AHK messagebox to display that emoji character).

Here's what wrong with your incorrect attempts:


MailSubject = {U+1F601} "Welcome to Vinewood Studios!" Still in legacy syntax. All of that is going to be interpreted as a literal string. AHK sees nothing special in this string.


MailSubject := {U+1F601} "Welcome to Vinewood Studios!"
MailSubject := {U+1F601} . "Welcome to Vinewood Studios!"
These two are equal (the concatenation operator . is optional/redundant), but they also equally wrong.
The {U+...} Unicode character notation is supposed to be treated as just a normal string, nothing more special than that. A supported AHK command when then notice it and translate it to the corresponding Unicode character.
Right now what you're trying to do is concatenate some incorrectly defined (hence your script error) object with a string.
The correct would be
MailSubject := "{U+1F601} Welcome to Vinewood Studios!"
However, as you maybe have noticed, I mentioned that only a supported AHK command would care about the {U+...} notation.
That notation is meant for send commands(docs), so it's not going to be of any help for you. Your email subject would just read the literal string {U+1F601} Welcome to Vinewood Studios!.
If you wanted to store the Unicode character in your script without typing out the Unicode character (useful so you wouldn't have to worry about retaining the correct encoding on your script file always when trying to share it or whatnot), you could use e.g Chr()(docs) like so:
MailSubject := Chr(0x1F601) " Welcome to Vinewood Studios!".


emoji := {U+1F601}️
MailSubject := emoji . "Welcome to Vinewood Studios!"
And here have the some problems as described above.
If you used emoji := "{U+1F601}️", it would be correct (except for a missing space between the emoji and the word Welcome) if used on a supported AHK command (send command).


MailSubject := ✌️ Welcome to Vinewood Studios!
Here you're in modern expression syntax (as you should be), but you're not using quotes to indicate you're specifying a string.
So what you're doing here is concatenating five empty variables (named ✌️, Welcome, to, Vinewood and Studios) together, which leaves you out with nothing, and then you're inverting that value with the logical-NOT operator !(docs).
Nothing evaluates to false, and then inverting false evaluates to true (1).


Ok, so hopefully you now might be able to understand what went wrong with your attempts.
But as said above, the MailSubject = ✌️ Welcome to Vinewood Studios! attempt should've worked? (At least ss far as I can tell, I can only assume you saved your script with a supporting encoding and ran the script with an Unicode AHK version).

More than likely what's going wrong, is that you have to specify a supporting encoding for your CDO.Message object.
A Google search for CDO.Message encoding yields quite a few results for people having this problem (e.g this or this) And there are varied solutions people are applying. Most common of which seems to be specifying a field named charset to utf-8.

I can't test the actual solution, but I hope this helps.
If you still have trouble understanding some of the AHK code, I can answer those questions.

Willett answered 24/1, 2021 at 20:57 Comment(2)
pmsg.BodyPart.Charset := "utf-8" worked in sending the correct emoji in the subject line. Didn't work to display Hebrew chars in the email body. pmsg.HtmlBodyPart.Charset and pmsg.TextBodyPart.Charset didn't help either (not even a combination of them). But I'm now certain that it's a ComObjCreate issue as you mentioned and that's what I'm researching now.Slovenly
p.s. the HTML file itself is already saved as utf-8Slovenly
X
0

To use Unicode, make sure your AHK files are saved with the UTF-8 BOM encoding.

Quote from Why are the non-ASCII characters in my script displaying or sending incorrectly?:

Although AutoHotkey supports Unicode text, it is optimized for backward compatibility, which means defaulting to the ANSI encoding rather than the more internationally recommended UTF-8. AutoHotkey will not automatically recognize a UTF-8 file unless it begins with a byte order mark.

Xanthe answered 19/9, 2021 at 13:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.