Jump to content

Send email, but prompt for subject and body text input


Recommended Posts

I can successfully send an email by double clicking my script here. I want to be able to double click this and have it pop up a text input for the subject and body (separately if possible). Bonus points if I could rename those pop up prompts to "Name:" and "Helpdesk Issue:"

#include <File.au3>

; ===============================================================================================================================
; Variables for the _INetSmtpMailCom
; ===============================================================================================================================
Global Enum _
        $g__INetSmtpMailCom_ERROR_FileNotFound = 1, _
        $g__INetSmtpMailCom_ERROR_Send, _
        $g__INetSmtpMailCom_ERROR_ObjectCreation, _
        $g__INetSmtpMailCom_ERROR_COUNTER

Global Const $g__cdoSendUsingPickup = 1 ; Send message using the local SMTP service pickup directory.
Global Const $g__cdoSendUsingPort = 2 ; Send the message using the network (SMTP over the network). Must use this to use Delivery Notification
Global Const $g__cdoAnonymous = 0 ; Do not authenticate
Global Const $g__cdoBasic = 1 ; basic (clear-text) authentication
Global Const $g__cdoNTLM = 2 ; NTLM
Global $gs_thoussep = "."
Global $gs_decsep = ","
Global $sFileOpenDialog = ""

; Delivery Status Notifications
Global Const $g__cdoDSNDefault = 0 ; None
Global Const $g__cdoDSNNever = 1 ; None
Global Const $g__cdoDSNFailure = 2 ; Failure
Global Const $g__cdoDSNSuccess = 4 ; Success
Global Const $g__cdoDSNDelay = 8 ; Delay


_Sendmail()



Func _Sendmail()
    Local $sSmtpServer = "smtp.gmail.com" ; address for the smtp-server to use - REQUIRED
    Local $sFromName = "user" ; name from who the email was sent
    Local $sFromAddress = "youremail@gmail.com" ; address from where the mail should come
    Local $sToAddress = "mailreceiver@gmail.com" ; destination address of the email - REQUIRED
    Local $sSubject = "testing" ; subject from the email - can be anything you want it to be
    Local $sBody = "test" ; the messagebody from the mail - can be left blank but then you get a blank mail
    Local $sAttachFiles = "" ; the file(s) you want to attach seperated with a ; (Semicolon) - leave blank if not needed
    Local $sCcAddress = "" ; address for cc - leave blank if not needed
    Local $sBccAddress = "" ; address for bcc - leave blank if not needed
    Local $sImportance = "Normal" ; Send message priority: "High", "Normal", "Low"
    Local $sUsername = "youremail@gmail.com" ; username for the account used from where the mail gets sent - REQUIRED
    Local $sPassword = "xxxxxxx" ; password for the account used from where the mail gets sent - REQUIRED
    Local $iIPPort = 465 ; GMAIL port used for sending the mail
    Local $bSSL = True ; GMAIL enables/disables secure socket layer sending - set to True if using httpS


    Local $bIsHTMLBody = False
    Local $iDSNOptions = $g__cdoDSNDefault

    Local $rc = _INetSmtpMailCom($sSmtpServer, $sFromName, $sFromAddress, $sToAddress, $sSubject, $sBody, $sAttachFiles, $sCcAddress, $sBccAddress, $sImportance, $sUsername, $sPassword, $iIPPort, $bSSL, $bIsHTMLBody, $iDSNOptions)
    If @error Then
        MsgBox(0, "_INetSmtpMailCom(): Error sending message", _
                "Error code: " & @error & @CRLF & @CRLF & _
                "Error Hex Number: " & _INetSmtpMailCom_ErrHexNumber() & @CRLF & @CRLF & _
                "Description: " & _INetSmtpMailCom_ErrDescription() & @CRLF & @CRLF & _
                "Description (rc): " & $rc & @CRLF & @CRLF & _
                "ScriptLine: " & _INetSmtpMailCom_ErrScriptLine() _
                )
        ConsoleWrite("### COM Error !  Number: " & _INetSmtpMailCom_ErrHexNumber() & "   ScriptLine: " & _INetSmtpMailCom_ErrScriptLine() & "   Description:" & _INetSmtpMailCom_ErrDescription() & @LF)
    Else
        Dim $iMsgBoxAnswer
        $iMsgBoxAnswer = MsgBox(262208, "SUCCESS", ":-)", 5)
    EndIf

EndFunc   ;==>_Enviarmail



#Region UDF Functions
; The UDF
; #FUNCTION# ====================================================================================================================
; Name ..........: _INetSmtpMailCom
; Description ...:
; Syntax ........: _INetSmtpMailCom($s_SmtpServer, $s_FromName, $s_FromAddress, $s_ToAddress[, $s_Subject = ""[, $as_Body = ""[,
;                  $s_AttachFiles = ""[, $s_CcAddress = ""[, $s_BccAddress = ""[, $s_Importance = "Normal"[, $s_Username = ""[,
;                  $s_Password = ""[, $IPPort = 25[, $bSSL = False[, $bIsHTMLBody = False[, $iDSNOptions = $g__cdoDSNDefault]]]]]]]]]]]])
; Parameters ....: $s_SmtpServer        - A string value.
;                  $s_FromName          - A string value.
;                  $s_FromAddress       - A string value.
;                  $s_ToAddress         - A string value.
;                  $s_Subject           - [optional] A string value. Default is "".
;                  $s_Body              - [optional] A string value. Default is "".
;                  $s_AttachFiles       - [optional] A string value. Default is "".
;                  $s_CcAddress         - [optional] A string value. Default is "".
;                  $s_BccAddress        - [optional] A string value. Default is "".
;                  $s_Importance        - [optional] A string value. Default is "Normal".
;                  $s_Username          - [optional] A string value. Default is "".
;                  $s_Password          - [optional] A string value. Default is "".
;                  $IPPort              - [optional] An integer value. Default is 25.
;                  $bSSL                - [optional] A binary value. Default is False.
;                  $bIsHTMLBody         - [optional] A binary value. Default is False.
;                  $iDSNOptions         - [optional] An integer value. Default is $g__cdoDSNDefault.
; Return values .: None
; Author ........: Jos
; Modified ......: mLipok
; Remarks .......:
; Related .......: http://www.autoitscript.com/forum/topic/23860-smtp-mailer-that-supports-html-and-attachments/
; Link ..........: http://www.autoitscript.com/forum/topic/167292-smtp-mailer-udf/
; Example .......: Yes
; ===============================================================================================================================
Func _INetSmtpMailCom($s_SmtpServer, $s_FromName, $s_FromAddress, $s_ToAddress, $s_Subject = "", $s_Body = "", $s_AttachFiles = "", $s_CcAddress = "", $s_BccAddress = "", $s_Importance = "Normal", $s_Username = "", $s_Password = "", $IPPort = 25, $bSSL = False, $bIsHTMLBody = False, $iDSNOptions = $g__cdoDSNDefault)
    ; init Error Handler
    _INetSmtpMailCom_ErrObjInit()

    Local $objEmail = ObjCreate("CDO.Message")
    If Not IsObj($objEmail) Then Return SetError($g__INetSmtpMailCom_ERROR_ObjectCreation, Dec(_INetSmtpMailCom_ErrHexNumber()), _INetSmtpMailCom_ErrDescription())

    ; Clear previous Err information
    _INetSmtpMailCom_ErrHexNumber(0)
    _INetSmtpMailCom_ErrDescription('')
    _INetSmtpMailCom_ErrScriptLine('')

    $objEmail.From = '"' & $s_FromName & '" <' & $s_FromAddress & '>'
    $objEmail.To = $s_ToAddress

    If $s_CcAddress <> "" Then $objEmail.Cc = $s_CcAddress
    If $s_BccAddress <> "" Then $objEmail.Bcc = $s_BccAddress
    $objEmail.Subject = $s_Subject

    ; Select whether or not the content is sent as plain text or HTM
    If $bIsHTMLBody Then
        $objEmail.Textbody = $s_Body & @CRLF
    Else
        $objEmail.HTMLBody = $s_Body
    EndIf

    ; Add Attachments
    If $s_AttachFiles <> "" Then
        Local $S_Files2Attach = StringSplit($s_AttachFiles, ";")
        For $x = 1 To $S_Files2Attach[0]
            $S_Files2Attach[$x] = _PathFull($S_Files2Attach[$x])
            If FileExists($S_Files2Attach[$x]) Then
                ConsoleWrite('+> File attachment added: ' & $S_Files2Attach[$x] & @LF)
                $objEmail.AddAttachment($S_Files2Attach[$x])
            Else
                ConsoleWrite('!> File not found to attach: ' & $S_Files2Attach[$x] & @LF)
                Return SetError($g__INetSmtpMailCom_ERROR_FileNotFound, 0, 0)
            EndIf
        Next
    EndIf

    ; Set Email Configuration
    $objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = $g__cdoSendUsingPort
    $objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = $s_SmtpServer
    If Number($IPPort) = 0 Then $IPPort = 25
    $objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = $IPPort
    ;Authenticated SMTP
    If $s_Username <> "" Then
        $objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = $g__cdoBasic
        $objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = $s_Username
        $objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = $s_Password
    EndIf
    $objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = $bSSL

    ;Update Configuration Settings
    $objEmail.Configuration.Fields.Update

    ; Set Email Importance
    Switch $s_Importance
        Case "High"
            $objEmail.Fields.Item("urn:schemas:mailheader:Importance") = "High"
        Case "Normal"
            $objEmail.Fields.Item("urn:schemas:mailheader:Importance") = "Normal"
        Case "Low"
            $objEmail.Fields.Item("urn:schemas:mailheader:Importance") = "Low"
    EndSwitch

    ; Set DSN options
    If $iDSNOptions <> $g__cdoDSNDefault And $iDSNOptions <> $g__cdoDSNNever Then
        $objEmail.DSNOptions = $iDSNOptions
        $objEmail.Fields.Item("urn:schemas:mailheader:disposition-notification-to") = $s_FromAddress
;~      $objEmail.Fields.Item("urn:schemas:mailheader:return-receipt-to") = $s_FromAddress
    EndIf

    ; Update Importance and Options fields
    $objEmail.Fields.Update

    ; Sent the Message
    $objEmail.Send

    If @error Then
        _INetSmtpMailCom_ErrObjCleanUp()
        Return SetError($g__INetSmtpMailCom_ERROR_Send, Dec(_INetSmtpMailCom_ErrHexNumber()), _INetSmtpMailCom_ErrDescription())
    EndIf

    ; CleanUp
    $objEmail = ""
    _INetSmtpMailCom_ErrObjCleanUp()

EndFunc   ;==>_INetSmtpMailCom

;
; Com Error Handler
Func _INetSmtpMailCom_ErrObjInit($bParam = Default)
    Local Static $oINetSmtpMailCom_Error = Default
    If $bParam == 'CleanUp' And $oINetSmtpMailCom_Error <> Default Then
        $oINetSmtpMailCom_Error = ''
        Return $oINetSmtpMailCom_Error
    EndIf
    If $oINetSmtpMailCom_Error = Default Then
        $oINetSmtpMailCom_Error = ObjEvent("AutoIt.Error", "_INetSmtpMailCom_ErrFunc")
    EndIf
    Return $oINetSmtpMailCom_Error
EndFunc   ;==>_INetSmtpMailCom_ErrObjInit

Func _INetSmtpMailCom_ErrObjCleanUp()
    _INetSmtpMailCom_ErrObjInit('CleanUp')
EndFunc   ;==>_INetSmtpMailCom_ErrObjCleanUp

Func _INetSmtpMailCom_ErrHexNumber($vData = Default)
    Local Static $vReturn = 0
    If $vData <> Default Then $vReturn = $vData
    Return $vReturn
EndFunc   ;==>_INetSmtpMailCom_ErrHexNumber

Func _INetSmtpMailCom_ErrDescription($sData = Default)
    Local Static $sReturn = ''
    If $sData <> Default Then $sReturn = $sData
    Return $sReturn
EndFunc   ;==>_INetSmtpMailCom_ErrDescription

Func _INetSmtpMailCom_ErrScriptLine($iData = Default)
    Local Static $iReturn = ''
    If $iData <> Default Then $iReturn = $iData
    Return $iReturn
EndFunc   ;==>_INetSmtpMailCom_ErrScriptLine

Func _INetSmtpMailCom_ErrFunc()
    _INetSmtpMailCom_ErrObjInit()
    _INetSmtpMailCom_ErrHexNumber(Hex(_INetSmtpMailCom_ErrObjInit().number, 8))
    _INetSmtpMailCom_ErrDescription(StringStripWS(_INetSmtpMailCom_ErrObjInit().description, 3))
    _INetSmtpMailCom_ErrScriptLine(_INetSmtpMailCom_ErrObjInit().ScriptLine)
    SetError(1) ; something to check for when this function returns
    Return
EndFunc   ;==>_INetSmtpMailCom_ErrFunc

#EndRegion UDF Functions

 

Link to comment
Share on other sites

Sure :)
This is your actual code:

Local $sSubject = "testing" ; subject from the email - can be anything you want it to be
Local $sBody = "test" ; the messagebody from the mail - can be left blank but then you get a blank mail

In the two variables, you are setting "testing" and "test".
If you want to let the user fills those variables, you have to prompt to the user something, and, basically, you have two ways to do it:

  1. Create Graphic User Interface, from which the user could fill some text fields ( GUICtrlCreateInput()/GUICtrlCreateEdit() );
  2. Have an InputBox ( which, in my opinion, should not be the best solution for you, since InputBox has small textbox ).

In the first case, after creating the GUI, with a button, you should read the content of your text boxes ( or edits ) with the function GUICtrlRead().
You can take a look here for the first way, and here for the second one :)


Best Regards.

Edited by FrancescoDiMuro

Click here to see my signature:

Spoiler

ALWAYS GOOD TO READ:

 

Link to comment
Share on other sites

Ok, very close now. So I got the input box(es) and button. I just need help on 2 things. 

  1. I can type my name, but then I can't click back into that text box to change it after I have clicked on the next text box. I can shift+tab back in, but not click.
  2. Run:
    1. Click Submit and it goes away but pops up the message "Ticket Submitted"
    2. Text that was in both boxes gets copied to Subject and Body
    3. Email is sent.
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <GUIConstants.au3>

; GUI
GUICreate("Auto Loader", 340, 250)
; LABEL 1
$Label_1 = GUICtrlCreateLabel("Name:", 20, 20, 250, 20)
; Input 1
$Input_1 = GUICtrlCreateEdit("", 60, 13, 150, 20)
; LABEL 2
$Label_2 = GUICtrlCreateLabel("Helpdesk issue in as much detail as possible:", 20, 40, 250, 20)
; Input 2
$Input_2 = GUICtrlCreateEdit("", 20, 60, 300, 150)
; BUTTON
$RUN_1 = GUICtrlCreateButton("Submit", 20, 220, 40, 20)
; GUI MESSAGE LOOP
GUISetState()

 

Link to comment
Share on other sites

Thank you, that is a lot easier and nicer! Will be using in the future!

So now I can click between the two, that problem is solved.I have this GUI, but how do I get it to do this:

Close the box when I click Submit and save what the user has input in the input boxes. I want to save them as variables so I can use them later in another section. Here's what I have:

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form1", 615, 437, 770, 285)
$Label1 = GUICtrlCreateLabel("Name:", 40, 32, 35, 17)
$Label2 = GUICtrlCreateLabel("Issue with as much detail as possible:", 40, 56, 180, 17)
$Name = GUICtrlCreateInput("", 80, 24, 129, 21)
$Issue = GUICtrlCreateInput("", 40, 72, 353, 100)
$Submit = GUICtrlCreateButton("Submit", 40, 176, 75, 25)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit

    EndSwitch
WEnd

I want it to take that (above) and fill in the $sSubject and $sBody into the below script with the data that was input from the user.

Func _Sendmail()
    Local $sSmtpServer = "smtp.gmail.com" ; address for the smtp-server to use - REQUIRED
    Local $sFromName = "user" ; name from who the email was sent
    Local $sFromAddress = "youremail@gmail.com" ; address from where the mail should come
    Local $sToAddress = "mailreceiver@gmail.com" ; destination address of the email - REQUIRED
    Local $sSubject = "testing" ; subject from the email - can be anything you want it to be
    Local $sBody = "test" ; the messagebody from the mail - can be left blank but then you get a blank mail
    Local $sAttachFiles = "" ; the file(s) you want to attach seperated with a ; (Semicolon) - leave blank if not needed
    Local $sCcAddress = "" ; address for cc - leave blank if not needed
    Local $sBccAddress = "" ; address for bcc - leave blank if not needed
    Local $sImportance = "Normal" ; Send message priority: "High", "Normal", "Low"
    Local $sUsername = "youremail@gmail.com" ; username for the account used from where the mail gets sent - REQUIRED
    Local $sPassword = "xxxxxxx" ; password for the account used from where the mail gets sent - REQUIRED
    Local $iIPPort = 465 ; GMAIL port used for sending the mail
    Local $bSSL = True ; GMAIL enables/disables secure socket layer sending - set to True if using httpS


    Local $bIsHTMLBody = False
    Local $iDSNOptions = $g__cdoDSNDefault

    Local $rc = _INetSmtpMailCom($sSmtpServer, $sFromName, $sFromAddress, $sToAddress, $sSubject, $sBody, $sAttachFiles, $sCcAddress, $sBccAddress, $sImportance, $sUsername, $sPassword, $iIPPort, $bSSL, $bIsHTMLBody, $iDSNOptions)
    If @error Then
        MsgBox(0, "_INetSmtpMailCom(): Error sending message", _
                "Error code: " & @error & @CRLF & @CRLF & _
                "Error Hex Number: " & _INetSmtpMailCom_ErrHexNumber() & @CRLF & @CRLF & _
                "Description: " & _INetSmtpMailCom_ErrDescription() & @CRLF & @CRLF & _
                "Description (rc): " & $rc & @CRLF & @CRLF & _
                "ScriptLine: " & _INetSmtpMailCom_ErrScriptLine() _
                )
        ConsoleWrite("### COM Error !  Number: " & _INetSmtpMailCom_ErrHexNumber() & "   ScriptLine: " & _INetSmtpMailCom_ErrScriptLine() & "   Description:" & _INetSmtpMailCom_ErrDescription() & @LF)
    Else
        Dim $iMsgBoxAnswer
        $iMsgBoxAnswer = MsgBox(262208, "SUCCESS", ":-)", 5)
    EndIf

EndFunc   ;==>_Enviarmail
Edited by mattsk42
Link to comment
Share on other sites

Happy to help you :)

In the Help file, there are a lot of examples on how to save content of an InputBox, or a check from a ChechBox :)

You can easily use GUICtrlRead() to read the data from the two Input Boxes, in order to use everywhere you want in the code.

Since you are continuousely "asking" the GUI for new "messages", you have to include in the Switch...Case...EndSwitch statement, the button you created before :)

So, add the case $Submit ( which, usually, is indicated as $idButton ), in the Switch in the While loop, and from there, you can run a function ( in which you do the stuffs you want ), or a few rows of code :)

21 minutes ago, mattsk42 said:

Close the box when I click Submit

Just use GUIDelete($Form1) to delete the form ( and close it ).

Click here to see my signature:

Spoiler

ALWAYS GOOD TO READ:

 

Link to comment
Share on other sites

Ok, got it all working EXCEPT two things.

1. I can't press enter in the second input box. It types everything on one line. I want to be able to type multiple lines like you would in a text editor.

2. It only closes when I click the red x button to close. I want to click the Submit button, and then it sends and closes from there. I think I found where I need to put code, but I'm not sure what.

Here's where I'm not sure (where it says <--this part):

While 1
    $Submit = GUIGetMsg()
    Switch $Submit
    Case (WHEN I CLICK THE SUBMIT BUTTON) ;<--this part
       Func _Sendmail()
    Local $sSmtpServer = "smtp.gmail.com" ; address for the smtp-server to use - REQUIRED
    Local $sFromName = GUICtrlRead($Name) ; name from who the email was sent
    ...

Here's the complete code (with names changed):

include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#Region ### START Koda GUI section ### Form=f:\scripts\autoit\name-issue.kxf
$Form1 = GUICreate("Form1", 371, 310, 284, 199)
$Dakota = GUICtrlCreateLabel("Dakota username:", 8, 8, 79, 17)
$Label2 = GUICtrlCreateLabel("Issue with as much detail as possible:", 8, 48, 180, 17)
$Name = GUICtrlCreateInput("", 88, 5, 105, 21)
$Issue = GUICtrlCreateInput("", 8, 72, 353, 200)
$Submit = GUICtrlCreateButton("Submit", 8, 280, 75, 25)
$Label1 = GUICtrlCreateLabel("(example: myname)", 88, 26, 98, 17)
$Label3 = GUICtrlCreateLabel("@dakota.com", 192, 8, 60, 17)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
$Service = "##Service##"
$AtDakota = "@dakota.com##"

While 1
    $Submit = GUIGetMsg()
    Switch $Submit
    Case (WHEN I CLICK THE SUBMIT BUTTON)
       Func _Sendmail()
    Local $sSmtpServer = "smtp.gmail.com" ; address for the smtp-server to use - REQUIRED
    Local $sFromName = GUICtrlRead($Name) ; name from who the email was sent
    Local $sFromAddress = "user@dakota.com"; address from where the mail should come
    Local $sToAddress = "helpdesk@dakota.com" ; destination address of the email - REQUIRED
    Local $sSubject = GUICtrlRead($Name) ; subject from the email - can be anything you want it to be
    Local $sBody = GUICtrlRead($Issue)&$Service&GUICtrlRead($Name)&$AtDakota ; the messagebody from the mail - can be left blank but then you get a blank mail
    Local $sAttachFiles = "" ; the file(s) you want to attach seperated with a ; (Semicolon) - leave blank if not needed
    Local $sCcAddress = "" ; address for cc - leave blank if not needed
    Local $sBccAddress = "" ; address for bcc - leave blank if not needed
    Local $sImportance = "Normal" ; Send message priority: "High", "Normal", "Low"
    Local $sUsername = "matt@dakota.com" ; username for the account used from where the mail gets sent - REQUIRED
    Local $sPassword = "supersecretpassword" ; password for the account used from where the mail gets sent - REQUIRED
    Local $iIPPort = 465 ; GMAIL port used for sending the mail
    Local $bSSL = True ; GMAIL enables/disables secure socket layer sending - set to True if using httpS

    Local $bIsHTMLBody = False
    Local $iDSNOptions = $g__cdoDSNDefault

    Local $rc = _INetSmtpMailCom($sSmtpServer, $sFromName, $sFromAddress, $sToAddress, $sSubject, $sBody, $sAttachFiles, $sCcAddress, $sBccAddress, $sImportance, $sUsername, $sPassword, $iIPPort, $bSSL, $bIsHTMLBody, $iDSNOptions)
    If @error Then
        MsgBox(0, "_INetSmtpMailCom(): Error sending message", _
                "Error code: " & @error & @CRLF & @CRLF & _
                "Error Hex Number: " & _INetSmtpMailCom_ErrHexNumber() & @CRLF & @CRLF & _
                "Description: " & _INetSmtpMailCom_ErrDescription() & @CRLF & @CRLF & _
                "Description (rc): " & $rc & @CRLF & @CRLF & _
                "ScriptLine: " & _INetSmtpMailCom_ErrScriptLine() _
                )
        ConsoleWrite("### COM Error !  Number: " & _INetSmtpMailCom_ErrHexNumber() & "   ScriptLine: " & _INetSmtpMailCom_ErrScriptLine() & "   Description:" & _INetSmtpMailCom_ErrDescription() & @LF)
    Else
        GUIDelete($Form1)
        Dim $iMsgBoxAnswer
        $iMsgBoxAnswer = MsgBox(262208, "SUCCESS", "Thank you.", 5)

    EndIf

EndFunc   ;==>_Enviarmail


#Region UDF Functions
; The UDF
; #FUNCTION# ====================================================================================================================
; Name ..........: _INetSmtpMailCom
; Description ...:
; Syntax ........: _INetSmtpMailCom($s_SmtpServer, $s_FromName, $s_FromAddress, $s_ToAddress[, $s_Subject = ""[, $as_Body = ""[,
;                  $s_AttachFiles = ""[, $s_CcAddress = ""[, $s_BccAddress = ""[, $s_Importance = "Normal"[, $s_Username = ""[,
;                  $s_Password = ""[, $IPPort = 25[, $bSSL = False[, $bIsHTMLBody = False[, $iDSNOptions = $g__cdoDSNDefault]]]]]]]]]]]])
; Parameters ....: $s_SmtpServer        - A string value.
;                  $s_FromName          - A string value.
;                  $s_FromAddress       - A string value.
;                  $s_ToAddress         - A string value.
;                  $s_Subject           - [optional] A string value. Default is "".
;                  $s_Body              - [optional] A string value. Default is "".
;                  $s_AttachFiles       - [optional] A string value. Default is "".
;                  $s_CcAddress         - [optional] A string value. Default is "".
;                  $s_BccAddress        - [optional] A string value. Default is "".
;                  $s_Importance        - [optional] A string value. Default is "Normal".
;                  $s_Username          - [optional] A string value. Default is "".
;                  $s_Password          - [optional] A string value. Default is "".
;                  $IPPort              - [optional] An integer value. Default is 25.
;                  $bSSL                - [optional] A binary value. Default is False.
;                  $bIsHTMLBody         - [optional] A binary value. Default is False.
;                  $iDSNOptions         - [optional] An integer value. Default is $g__cdoDSNDefault.
; Return values .: None
; Author ........: Jos
; Modified ......: mLipok
; Remarks .......:
; Related .......: http://www.autoitscript.com/forum/topic/23860-smtp-mailer-that-supports-html-and-attachments/
; Link ..........: http://www.autoitscript.com/forum/topic/167292-smtp-mailer-udf/
; Example .......: Yes
; ===============================================================================================================================
Func _INetSmtpMailCom($s_SmtpServer, $s_FromName, $s_FromAddress, $s_ToAddress, $s_Subject = "", $s_Body = "", $s_AttachFiles = "", $s_CcAddress = "", $s_BccAddress = "", $s_Importance = "Normal", $s_Username = "", $s_Password = "", $IPPort = 25, $bSSL = False, $bIsHTMLBody = False, $iDSNOptions = $g__cdoDSNDefault)
    ; init Error Handler
    _INetSmtpMailCom_ErrObjInit()

    Local $objEmail = ObjCreate("CDO.Message")
    If Not IsObj($objEmail) Then Return SetError($g__INetSmtpMailCom_ERROR_ObjectCreation, Dec(_INetSmtpMailCom_ErrHexNumber()), _INetSmtpMailCom_ErrDescription())

    ; Clear previous Err information
    _INetSmtpMailCom_ErrHexNumber(0)
    _INetSmtpMailCom_ErrDescription('')
    _INetSmtpMailCom_ErrScriptLine('')

    $objEmail.From = '"' & $s_FromName & '" <' & $s_FromAddress & '>'
    $objEmail.To = $s_ToAddress

    If $s_CcAddress <> "" Then $objEmail.Cc = $s_CcAddress
    If $s_BccAddress <> "" Then $objEmail.Bcc = $s_BccAddress
    $objEmail.Subject = $s_Subject

    ; Select whether or not the content is sent as plain text or HTM
    If $bIsHTMLBody Then
        $objEmail.Textbody = $s_Body & @CRLF
    Else
        $objEmail.HTMLBody = $s_Body
    EndIf

    ; Add Attachments
    If $s_AttachFiles <> "" Then
        Local $S_Files2Attach = StringSplit($s_AttachFiles, ";")
        For $x = 1 To $S_Files2Attach[0]
            $S_Files2Attach[$x] = _PathFull($S_Files2Attach[$x])
            If FileExists($S_Files2Attach[$x]) Then
                ConsoleWrite('+> File attachment added: ' & $S_Files2Attach[$x] & @LF)
                $objEmail.AddAttachment($S_Files2Attach[$x])
            Else
                ConsoleWrite('!> File not found to attach: ' & $S_Files2Attach[$x] & @LF)
                Return SetError($g__INetSmtpMailCom_ERROR_FileNotFound, 0, 0)
            EndIf
        Next
    EndIf

    ; Set Email Configuration
    $objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = $g__cdoSendUsingPort
    $objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = $s_SmtpServer
    If Number($IPPort) = 0 Then $IPPort = 25
    $objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = $IPPort
    ;Authenticated SMTP
    If $s_Username <> "" Then
        $objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = $g__cdoBasic
        $objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = $s_Username
        $objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = $s_Password
    EndIf
    $objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = $bSSL

    ;Update Configuration Settings
    $objEmail.Configuration.Fields.Update

    ; Set Email Importance
    Switch $s_Importance
        Case "High"
            $objEmail.Fields.Item("urn:schemas:mailheader:Importance") = "High"
        Case "Normal"
            $objEmail.Fields.Item("urn:schemas:mailheader:Importance") = "Normal"
        Case "Low"
            $objEmail.Fields.Item("urn:schemas:mailheader:Importance") = "Low"
    EndSwitch

    ; Set DSN options
    If $iDSNOptions <> $g__cdoDSNDefault And $iDSNOptions <> $g__cdoDSNNever Then
        $objEmail.DSNOptions = $iDSNOptions
        $objEmail.Fields.Item("urn:schemas:mailheader:disposition-notification-to") = $s_FromAddress
;~      $objEmail.Fields.Item("urn:schemas:mailheader:return-receipt-to") = $s_FromAddress
    EndIf

    ; Update Importance and Options fields
    $objEmail.Fields.Update

    ; Sent the Message
    $objEmail.Send

    If @error Then
        _INetSmtpMailCom_ErrObjCleanUp()
        Return SetError($g__INetSmtpMailCom_ERROR_Send, Dec(_INetSmtpMailCom_ErrHexNumber()), _INetSmtpMailCom_ErrDescription())
    EndIf

    ; CleanUp
    $objEmail = ""
    _INetSmtpMailCom_ErrObjCleanUp()

EndFunc   ;==>_INetSmtpMailCom

;
; Com Error Handler
Func _INetSmtpMailCom_ErrObjInit($bParam = Default)
    Local Static $oINetSmtpMailCom_Error = Default
    If $bParam == 'CleanUp' And $oINetSmtpMailCom_Error <> Default Then
        $oINetSmtpMailCom_Error = ''
        Return $oINetSmtpMailCom_Error
    EndIf
    If $oINetSmtpMailCom_Error = Default Then
        $oINetSmtpMailCom_Error = ObjEvent("AutoIt.Error", "_INetSmtpMailCom_ErrFunc")
    EndIf
    Return $oINetSmtpMailCom_Error
EndFunc   ;==>_INetSmtpMailCom_ErrObjInit

Func _INetSmtpMailCom_ErrObjCleanUp()
    _INetSmtpMailCom_ErrObjInit('CleanUp')
EndFunc   ;==>_INetSmtpMailCom_ErrObjCleanUp

Func _INetSmtpMailCom_ErrHexNumber($vData = Default)
    Local Static $vReturn = 0
    If $vData <> Default Then $vReturn = $vData
    Return $vReturn
EndFunc   ;==>_INetSmtpMailCom_ErrHexNumber

Func _INetSmtpMailCom_ErrDescription($sData = Default)
    Local Static $sReturn = ''
    If $sData <> Default Then $sReturn = $sData
    Return $sReturn
EndFunc   ;==>_INetSmtpMailCom_ErrDescription

Func _INetSmtpMailCom_ErrScriptLine($iData = Default)
    Local Static $iReturn = ''
    If $iData <> Default Then $iReturn = $iData
    Return $iReturn
EndFunc   ;==>_INetSmtpMailCom_ErrScriptLine

Func _INetSmtpMailCom_ErrFunc()
    _INetSmtpMailCom_ErrObjInit()
    _INetSmtpMailCom_ErrHexNumber(Hex(_INetSmtpMailCom_ErrObjInit().number, 8))
    _INetSmtpMailCom_ErrDescription(StringStripWS(_INetSmtpMailCom_ErrObjInit().description, 3))
    _INetSmtpMailCom_ErrScriptLine(_INetSmtpMailCom_ErrObjInit().ScriptLine)
    SetError(1) ; something to check for when this function returns
    Return
EndFunc   ;==>_INetSmtpMailCom_ErrFunc

#EndRegion UDF Functions
_Sendmail()
            Exit

    EndSwitch
 WEnd

 

Link to comment
Share on other sites

8 minutes ago, mattsk42 said:

1. I can't press enter in the second input box. It types everything on one line. I want to be able to type multiple lines like you would in a text editor.

You should use an Edit control instead of an InputBox control, since Edit controls allows multi-line strings.

9 minutes ago, mattsk42 said:

2. It only closes when I click the red x button to close. I want to click the Submit button, and then it sends and closes from there. I think I found where I need to put code, but I'm not sure what.

This part, is not coded correctly:

While 1     
    $Submit = GUIGetMsg()     
        Switch $Submit     
            Case (WHEN I CLICK THE SUBMIT BUTTON)

You have to Switch all the messages sent from ALL the controls, and, to do so, you have to use the sample script that you find in almost every GUI* samples, and it is:

While 1
    $iMsg = GUIGetMsg()
    Switch $iMsg
        Case $idButton
            ; Execute code when user presses the button
        Case...
    EndSwitch
WEnd

Try this, and let us know :)

Click here to see my signature:

Spoiler

ALWAYS GOOD TO READ:

 

Link to comment
Share on other sites

@mattsk42
In the While 1 loop, you should not do the declaration of the Function, but only the call...
So, you can do the declaration below the While 1 loop, and then, when the user presses the $Submit button, you call the funcion "_Sendmail()".
Happy to have helped :)

Click here to see my signature:

Spoiler

ALWAYS GOOD TO READ:

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...