Jump to content

Multiple email attachments from script


Recommended Posts

I'm using a script to send out an email with an attachment. i'm trying to tweak it so that it will send out multiple attachments who's file name will change every hour. i'm trying to figure out a way to attach these files either with wildcards or by telling the script to attach all files in a single folder. i'm by no means a programmer so any help will be appreciated. the script and filename format is below.

file names: DICKSord041120111125AM.dat (the files will have the date and time they are created as part of the file name so they are never static)

script:

#include<file.au3>

Global $oMyRet[2]

Global $oMyError = ObjEvent("AutoIt.Error", "MyErrFunc")

$rc = _INetSmtpMailCom('server.domain.com', "EDI", "######@daisy.com", "#####@daisy.com", "Orders Subject", "Edi Orders", "Z:\Orders\*.*", "","", "#######", "######")

If @error then

msgbox(0,"Error sending message","Error code:" & @error & " Description:" & $rc)

EndIf

Func _INetSmtpMailCom($s_SmtpServer, $s_FromName, $s_FromAddress, $s_ToAddress, $s_Subject = "", $as_Body = "", $s_AttachFiles = "", $s_CcAddress = "", $s_BccAddress = "", $s_Username = "", $s_Password = "")

$objEmail = ObjCreate("CDO.Message")

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

$objEmail.To = $s_ToAddress

Local $i_Error = 0

Local $i_Error_desciption = ""

If $s_CcAddress <> "" Then $objEmail.Cc = $s_CcAddress

If $s_BccAddress <> "" Then $objEmail.Cc = $s_BccAddress

$objEmail.Subject = $s_Subject

If StringInStr($as_Body,"<") and StringInStr($as_Body,">") Then

$objEmail.HTMLBody = $as_Body

Else

$objEmail.Textbody = $as_Body & @CRLF

EndIf

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

$objEmail.AddAttachment ($S_Files2Attach[$x])

Else

$i_Error_desciption = $i_Error_desciption & @lf & 'File not found to attach: ' & $S_Files2Attach[$x]

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") = $s_SmtpServer

$objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25

;Authenticated SMTP

If $s_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") = $s_Username

$objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendpassword") = $s_Password

EndIf

;Update settings

$objEmail.Configuration.Fields.Update

; Sent the Message

$objEmail.Send

if @error then

SetError(2)

return $oMyRet[1]

EndIf

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

Link to comment
Share on other sites

You could do something like:

#Include <File.au3>
#include <INet.au3>
$aArray = _FileListToArray($sPath , "DICKSord*.dat", 1)
; Do your error checking of _FileListToArray here
$sAtt = _ArrayToString($aArray, ";", 1)
$rc = _INetSmtpMailCom('server.domain.com', "EDI", "######@daisy.com",  "#####@daisy.com", "Orders Subject", "Edi Orders", $sAtt,  "","", "#######", "######")
Edited by water

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

i get where to tell it to attach a file/files, the problem is that the names of the files change due to the date and time stamp on the end of the file name. it won't let me use a wildcard to define the file names to attach. i'm also trying to understand the array above. it almost makes sense on how to use it. if i need say 15 entries in the array, how would i tweak it to accomodate that?

Edited by wmarques
Link to comment
Share on other sites

My code takes all the files starting with string "DICKSord" and extension "dat" (first line) and creates a string of filenames separated by a semicolon.

But I think this won't work as $aArray only contains the filenames but no path information. So my first try has to be changes as follows.

I'm not at my Windows PC but I think this should work:

$sPath = "C:\temp"                                                  ; path where your attachements are located 
$aArray = _FileListToArray($sPath , "DICKSord*.dat", 1)             ; create an array of all files (filtered)
For $i = 1 to $aArray[0]                                            ; prepend the path to every filename
    $aArray[$i] = $sPath & "\" & $aArray[$i]
Next
$sAtt = _ArrayToString($aArray, ";", 1)                             ; create a string of all attachements separated by a semicolon
$rc = _INetSmtpMailCom('server.domain.com', "EDI", "######@daisy.com",  "#####@daisy.com", "Orders Subject", "Edi Orders", $sAtt,  "","", "#######", "######")

If you only want to send some of the files from the array you must filter out the files you want to attach and create string $sAtt yourself.

How do you want to filter the files to send (by name ...)?

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

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...