Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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_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.
}
like image 448
Zvi Twersky Avatar asked Jan 24 '21 11:01

Zvi Twersky


People also ask

How to send emojis in an email?

Click on the one that best suits the emotion you want to transmit and select Enter on your keyboard to insert in the email. The emoji panel contains several symbols, including human faces, animals, objects, and road signs, to mention a few.

How to choose the emoji version for the recipient's computer?

Note: If you use a Windows 7 PC, you can choose black and white emoji via the Segoe UI Symbol font. The colored emoji version will be shown if the recipient’s computer supports colorful emoji.

Why do emojis have question marks on them?

These boxes and question marks appear because emoji support on the sender's device is not the same as emoji support on the device of the recipient. ... When new versions of Android and iOS are pushed out, that's when emoji boxes and question

Can you use emojis in mail chimp to avoid spam?

Mail Chimp makes no mention of emojis in their Knowledge Base article about avoiding spam filters. And emojis don’t appear on Remarkety’s list of 25 Ways to Avoid Sending Spam either. In fact, in all my Googling, I found that emojis can actually increase open rates, when used strategically.


Video Answer


1 Answers

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.

like image 109
0x464e Avatar answered Oct 05 '22 21:10

0x464e