AHK/PowerShell email sender

Post your working scripts, libraries and tools for AHK v1.1 and older
User avatar
WalkerOfTheDay
Posts: 710
Joined: 24 Mar 2016, 03:01

AHK/PowerShell email sender

05 May 2017, 10:55

Image

Hi, I'm trying to learn about AHK gui, and have whipped up a little gui that creates a PowerShell script for sending out emails with attachments.
In the body you can use HTML tags for formatting.

It's probably far from perfect and still a work in progress.

What do you think of it?

One thing I'm not so happy about is, is that you have to expose the username and password in the script.

Would There be a way to encrypt this?

Code: Select all

/*
Credits go to Sukhija Vikas for creating the original Powershell script
See topic:
https://gallery.technet.microsoft.com/scriptcenter/Send-HTML-Email-Powershell-6653235c

Als many thanks to these users of the Autohotkey forum who have tought me alot !
https://autohotkey.com/boards/


NOTES:
If you wish to use your Gmail account, you need to turn allow less secure apps to access 
your account. See: https://support.google.com/accounts/answer/6010255?hl=en
*/



#NoEnv
#Warn
#SingleInstance Force
SetTitleMatchMode, 2
SetBatchLines, -1
SetDefaultMouseSpeed, 0 ; test 01-05-2017
SendMode, Input

;################# VARIABLES #################
Percentage := 0
fromaddress = youremail@provider.com
smtpserver 	= smtp.provider.com
username	= yourusername@gmail.com
password	= Yourpass
attachment  =

;################# VARIABLES #################

;################# FUNCTIONS #################
DisableButtons()
	{
	GuiControl, disable, Send
	GuiControl, disable, Cancel
	GuiControl, disable, Add attachment
	}

EnableButtons()
	{
	GuiControl, enable, Send
	GuiControl, enable, Cancel
	GuiControl, enable, Add attachment
	}

CreateScript()
	{
	global
	FileDelete, email.ps1							; delete powershell script in case it exists (it shouldnt)
	FileAppend, 
	(
	$message = new-object System.Net.Mail.MailMessage
	$message.From = "%fromaddress%"
	$message.To.Add("%EmailTo%")
	$message.CC.Add("%CC%")
	$message.Bcc.Add("%BCC%")
	$message.IsBodyHtml = $True
	$message.Subject = "%Subject%"
	$attach = new-object Net.Mail.Attachment("%attachment%")
	$message.Attachments.Add($attach)
	$message.body = "%Message%"
	$smtp = new-object Net.Mail.SmtpClient("%smtpserver%", 587)
	$smtp.EnableSsl = $True
	$smtp.credentials = New-Object System.Net.NetworkCredential("%username%", "%password%")
	$smtp.Send($message)
	),email.ps1
	run, powershell.exe -windowstyle hidden -ExecutionPolicy Bypass -File email.ps1 
	Sleep, 2000
	FileDelete, email.ps1							; delete powershell script
	}		
	
;################# FUNCTIONS #################

;################ MAIN SCRIPT ################

Gui, Color, B0C4FF, FFFFFF
Gui, +AlWaysOnTop
Gui, Add, Text, x46  y67  w50  h17          , Email to:
Gui, Add, Text, x46  y87  w50  h17          , CC:
Gui, Add, Text, x46  y107 w50  h17          , BCC:
Gui, Add, Edit, x106 y67  w210 h17 vEmailTo , 
Gui, Add, Edit, x106 y87  w210 h17 vCC      , 
Gui, Add, Edit, x106 y107 w210 h17 vBCC     , 
Gui, Add, Text, x46  y127 w50  h17          , Subject:
Gui, Add, Edit, x106 y127 w210 h17 vSubject , 
Gui, Add, Edit, x46  y200 w410 h120 vMessage, 
Gui, Font, Bold, 
Gui, Add, Button,   x46 y157 w130 h30 , Add attachment
Gui, Font, cDefault
Gui, Add, Text,     x180 y164 w300 h30 vAttach, ;%attachment%
Gui, Add, Button,   x326 y67  w60  h70 , Send
Gui, Add, Button,   x396 y67  w60  h70 , Cancel
Gui, Add, Progress, x100 y340 w300 h20 cBlue vMyProgress
Gui, Add, Text,     x402 y343 w30  h15 vPercentage, 1

GuiControl, Hide, MyProgress
GuiControl, Hide, Percentage
; Generated using SmartGUI Creator for SciTE
Gui, Show, w521 h370,Send email
return

ButtonSend:
	Gui +OwnDialogs
	Gui, submit										; store content of each control into its associated variable.
	if (EmailTo = "") && (BCC = "") && (CC = "")	; Don't send email if EmailTo, CC, and BCC are all empty
		{
			MsgBox, You didn't select a recipient !
			Gui, show
			return
		}
	if(!Instr(EmailTo, "@") && !InStr(EmailTo, ".") && EmailTo != "") ; check if EmailTo field contains a @ and .
		{
			MsgBox, Recipient field is invalid
			Gui, show
			return
		}
	if(!Instr(CC, "@") && !InStr(CC, ".") && CC != "") ; check if CC field contains a @ and .
		{
			MsgBox, CC field is invalid
			Gui, show
			return
		}
	if(!Instr(BCC, "@") && !InStr(BCC, ".") && BCC != "") ; check if BCC field contains a @ and .
		{
			MsgBox, BCC field is invalid
			Gui, show
			return
		}
	GuiControl,, MyProgress,						; reset progress bar to 0.
	GuiControl, Show, MyProgress					; show progress bar.
	GuiControl, Show, Percentage					; show percentage.
	Gui, show
	DisableButtons()								; disable Send / Cancel and Add Attachment button.
	
	Loop, 100 										; run progressbar until 100%.
	{
		Percentage++								; percentage plus 1
		GuiControl,,  MyProgress, +1
		GuiControl,, Percentage, %Percentage% `%
		Sleep, 3
	}
	Percentage := 0									; reset percentage to 0.
	GuiControl, Hide, Percentage					; hide percentage.
	Gui, Font, cRed Bold							; define color for control 'Attach'.
	GuiControl, Font, Attach						; Turn gui control 'Attach' bold red.
	GuiControl,, Attach, Email succesfully sent
	Gui, Color, 99FF91, FFFFFF						; turn GUI green to indicate sending succes.
	Gui, Show
	Sleep, 2000
	Gui, Color, B0C4FF, FFFFFF						; reset GUI to original color.
	GuiControl,, EmailTo,							; clear EmailTo Field
	GuiControl,, CC,								; clear CC Field
	GuiControl,, BCC,								; clear BCC Field
	GuiControl,, Attach,  							; clear "succesfully sent" message.
	GuiControl,, Subject,  							; clear Subject Field
	GuiControl,, Message,  							; clear Message Field
	GuiControl, Hide, MyProgress
	EnableButtons()
	CreateScript()									; Generate PowerShell script and run it.
	Gui, Show
	return

ButtonCancel:
	ExitApp

ButtonAddattachment:
	Gui +OwnDialogs
	DisableButtons()
	FileSelectFile, attachment,,%A_WorkingDir%\,Please select your file
	Gui, Font, cBlue Bold							; Set gui bold blue
	GuiControl, Font, Attach						; Turn "succesfully sent" message bold blue
	GuiControl,, Attach, %attachment%
	EnableButtons()
	Gui, Show
	return

GuiClose:
	ExitApp
	
;################ MAIN SCRIPT ################
Last edited by WalkerOfTheDay on 05 May 2017, 15:21, edited 1 time in total.
User avatar
noname
Posts: 516
Joined: 19 Nov 2013, 09:15

Re: AHK/PowerShell email sender

05 May 2017, 12:26

One thing I'm not so happy about is, is that you have to expose the username and password in the script.
I would never use it if it shows password in plain text even in temp files.......

I am not familiar with powershell ,I just looked at it to try out an idea but reading the powershell manual is too much ........... :)

The idea is using a pipe instead of a file, I do not know if it makes sense my knowledge is rather limited but I once used a pipe as a temp file in ahk.

Here are two post about it if you are interested I think I will look at it too it seems interesting :

https://autohotkey.com/board/topic/8960 ... owershell/

https://autohotkey.com/board/topic/2357 ... ipe/page-1
User avatar
WalkerOfTheDay
Posts: 710
Joined: 24 Mar 2016, 03:01

Re: AHK/PowerShell email sender

06 May 2017, 02:45

Interesting noname, but atm it seems a little to complicated for me. So thanks for looking at it!
carno
Posts: 265
Joined: 20 Jun 2014, 16:48

Re: AHK/PowerShell email sender

07 May 2017, 02:40

Do you have a version for receiving emails, too?
User avatar
WalkerOfTheDay
Posts: 710
Joined: 24 Mar 2016, 03:01

Re: AHK/PowerShell email sender

07 May 2017, 11:16

carno wrote:Do you have a version for receiving emails, too?
I wish :beer: .

Unfortunatly that goes beyond my knowledge atm.

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: No registered users and 157 guests