Combining the Daily Horoscope from the Web with this AutoHotkey E-mail Sending Technique Makes It Possible to Deliver the Prescription to Anyone
In recent blogs, I discussed how to extract data from a Web page (“Quick and Dirty Web Data Extraction Script“) and send an e-mail directly from an AutoHotkey script (“How to Send E-mail Directly from an AutoHotkey Script“). This time I put them both together to demonstrate how to deliver a daily horoscope to yourself (or a friend).
E-mail—the oldest and most universal method for reaching people—offers a major advantage over text messages and other digital forms of communications (e.g. Twitter, Facebook). Most people own at least one e-mail address which they can access from any of their computers (PC, Mac, or Linux), any smartphone (iPhone or Android), or tablet. To send an e-mail, you don’t need to know a phone number or the type of device. E-mail servers push the message directly to the target addressee. Plus, using AutoHotkey, you can automate the periodic sending of an e-mail.
How to Get Your Daily Horoscope
I combined the techniques discussed in the previous blogs to create a script which sends a new e-mail every morning:
- The script accesses a horoscope site extracting the blurb for the day.
- The script includes the blurb in an e-mail sent via Gmail.
- Windows Task Scheduler runs the script at the same time every day.
In this blog, I don’t discuss every line of code. I covered much of this script’s content in the previous overviews—although the e-mail sending routine (different from the last one) still contains some mysterious elements. One item (password security), I plan to discuss in more detail in a future blog.
You can copy the script and put it into an AutoHotkey script (.ahk), but it won’t run until you add the proper credentials for your e-mail account. If you don’t use Gmail, then you will need to update the SMTP server and any other parameters as appropriate.
(The script visually word wraps the AutoHotkey code depending upon your reading device. However, the lines should appear intact when copied.)
The horoscope sending script using the Gmail server (account required):
; =========================================================== ; This first portion of the script extracts the horoscope for Cancer ; https://jacksautohotkeyblog.wordpress.com/2019/05/06/autohotkey-web-data-extraction-regex-trick/ ; =========================================================== GetHoroscope := "https://www.astrology.com/us/horoscope/daily-extended.aspx?sign=cancer" whr := ComObjCreate("WinHttp.WinHttpRequest.5.1") whr.Open("GET", GetHoroscope) whr.Send() Sleep 100 Horoscope := whr.ResponseText ; Web page code RegExMatch(Horoscope, "<p><span class=""date"">(.*?)</p>", Today) Today1 := RegExReplace(Today1,"<.+?>") ; First subexpression ; =========================================================== ; Password hidden in Windows Registry ; =========================================================== RegRead, Password, HKEY_CURRENT_USER\Software\DateStampConvert, ValueRead ; =========================================================== ; This portion of the script e-mails the horoscope ; Found this script at: ; https://autohotkey.com/board/topic/36522-cdo-com-email-delivery/://autohotkey.com/board/topic/36522-cdo-com-email-delivery/page-9?&#entry318725 ; Changed COM_CreateObject("CDO.Message") to ComObjCreate("CDO.Message") ; =========================================================== sFrom := "SendingAccount@gmail.com" sTo := "Recipient@gmail.com" FormatTime, TimeString,, LongDate ; Add date e.g. May 17, 2019 sSubject := "Horoscope — " . TimeString sBody := "Horoscope for People Born under Cancer `r`r" . Today1 sAttach := mAttrib = From|To|Subject|TextBody vars2 = sFrom|sTo|sSubject|sBody StringSplit, Attrib, mAttrib, | sServer := "smtp.gmail.com" ; specify your SMTP server nPort := 465 ; 25 bTLS := True ; False nSend := 2 ; cdoSendUsingPort nAuth := 1 ; cdoBasic tOut := 60 url := "http://schemas.microsoft.com/cdo/configuration/" uSub = sendusing|smtpconnectiontimeout|smtpserver|smtpserverport|smtpusessl|smtpauthenticate|sendusername|sendpassword vars1 = nSend|tOut|sServer|nPort|bTLS|nAuth|sUsername|sPassword StringSplit, sub, uSub, | sUsername := "username" sPassword := Password pmsg := ComObjCreate("CDO.Message") pfld := pmsg.Configuration.Fields Loop, Parse, vars1, | pfld.Item[url sub%A_Index%]:= %A_LoopField% pfld.Update() Loop, Parse, vars2, | pmsg[Attrib%A_Index%]:= %A_LoopField% if sAttach Loop, Parse, sAttach, |, %A_Space%%A_Tab% pmsg.AddAttachment[A_LoopField] pmsg.Send() pfld:=pmsg:="" Return
Replace all the values shown in red with the appropriate settings for your horoscope and Gmail account.
Supplying the Password
I didn’t want to embed my password in the script or another text file. That would make displaying or supplying the script dangerous—at least for my e-mail account. Therefore, hidden out of plain view, I manually created (Run (+R), regedit) a new Windows Registry key for the password. The AutoHotkey script then reads the password:
RegRead, Password, HKEY_CURRENT_USER\Software\DateStampConvert, ValueRead
Yes, if someone gets on my computer and knows how to open the Windows Registry, they can find the password. But if someone has access to my computer, I’m already in trouble.
To use the script as written, you must add your e-mail password to the Windows Registry (REG_SZ key type) at the location shown above. (You can use any Registry location you like—as long as you change the command above to match.) Or, you can replace the Password variable with the actual password in quotes (e.g. “jackspassword”).
I only used the above Windows Registry location because I had previously created it when discussing how to create keys for saving script settings (“Save AutoHotkey Script Settings in Your Windows Registry“).
Next time, I’ll discuss alternative approaches to the password protection problem.
Adding the Date
To include a little context for tailoring each e-mail, I added a formatted date:
FormatTime, TimeString,, LongDate ; Add date e.g. Friday, May 17, 2019 sSubject := "Horoscope — " . TimeString
The FormatTime command converts the current time in the LongDate format (e.g. Friday, May 17, 2019) and saves it to the variable TimeString for appending to the Subject line of the e-mail.
Running the Script Periodically
Many AutoHotkey users might consider using the SetTimer command to run the script on a daily basis. However, that would require some type of continuously running AutoHotkey script. In this situation, Windows Task Scheduler makes perfect sense. Once set up, Task Scheduler launches the script every day at the designated time.
To access Windows Task Scheduler, right-click on the Windows Start Button (Windows 10) and select Computer Management. Or, on most Windows computers, you can look for “Task Scheduler” or “Schedule Task” in various windows or search options (Control Panel, Start Menu, etc).
While an AutoHotkey script could do this scheduling job, sometimes you’ll find it easier to use built-in Windows tools.
For more information on how to run Task Scheduler see the blog “Stop Accidental Deletions with the BlockInput Command (AutoHotkey Tip—Part Two).” I previously used Task Scheduler to resolve a privilege issue with a script. That blog demonstrates how to set up a reoccurring job.
Using the Horoscope E-mail Script as a Template
You can modify the above script to extract information from almost any Web page and deliver it to your Inbox on any digital device supporting e-mail.
Click the Follow button at the top of the sidebar on the right of this page for e-mail notification of new blogs. (If you’re reading this on a tablet or your phone, then you must scroll all the way to the end of the blog—pass any comments—to find the Follow button.)
This post was proofread by Grammarly
(Any other mistakes are all mine.)
(Full disclosure: If you sign up for a free Grammarly account, I get 20¢. I use the spelling/grammar checking service all the time, but, then again, I write a lot more than most people. I recommend Grammarly because it works and it’s free.)