Send email automatically with AHK

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
PuzzledGreatly
Posts: 1303
Joined: 29 Sep 2013, 22:18

Send email automatically with AHK

22 Aug 2017, 22:04

I have a script that is working with one Windows 7 PC but fails on another. The one that works runs as admin whereas the one that fails doesn't. Is it necessary to run as admin to send email automatically using AHK? Here is the script. It looks at a folder and send an email with an attachment for files not older than a day:

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.

SetBatchLines, -1

sendList = c:\temp\sendList.txt

FileDelete, %sendList%

	Time := A_Now
	;lastAccessed -= 1, D
	EnvAdd, Time, -5, Days

loop, C:\Auto Emailer\*.*, 0, 1
{
	if (A_LoopFileTimeModified > Time)
	if A_LoopFileExt contains doc,txt,jpg
	FileAppend, %A_LoopFileLongPath%`n, %sendList%
}	

loop, read, %sendList%
{
	ifexist, %A_LoopReadLine% 
	{
		pfld :=   pmsg.Configuration.Fields

		For field,value in fields
		   pfld.Item(schema . field) := value
		pfld.Update()

		splitpath, A_loopreadline, doc
		
		title = File: %doc%
		
		content = Update: %A_LoopReadLine%

		sFrom     := "[email protected]"
		sTo       := "[email protected]"
		sSubject  := title
		sBody     := content
		sAttach   := A_LoopReadLine

		sServer   := "server.example.com" ; specify your SMTP server
		nPort     :=  465
		bTLS      :=  False
		nSend     := 2   ; cdoSendUsingPort
		nAuth     := 1   ; cdoBasic
		sUsername := "[email protected]"
		sPassword := "password"

		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
		pmsg.AddAttachment(sAttach)
		pmsg.Send()	
	}
}

soundbeep
	
exitapp
Here is the error message the script generates:
---------------------------
SendEmail.ahk
---------------------------
Error: 0x80040213 -
Source: CDO.Message.1
Description: The transport failed to connect to the server.
HelpFile: (null)
HelpContext: 0

Specifically: Send

Line#
109: pfld.Item("http://schemas.microsoft.com/cdo/config ... ndpassword") := sPassword
110: pfld.Update()
112: pmsg.From := sFrom
113: pmsg.To := sTo
114: pmsg.Subject := sSubject
115: pmsg.TextBody := sBody
116: pmsg.AddAttachment(sAttach)
---> 117: pmsg.Send()
118: }
119: }
153: SoundBeep
155: ExitApp
156: Exit
157: Exit
157: Exit

Continue running the script?
---------------------------
Yes No
---------------------------
The firewall settings for each computer allow outbound connections automatically. Can anyone see anything wrong with script, suggest a fix or offer an alternative method to send emails automatically? Thanks.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: doodles333, vysmaty and 235 guests