How can I send email with AHK code?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
chezzoc
Posts: 18
Joined: 04 Jul 2022, 02:53

How can I send email with AHK code?

Post by chezzoc » 06 Dec 2022, 10:09

Hi. I looked up various of ways to send an email with AHK before posting this, but none of them really worked. here is one of them : viewtopic.php?t=71493
with the code

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 := "XXXXXXX"
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()
the above did not work for me as well as other ones too. do these methods dont work anymore? if not then how can I try to send an email from AHK code?

User avatar
mikeyww
Posts: 26951
Joined: 09 Sep 2014, 18:38

Re: How can I send email with AHK code?

Post by mikeyww » 06 Dec 2022, 11:39

I find that "did not work" is an inadequate problem description. What actually happened? Did the script run? Was there an error message? What did you attempt to do? What is the overall goal of the script?

This worked here with another server, so you may need to check your parameters and ensure that you are authorized to send. Try a simple text message with no attachment, to see if you can simply send a message. That will confirm the basic process of sending. After it works, send an attachment by hard-coding one value. After that works, you can expand from there.

This process of testing enables you to investigate and understand your script in a stepwise fashion. It can help in isolating the problem that you are having.

Code: Select all

sendEmail(from, to, cc, bcc, subj, body, server, port, user, pwd) {
 /*
 https://autohotkey.com/board/topic/60813-cdo-com-email-delivery-ahk-l/
 https://jacksautohotkeyblog.wordpress.com/2019/05/10/how-to-send-e-mail-directly-from-an-autohotkey-script/
 https://jacksautohotkeyblog.wordpress.com/2019/05/17/e-mail-the-daily-horoscope-to-yourself-autohotkey-trick/
 */
 pmsg := ComObjCreate("CDO.Message")
 pmsg.From := from, pmsg.To := to, pmsg.CC := cc, pmsg.BCC := bcc, pmsg.Subject := subj, pmsg.TextBody := body
 fields := Object(), fields.smtpserver := server, fields.smtpserverport := port, fields.smtpusessl := True
 fields.sendusing := 2 ; cdoSendUsingPort
 fields.smtpauthenticate := 1 ; cdoBasic
 fields.sendusername := user, fields.sendpassword := pwd, 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()
 Try pmsg.Send()
 Catch, err
  errMsg := "Problem:  " err.Extra
   . "`nFile:          " err.File
   . "`nLine:         " err.Line
   . "`n              " err.What
   . "`nError:                       " err.Message
 Return errMsg
}

User avatar
JoeWinograd
Posts: 2200
Joined: 10 Feb 2014, 20:00
Location: U.S. Central Time Zone

Re: How can I send email with AHK code?

Post by JoeWinograd » 06 Dec 2022, 12:01

chezzoc wrote:looked up various of ways to send an email with AHK
I don't know if this is one of the ways that you found, but if not, give it a look:
viewtopic.php?f=76&t=67283

Regards, Joe

User avatar
mikeyww
Posts: 26951
Joined: 09 Sep 2014, 18:38

Re: How can I send email with AHK code?

Post by mikeyww » 06 Dec 2022, 12:08

I think that all of these things are roughly the same in terms of coding.

Potential issues:
1. Not authorized to send
2. Invalid e-mail address
3. Invalid attachment
4. Cannot send attachment
5. Other

chezzoc
Posts: 18
Joined: 04 Jul 2022, 02:53

Re: How can I send email with AHK code?

Post by chezzoc » 06 Dec 2022, 12:26

mikeyww wrote:
06 Dec 2022, 11:39
I find that "did not work" is an inadequate problem description. What actually happened? Did the script run? Was there an error message? What did you attempt to do? What is the overall goal of the script?

This worked here with another server, so you may need to check your parameters and ensure that you are authorized to send. Try a simple text message with no attachment, to see if you can simply send a message. That will confirm the basic process of sending. After it works, send an attachment by hard-coding one value. After that works, you can expand from there.

This process of testing enables you to investigate and understand your script in a stepwise fashion. It can help in isolating the problem that you are having.

Code: Select all

sendEmail(from, to, cc, bcc, subj, body, server, port, user, pwd) {
 /*
 https://autohotkey.com/board/topic/60813-cdo-com-email-delivery-ahk-l/
 https://jacksautohotkeyblog.wordpress.com/2019/05/10/how-to-send-e-mail-directly-from-an-autohotkey-script/
 https://jacksautohotkeyblog.wordpress.com/2019/05/17/e-mail-the-daily-horoscope-to-yourself-autohotkey-trick/
 */
 pmsg := ComObjCreate("CDO.Message")
 pmsg.From := from, pmsg.To := to, pmsg.CC := cc, pmsg.BCC := bcc, pmsg.Subject := subj, pmsg.TextBody := body
 fields := Object(), fields.smtpserver := server, fields.smtpserverport := port, fields.smtpusessl := True
 fields.sendusing := 2 ; cdoSendUsingPort
 fields.smtpauthenticate := 1 ; cdoBasic
 fields.sendusername := user, fields.sendpassword := pwd, 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()
 Try pmsg.Send()
 Catch, err
  errMsg := "Problem:  " err.Extra
   . "`nFile:          " err.File
   . "`nLine:         " err.Line
   . "`n              " err.What
   . "`nError:                       " err.Message
 Return errMsg
}
This is the error I'm getting using my code. I'm not sure whats wrong. I'm also getting same error with Joe's code from his link
image.png
image.png (48.44 KiB) Viewed 948 times
But with the code you listed. i tried using the function. the script runs and doesnt produce any error. but im not getting an email sent to me. this is the function parameters i Used

Code: Select all

sendEmail(from, to, cc, bcc, subj, body, server, port, user, pwd)

Code: Select all

sendEmail("[email protected]", "[email protected]", "hi", "", "test email", "test body","smtp.gmail.com", 465, "[email protected]", "myPassword123")

User avatar
Chunjee
Posts: 1422
Joined: 18 Apr 2014, 19:05
Contact:

Re: How can I send email with AHK code?

Post by Chunjee » 06 Dec 2022, 13:07

I believe Google no longer allows sending through their SMTP like this.

viewtopic.php?f=76&t=102434&p=455909


edit: actually follow the instructions in that thread. Requires an auth2.0 password or something.
Last edited by Chunjee on 06 Dec 2022, 13:29, edited 1 time in total.

chezzoc
Posts: 18
Joined: 04 Jul 2022, 02:53

Re: How can I send email with AHK code?

Post by chezzoc » 06 Dec 2022, 13:22

ok I just got it to work. the problem was with Gmail. apparently you have to generate an App password and use that instead of ur real password and it would work. ty for the help guys. appreciate it.

Post Reply

Return to “Ask for Help (v1)”