Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
584 views
in Technique[技术] by (71.8m points)

autohotkey - Sending email via ahk with emoji produces issues

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_STORAGEGOOGLE_DRIVEGmail TemplatesVSAVSA-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_STORAGEGOOGLE_DRIVEGmail TemplatesVSAVSA-PRE-ORDER?? Welcome to Vinewood Studios!.html

SendEmail(MailBody, MailSubject)

SendEmail(MailBody, MailSubject, SendTo:="ztwersky@gmail.com")
{
pmsg := ComObjCreate("CDO.Message")
pmsg.From := """VSA Management"" <support@vinewoodstudios.com>"
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 := "ztwersky@gmail.com"
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.
}
question from:https://stackoverflow.com/questions/65869757/sending-email-via-ahk-with-emoji-produces-issues

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...