Send Email Directly from AutoHotkey

Post your working scripts, libraries and tools
User avatar
jackdunning
Posts: 120
Joined: 01 Aug 2016, 18:17
Facebook: https://www.facebook.com/ComputorEdgeEBooks/posts
Contact:

Send Email Directly from AutoHotkey

10 Jan 2020, 17:29

I found this script in the archived AutoHotkey Forum topic "CDO COM - Email Delivery [AHK_L]" by shajul from November 29, 2010. I checked this forum but didn't find this version. (Let me know if I just missed it.)

You can use it to add direct e-mailing to any of your scripts—no e-mail client program required. I take no credit for writing the script. I only post it here because I find the technique important enough to deserve a reposting:

Code: Select all

pmsg := ComObjCreate("CDO.Message")     ; Do not change
pmsg.From := """Mr Big"" <[email protected]>"
pmsg.To := "[email protected]"
pmsg.BCC := "" ; Blind Carbon Copy, Invisible for all, same syntax as CC
pmsg.CC := ""
pmsg.Subject := "E-mail Test"

pmsg.TextBody := "Test of sending e-mail directly from AutoHotkey."

; Remove semicolon to activate attachment feature
; 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/"  ; Do not change


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()
It works well enough to add to my scripts page, mention in this forum, and save for posterity. I discuss it in "How to Send E-mail Directly from an AutoHotkey Script."
I've added the EmailSend.ahk script to my AutoHotkey scripts page.

This script acts as a template for sending e-mail directly from AutoHotkey. You must tailor the code to your own e-mail server, username, password, recipient name and other pertinent data.

Many AutoHotkey scripts merely open the default Windows e-mail program (MailTo:) while inserting the appropriate parameters. This AutoHotkey script bypasses any e-mail programs and directly sends the message using ComObject.

It worked for me and has shown itself to be reliable when run by Window Schedule to deliver a daily horoscope scraped from a Web page (image above). I discuss that horoscope mailing script in "E-mail the Daily Horoscope to Yourself."
Tre4shunter
Posts: 73
Joined: 26 Jan 2016, 16:05

Re: Send Email Directly from AutoHotkey

12 Jan 2020, 12:46

Hey @jackdunning -

I used to use CDO as well for the last few years and it has worked great! Thanks for compiling it nicelyand reminding the community about it as well.

Recently - i found a need to use exchange online as an smtp server to send mail automatically....and unfortunately, CDO is unable to do it because it does not properly support TLS (if you find otherwise, please let me know!)

I've found a workaround using https://www.emailarchitect.net/easendmail.

Here is the ahk:

Code: Select all

;@Ahk2Exe-AddResource *24 SendMail.exe.manifest, 1
sObj := ComObjCreate("{DF8A4FE2-221A-4504-987A-3FD4720DB929}")
sObj.LicenseCode := "TryIt"

sObj.From := "[email protected]"
sObj.AddRecipient("Joe","[email protected]",0)

sObj.UserName := "[email protected]"
sObj.Password := "xxxxxxxx"

sObj.ServerAddr := "smtp.office365.com"
sObj.ConnectType :=  4
sObj.ServerPort := 587

sObj.Subject := "Subject line here"
sObj.BodyText := "Body Text"
a := sObj.SendMail()
ExitApp
I used the compiler directive at the top in order to add the manifest file for the .dll into the compiled autohotkey executable. This way you do not need to register the .dll and the app will work standalone on any system as long as you have the .dll and the yourfile.exe.manifest in the same directory.

See this forum post for reference on what the above ^ means.

Code: Select all

https://www.autohotkey.com/boards/viewtopic.php?t=9954#p55300
credit @tmplinshi for that!

Here is a .zip containing all of the necessary files. Obviously you will need to compile with your correct credentials in order for it to work.

Let me know if you have any questions!

Thanks!
SOTE
Posts: 1129
Joined: 15 Jun 2015, 06:21

Re: Send Email Directly from AutoHotkey

12 Jan 2020, 13:28

Tre4shunter, you might should have mentioned that the components offered at that website are payware and not free, so that people are clear about that.
Tre4shunter
Posts: 73
Joined: 26 Jan 2016, 16:05

Re: Send Email Directly from AutoHotkey

12 Jan 2020, 13:42

You are right, i mean to! Yes, it is not freeware. Thanks for pointing that out @SOTE and as you can see in the ahk, the license is et to 'Try me'
gwarble
Posts: 461
Joined: 30 Sep 2013, 15:01

Re: Send Email Directly from AutoHotkey

12 Jan 2020, 14:06

I like the native COM approach personally, but there are FREE alternatives, like blat.dll|exe or mailsend.exe


one thing that might help people testing this... in order to get sending to work with Gmail i had to turn on "allow access to less secure apps" in the security settings




Code: Select all

 To      := "[email protected]"
 Subject := "Subject"
 Body := "Body"
 From    := """From"" <[email protected]>"
 User    := "[email protected]"
 Pass    := "password" 
 Server  := "smtp.gmail.com"
 Port    := 465 ;587 ;25

 Email(From, To, Subject, Body, User, Pass, Server, Port)
Return


Email(sFrom, sTo, sSubject, sBody, sUsername, sPassword, sServer, nPort, sAttach="") {
 bTLS      := True ; False
 nSend     := 2   ; cdoSendUsingPort
 nAuth     := 1   ; cdoBasic
 ;ComObjError(false) ;suppress errors from end user
 pmsg :=   ComObjCreate("CDO.Message")
 pcfg :=   pmsg.Configuration
 pfld :=   pcfg.Fields
 pfld.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") := nSend
 pfld.Item("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") := 60
 pfld.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") := sServer
 pfld.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") := nPort
 pfld.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") := bTLS
 pfld.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") := nAuth
 pfld.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") := sUsername
 pfld.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") := sPassword
 pfld.Update()
 pmsg.From := sFrom
 pmsg.To := sTo
 pmsg.Subject := sSubject
 pmsg.TextBody := sBody
 Loop, Parse, sAttach, |, %A_Space%%A_Tab%
  pmsg.AddAttachment(A_LoopField)
 pmsg.Send()
Return
}
EitherMouse - Multiple mice, individual settings . . . . www.EitherMouse.com . . . . forum . . . .
Tre4shunter
Posts: 73
Joined: 26 Jan 2016, 16:05

Re: Send Email Directly from AutoHotkey

13 Jan 2020, 09:19

All -

I prefer to just use CDO as well, as there are no external dependencies required, and after reading this thread i definitely over complicated things by using EASendMail....if you require TLS i would just go with mailsend.exe
gwarble
Posts: 461
Joined: 30 Sep 2013, 15:01

Re: Send Email Directly from AutoHotkey

13 Jan 2020, 20:40

before using CDO COM i used to prefer the blat.dll over mailsend.exe but I can't remember the support for SSL/TLS (and I don't really understand the difference between the two)

I have not been able to get the CDO COM method to work with Office365 ... being picky with From/Sender/Username ...
The CDO COM method tested working with Office365
(smtp.office365.com, port 25, TLS on port 587 with TLS, does not work from xfinity residential)

The CDO COM method tested working yesterday with Gmail
(smtp.gmail.com, port 465, SSL on, allow less secure apps on, limit 99 per 24hrs?)

also tested working yesterday with a freehosting.com website email
(cpanel.freehosting.com, port 465, SSL on, limit 15 per hour?)
Last edited by gwarble on 14 Jan 2020, 18:01, edited 1 time in total.
EitherMouse - Multiple mice, individual settings . . . . www.EitherMouse.com . . . . forum . . . .
Tre4shunter
Posts: 73
Joined: 26 Jan 2016, 16:05

Re: Send Email Directly from AutoHotkey

14 Jan 2020, 11:13

@gwarble

My understanding is that CDO does not support TLS, which is what your issue with trying to use O365 smtp is. This was the same problem i had, which initially led me to using EASendmail...though ive since switched to just using mailsend.exe as it is freeware.

Blat as far as i know also does not support TLS.
User avatar
moefr01
Posts: 95
Joined: 25 Nov 2015, 09:01
Location: Germany

Re: Send Email Directly from AutoHotkey

14 Jan 2020, 12:03

@Tre4shunter, @gwarble

did you ever try:
...
fields.smtpserver := "smtp.office365.com"
fields.smtpserverport := 25
fields.sendtls := True
fields.smtpusessl := True
fields.sendusing := 2
fields.smtpauthenticate := 1
...

TLS is working as expected... :wave: moefr01
gwarble
Posts: 461
Joined: 30 Sep 2013, 15:01

Re: Send Email Directly from AutoHotkey

14 Jan 2020, 15:05

I did, but maybe port 25 is blocked for me?
I'll try again just in case
EitherMouse - Multiple mice, individual settings . . . . www.EitherMouse.com . . . . forum . . . .
Tre4shunter
Posts: 73
Joined: 26 Jan 2016, 16:05

Re: Send Email Directly from AutoHotkey

14 Jan 2020, 16:46

Sure enough...it does work! I was always trying to use port 587 with TLS when it wasnt working....you're the man.

Thanks @moefr01 !!
gwarble
Posts: 461
Joined: 30 Sep 2013, 15:01

Re: Send Email Directly from AutoHotkey

14 Jan 2020, 17:45

Confirmed it does function with Office365 here from my work network (Sonic fiber optic)
and according to google, Xfinity (who I use at home) has blocked the use of port 25
EitherMouse - Multiple mice, individual settings . . . . www.EitherMouse.com . . . . forum . . . .
User avatar
jackdunning
Posts: 120
Joined: 01 Aug 2016, 18:17
Facebook: https://www.facebook.com/ComputorEdgeEBooks/posts
Contact:

Re: Send Email Directly from AutoHotkey

14 Jan 2020, 18:06

I too have Xfinity at home. This probably explains why port 25 didn't work for me. Also, that's why my script shows port 465—which does work—at least with Gmail.
User avatar
twitter SeX
Posts: 2
Joined: 19 Oct 2019, 00:59
Facebook: streetart
Google: streetart

Re: Send Email Directly from AutoHotkey

11 Feb 2020, 15:23

i tried to use the code without success until yet

@jack os version? ahk version?

i know a discontinued automation project alomware actions with links on different dl sites i.e. download.cnet.com /AlomWare-Actions/3000-2084_4-76476216.html

they offer a template " send a Desktop screenshot to an email address " via a click or a keyboard shortcut

it worked for me with an gmx.de email account with port 587 -_- but not with gmail
gmx offers too english @gmx•com accounts in USA
Last edited by twitter SeX on 11 Feb 2020, 22:46, edited 1 time in total.
User avatar
Delta Pythagorean
Posts: 568
Joined: 13 Feb 2017, 13:44
GitHub: DelPyth
Location: Somewhere in the US

Re: Send Email Directly from AutoHotkey

11 Feb 2020, 18:07

Isn't there a way to send an email using the run command or something? I think there was but I can't remember what I did to get it working. I might have had a program installed to do it.

- [AHK].......: 1.1.33.02 Unicode 64-bit
- [OS].........: Windows 10.0.18362
- [GITHUB]...: github.com/DeltaPyth
- [PAYPAL]....: paypal.me/DelPyth
- [DISCORD]..: Delta#3324

Remember to use [code]CODE[/code] for your multi-line scripts.
Stay safe, stay inside, and remember to wash your hands for 20 seconds!
User avatar
jackdunning
Posts: 120
Joined: 01 Aug 2016, 18:17
Facebook: https://www.facebook.com/ComputorEdgeEBooks/posts
Contact:

Re: Send Email Directly from AutoHotkey

15 Feb 2020, 17:48

twitter SeX wrote:
11 Feb 2020, 15:23
i tried to use the code without success until yet

@jack os version? ahk version?

i know a discontinued automation project alomware actions with links on different dl sites i.e. download.cnet.com /AlomWare-Actions/3000-2084_4-76476216.html

they offer a template " send a Desktop screenshot to an email address " via a click or a keyboard shortcut

it worked for me with an gmx.de email account with port 587 -_- but not with gmail
gmx offers too english @gmx•com accounts in USA
I use Windows 10 and the current version of AutoHotkey. Depending upon the email account, you may need to change ports—if it works at all. Some providers might block this type of email. I'm not an expert on email protocols. I just know that it works for me with gmail.
User avatar
jackdunning
Posts: 120
Joined: 01 Aug 2016, 18:17
Facebook: https://www.facebook.com/ComputorEdgeEBooks/posts
Contact:

Re: Send Email Directly from AutoHotkey

15 Feb 2020, 17:54

Delta Pythagorean wrote:
11 Feb 2020, 18:07
Isn't there a way to send an email using the run command or something? I think there was but I can't remember what I did to get it working. I might have had a program installed to do it.
Commonly, AutoHotkey users employ the Run, Mailto: command when automating Windows e-mail. In conjunction with other AutoHotkey automation techniques (such as those discussed in the chapters in Section 8.1, “Windows Automation” of the book Jack’s Motley Assortment of AutoHotkey Tips), scripts can control virtually every aspect of e-mail processing. However, you do run into a few issues when using e-mail programs for sending an e-mail:

  • You must load and navigate the program every time you want to send a message.
  • After AutoHotkey loads the application, any additional automation must fit the specific program (i.e. Windows Mail, Outlook, Mozilla Thunderbird, Web-based mail programs, etc).
  • With most e-mail apps, the Run, Mailto: command will not automatically send the e-mail. It merely opens your default e-mail program and loads the text into a new e-mail window.
The process can be slow and cumbersome.
VeryNewToAHK
Posts: 1
Joined: 04 Sep 2020, 15:12

Re: Send Email Directly from AutoHotkey

04 Sep 2020, 15:18

I copied the script onto my Windows 10 laptop and I am getting this error.

Error at line 5.
The following variable name contains an illegal character
"pmsg.From"
The program will exit.

Any help would be greatly appreciated. Thank you
gregster
Posts: 5388
Joined: 30 Sep 2013, 06:48

Re: Send Email Directly from AutoHotkey

04 Sep 2020, 19:45

VeryNewToAHK wrote:
04 Sep 2020, 15:18
I copied the script onto my Windows 10 laptop and I am getting this error.

Error at line 5.
The following variable name contains an illegal character
"pmsg.From"
The program will exit.
Seems like a strange error. Which AutoHotkey version do you use?
Something ancient like 1.0.x ? Then try something up-to-date like v1.1.33.02.

Return to “Scripts and Functions”

Who is online

Users browsing this forum: gwarble and 29 guests