Send E-Mail

5112 단어 WindowsAutoHotkey
Automating E-mail is something really convenient in daily tasks.
SendMail.ahk
#Noenv
#Persistent

oParam1 := {    FromWhom:       "Your Name"
        ,   SenderAddress:      "[email protected]"  ; this address is just an example
        ,   SendToAddress:      "[email protected]"  ; this address is just an example
        ,   BCC:                ""  ; can be blank
        ,   CC:             ""  ; can be blank
        ,   MessageTitle:       "Email Sending Test"
        ,   IsBodyHTML:     False                   ; Set it true if the message format is HTML                 
        ,   Message:            "Hello World!"   }      ; If the above value is True, format it like: <html><body>Hello World!</body></html>

oParam2 := {    SMTPServer:     "smtp.gmail.com"    ; using the gmail server for demo
        ,   SMTPServerPort:     465         ; the gmail smtp sever uses 465 
        ,   UseSSL:         True
        ,   SMTPUserName:       "username"  ; set your user name here e.g. [email protected]
        ,   SMTPPassword:       "password"      ; set your password here
        ,   SMTPTimeout:        30      }   ; seconds 

oParam3 := []       
oParam3.Insert(A_ScriptFullPath)        ; Attach the script itself for demo
oParam3.Insert("C:\Users\Public\Pictures\Sample Pictures\Koala.jpg")        ; Attach a sample picture for demo

if SendEmail(oParam1, oParam2, oParam3)     
    TrayTip, E-Mail Sent, % oParam1.MessageTitle " has been sent.", 5, 1
else
    TrayTip, E-Mail Failed, % oParam1.MessageTitle " failed to be sent.", 5, 1
Return

SendEmail(oMsg, oSMTP, oAttachments="") {
    ;[Parameters]
    ; oMsg must be an object with the following keys:
    ;   FromWhom:       the sender name 
    ;   SenderAddress:  the sender E-mail address
    ;   SendToAddress:  the receiver E-mail address
    ;   BCC:                Carbon Copy addresses separated by commas. Can be omitted. e.g. [email protected],[email protected]
    ;   CC:             Blined Carbon Copy addresses separated by commas. Can be omitted.
    ;   MessageTitle:       the subject of the E-mail
    ;   IsBodyHTML:     True/False to indicate the message format. If set to false, the message is sent as text.
    ;   Message:            the message, either in Html or text format. 
    ;   Encoding:       the character set. If omitted, UTF-8 will be applied. 
    ; oSMTP must be an object with the following keys:
    ;   SMTPServer:     the server address  e.g. smtp.gmail.com
    ;   SMTPServerPort: the port number, e.g. 465
    ;   UseSSL:         True/False to indicate whether the message is sent via SSL 
    ;   SMTPUserName:       the user name to log in to the SMTP server
    ;   SMTPPassword:       the password to log in to the SMTP server
    ;   SMTPTimeout:        the connection timeout seconds. If omitted, 60 seconds is applied.
    ; oAttachments must be an object containing values of file paths for attachment.

    ComError := ComObjError(false)
    pmsg                := ComObjCreate("CDO.Message")
    pmsg.From           := """" oMsg.FromWhom """ <" oMsg.SenderAddress ">"
    pmsg.To         := oMsg.SendToAddress
    pmsg.BCC            := oMsg.BCC
    pmsg.CC         := oMsg.CC
    pmsg.Subject        := oMsg.MessageTitle
    pmsg[oMsg.IsBodyHTML ? "HtmlBody" : "TextBody"]     := oMsg.Message
    pmsg.BodyPart.Charset := oMsg.Encoding ? oMsg.Encoding : "UTF-8"

    oFields := {    smtpserver:         oSMTP.SMTPServer
            ,   smtpserverport:     oSMTP.SMTPServerPort
            ,   smtpusessl:         oSMTP.UseSSL
            ,   sendusing:          2   ; use SMTP
            ,   smtpauthenticate:       1   ; 1: Basic / 2: NTLM
            ,   sendusername:       oSMTP.SMTPUserName
            ,   sendpassword:       oSMTP.SMTPPassword
            ,   smtpconnectiontimeout:  oSMTP.SMTPTimeout   ? oSMTP.SMTPTimeout : 60 }

    For field, value in oFields
       pmsg.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/" field) := value

    pmsg.Configuration.Fields.Update()

    ; Add attachments
    For k, sFilePath in oAttachments
        pmsg.AddAttachment(sFilePath)

    ; Send E-mail
    pmsg.Send()
    fSent := A_LastError ? False : True  
    ComObjError(ComError)
    Return fSent

    ; by A_Samurai 2012/02/28 License: Public Domain
}

좋은 웹페이지 즐겨찾기