Class: Email
0.5
Interface: EmailI
0.6
Description
An Email class (standard-library compatible) which implements my EmailI interface and provides email sending functionality through AHK in an object-oriented fashion.
Download
Email.zip
To use this, or any, class in AHK, you need the Class library. You can get the Class library here:
Class.zip. Simply extract all files to one of the two AHK library directories (usually My Documents\AutoHotKey\lib)
Example Usage
Code:
#Include Email.ahk
#Include EmailI.ahk
; Set some message-related variables
SmtpServer = your.smtpserver.com
SmtpPort = 25
SmtpUsername = yourusername
SmtpPassword = yourpassword
from = You <you@yourdomain.com>
to = Someone Else <someone@somedomain.com>
subject = Test message
Body := "This is the body...`n`nIt is where your message goes.`n`n--`nAnd your signature"
html := false
priority = 3
attachment = ; Put a file path here
; Create the message and set your options
Msg := Email_new(from, to, subject, body)
Email_setPriority(Msg, priority)
Email_smtpServer(Msg, SmtpServer, SmtpPort, SmtpUsername, SmtpPassword)
Email_setSendHTML(Msg, html)
if attachment
Email_attach(Msg, attachment)
; Send the message
Email_send(Msg)
Instructions1. Either place Email.ahk and EmailI.ahk in one of your AHK lib directories, or #include them in your script directly
2. Place blat.dll in a 'bin' subdir of your script (you can also use almost any other mailer--see below)
3. See Email_test.ahk for a basic usage example.
4. See Email.ahk to see what other functions are available
5. Wait for more documentation if you're not into experimenting
ExplanationI needed some email functionality for a script I am writing for work, and I wanted to keep it OOP, and not tied to any single email utility, since I have no way of knowing what we may want to use in the future.
So I wrote a neat little library to interface with Blat.dll (or Blat.exe, or any sendmail-like app) to send mail notifications via SMTP if my script fails.
In OOP terms, I wrote an Email interface (EmailI) that defines an Email object. I also implemented that interface in the Email class, which enables you to use almost any sendmail-like mailer (exe or dll) by configuring the constants.
What does this mean? If you're a user, you can utilize the Email class to send mail from your scripts. If you're into OOP for AHK, you can implement the EmailI Interface yourself and create your own email class!
Through the Email_constant() function, you can define many custom variables and parameter names in order to use almost any mailer needed, and place it wherever you want.
To demonstrate how easy it is through the most simple case possible, I've also included Blat.exe alongside Blat.dll. You can go ahead and delete it if you'd like. Or, if you prefer an Exe, try adding this toward the top of the above example:
Code:
Email_constant("mailerFile", "Blat.exe")
Email_constant("mailerIsExe", true)
And there you have it, the exact same email should be sent the exact same way, via Exe instead of Dll. By changing some dictionary values within the mailerOpts constant, you can change the parameter names of individual options corresponding to what's offered by most popular mailers. As long as you use Blat (included), you don't need to mess with that, however.