Jump to content

Recommended Posts

  • Developers
Posted (edited)
On 12/9/2024 at 5:34 PM, Jos said:

Just had a look at this, and something like this change to my UDF will make it work for me.

Just use the defined format defining the source file full path in the HTML body:

Global $oMyRet[2]
Global $oMyError = ObjEvent("AutoIt.Error", "MyErrFunc")
;##################################
; Include
;##################################
#include <file.au3>
;##################################
; Variables
;##################################
$SmtpServer = "smtp.gmail.com"              ; address for the smtp-server to use - REQUIRED
$FromName = "Jos"                      ; name from who the email was sent
$FromAddress = "j....@gmail.com" ;  address from where the mail should come
$ToAddress   = "j....@gmail.com"   ; destination address of the email - REQUIRED
$Subject = "Test Gmail message with inline images"                   ; subject from the email - can be anything you want it to be
$Body = "<body> test email with 2 images inline:<br>" & _
        "first image: ##image#'C:\path-to-file\image1.png'##" & _
        "second image: ##image#C:\path-to-file\image2.png##" & _
        "<br></body>"                           ; the messagebody from the mail - can be left blank but then you get a blank mail
$AttachFiles = ""                       ; the file you want to attach- leave blank if not needed
$CcAddress = ""                         ; address for cc - leave blank if not needed
$BccAddress = ""                        ; address for bcc - leave blank if not needed
$Importance = "Normal"                  ; Send message priority: "High", "Normal", "Low"
$Username = "j....@gmail.com"           ; username for the account used from where the mail gets sent - REQUIRED
$Password = "--your password --"        ; password for the account used from where the mail gets sent - REQUIRED
$IPPort = 465                           ; GMAIL port used for sending the mail
$ssl = 1                                ; GMAIL enables/disables secure socket layer sending - put to 1 if using httpS
$tls = 0                                ; enables/disables TLS when required

;~ $SmtpServer = "smtp.gmail.com"       ; GMAIL address for the smtp-server to use - REQUIRED
;~ $IPPort=465                          ; GMAIL port used for sending the mail
;~ $ssl=1                               ; GMAIL enables/disables secure socket layer sending - put to 1 if using httpS
;~ $SmtpServer = "smtp.office365.com"   ; O365 address for the smtp-server to use - REQUIRED
;~ $IPPort=25                           ; O365 port used for sending the mail
;~ $ssl=1                               ; O365 enables/disables secure socket layer sending - put to 1 if using httpS
;~ SmtpServer = "smtp.mail.yahoo.com"   ; Yahoo address for the smtp-server to use - REQUIRED
;~ $IPPort = 465                        ; Yahoo port used for sending the mail
;~ $ssl = 1                             ; Yahoo enables/disables secure socket layer sending - put to 1 if using httpS

;##################################
; Script
;##################################
Global $oMyRet[2]
Global $oMyError = ObjEvent("AutoIt.Error", "MyErrFunc")
$rc = _INetSmtpMailCom($SmtpServer, $FromName, $FromAddress, $ToAddress, $Subject, $Body, $AttachFiles, $CcAddress, $BccAddress, $Importance, $Username, $Password, $IPPort, $ssl, $tls)
If @error Then
    MsgBox(0, "Error sending message", "Error code:" & @error & "  Description:" & $rc)
EndIf
;
; The UDF
Func _INetSmtpMailCom($SmtpServer, $FromName, $FromAddress, $ToAddress, $Subject = "", $as_Body = "", $AttachFiles = "", $CcAddress = "", $BccAddress = "", $Importance = "Normal", $Username = "", $Password = "", $IPPort = 25, $ssl = 0, $tls = 0)
    Local $objEmail = ObjCreate("CDO.Message")
    ; convert the Fromname to UTF-8 basex64 to allow for UTF characters in the name
    If $FromName <> "" Then $FromName = "=?utf-8?B?" & _Base64Encode(BinaryToString(StringToBinary($FromName, 4), 1)) & '?='
    $objEmail.From = '"' & $FromName & '" <' & $FromAddress & '>'
    $objEmail.To = $ToAddress
    Local $i_Error = 0
    Local $i_Error_desciption = ""
    If $CcAddress <> "" Then $objEmail.Cc = $CcAddress
    If $BccAddress <> "" Then $objEmail.Bcc = $BccAddress
    $objEmail.Subject = $Subject
    If StringInStr($as_Body, "<") And StringInStr($as_Body, ">") Then
        ; add embedded images when defined
        Local $aImages = 0, $iImgCnt = 0
        $aImages = StringRegExp($as_Body, "##image#['""]?([^#'""]*)['""]?##", $STR_REGEXPARRAYGLOBALMATCH)
        $iImgCnt += 1
        For $i = 0 To UBound($aImages) - 1
            $iImgCnt += 1
            $fileext = StringRegExpReplace($aImages[$i], ".*(\.+.*)", "$1")
            $as_Body = StringRegExpReplace($as_Body, "(##image#['""]?([^#'""]*)['""]?##)", "<img src=" & $iImgCnt & $fileext & ">", 1)
            $objBP = $objEmail.AddRelatedBodyPart($aImages[$i], $iImgCnt & $fileext, 1)
            $objBP.Fields.Item("urn:schemas:mailheader:Content-ID") = "<" & $iImgCnt & $fileext & ">"
            $objBP.Fields.Update
            ConsoleWrite('+> Image placed inline: ' & $aImages[$i] & @LF)
        Next
        $objEmail.HTMLBody = $as_Body
        $objEmail.HTMLBodyPart.CharSet = "UTF-8"
    Else
        $objEmail.Textbody = $as_Body & @CRLF
        $objEmail.TextBodyPart.CharSet = "UTF-8"
    EndIf
    If $AttachFiles <> "" Then
        Local $Files2Attach = StringSplit($AttachFiles, ";")
        For $x = 1 To $Files2Attach[0]
            $Files2Attach[$x] = _PathFull($Files2Attach[$x])
            If FileExists($Files2Attach[$x]) Then
                ConsoleWrite('+> File attachment added: ' & $Files2Attach[$x] & @LF)
                $objEmail.AddAttachment($Files2Attach[$x])
            Else
                ConsoleWrite('!> File not found to attach: ' & $Files2Attach[$x] & @LF)
                SetError(1)
                Return 0
            EndIf
        Next
    EndIf
    $objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
    $objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = $SmtpServer
    If Number($IPPort) = 0 Then $IPPort = 25
    $objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = $IPPort
    ;Authenticated SMTP
    If $Username <> "" Then
        $objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
        $objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = $Username
        $objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = $Password
    EndIf
    ; Set security params
    If $ssl Then $objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True
    If $tls Then $objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendtls") = True
    ;Update settings
    $objEmail.Configuration.Fields.Update
    ; Set Email Importance
    Switch $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
    $objEmail.Fields.Update
    ; Sent the Message
    $objEmail.Send
    If @error Then
        SetError(2)
        Return $oMyRet[1]
    EndIf
    $objEmail = ""
EndFunc   ;==>_INetSmtpMailCom
;
;
; Com Error Handler
Func MyErrFunc()
    $HexNumber = Hex($oMyError.number, 8)
    $oMyRet[0] = $HexNumber
    $oMyRet[1] = StringStripWS($oMyError.description, 3)
    ConsoleWrite("### COM Error !  Number: " & $HexNumber & "   ScriptLine: " & $oMyError.scriptline & "   Description:" & $oMyRet[1] & @LF)
    SetError(1) ; something to check for when this function returns
    Return
EndFunc   ;==>MyErrFunc

; Base64 UDF made by Trancexx https://www.autoitscript.com/forum/topic/84133-winhttp-functions/?do=findComment&comment=1260746
Func _Base64Encode($vInput)
    Local Const $CRYPT_STRING_BASE64 = 0x00000001
    Local Const $CRYPT_STRING_NOCRLF = 0x40000000

    $vInput = Binary($vInput)
    Local $tInput = DllStructCreate("byte[" & BinaryLen($vInput) & "]")
    DllStructSetData($tInput, 1, $vInput)

    Local $aCall = DllCall("Crypt32.dll", "bool", "CryptBinaryToStringA", _
            "struct*", $tInput, _
            "dword", DllStructGetSize($tInput), _
            "dword", $CRYPT_STRING_BASE64 + $CRYPT_STRING_NOCRLF, _
            "ptr", 0, _
            "dword*", 0)
    If @error Or Not $aCall[0] Then Return SetError(1, 0, "") ; error calculating the length of the needed buffer

    Local $tOut = DllStructCreate("char[" & $aCall[5] & "]")

    $aCall = DllCall("Crypt32.dll", "int", "CryptBinaryToStringA", _
            "struct*", $tInput, _
            "dword", DllStructGetSize($tInput), _
            "dword", $CRYPT_STRING_BASE64 + $CRYPT_STRING_NOCRLF, _
            "struct*", $tOut, _
            "dword*", DllStructGetSize($tOut))
    If @error Or Not $aCall[0] Then Return SetError(2, 0, "") ; error encoding

    Return DllStructGetData($tOut, 1)
EndFunc   ;==>_Base64Encode

 

Updated code which now allows quoted or unquoted filenames. Change the marker characters to ##.
@Jemboy,
Does it now work for you?

 

 

Edited by Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

  • Developers
Posted
On 12/11/2024 at 1:20 PM, Jos said:

Updated code which now allows quoted or unquoted filenames. Change the marker characters to ##.
@Jemboy,
Does it now work for you?

@Jemboy Thanks for the courtesy of not answering. Will return the favor the next time. 

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

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
  • Recently Browsing   0 members

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