Jump to content

smtp for multiple emails with attachments


JMT
 Share

Recommended Posts

#Include<file.au3>

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

;##################################

; Include

;##################################

#Include<file.au3>

;##################################

; Variables

;##################################

$s_SmtpServer = "MailServer" ; address for the smtp-server to use - REQUIRED

$s_FromName = "Name" ; name from who the email was sent

$s_FromAddress = "your@Email.Address.com" ; address from where the mail should come

$s_ToAddress = "your@Email.Address.com" ; destination address of the email - REQUIRED

$s_Subject = "Userinfo" ; subject from the email - can be anything you want it to be

$as_Body = "" ; the messagebody from the mail - can be left blank but then you get a blank mail

$s_AttachFiles = "" ; the file you want to attach- leave blank if not needed

$s_CcAddress = "CCadress1@test.com" ; address for cc - leave blank if not needed

$s_BccAddress = "BCCadress1@test.com" ; address for bcc - leave blank if not needed

$s_Username = "******" ; username for the account used from where the mail gets sent - REQUIRED

$s_Password = "********" ; password for the account used from where the mail gets sent - REQUIRED

$IPPort = 25 ; port used for sending the mail

$ssl = 0 ; enables/disables secure socket layer sending - put to 1 if using httpS

;~ $IPPort=465 ; GMAIL port used for sending the mail

;~ $ssl=1 ; GMAILenables/disables secure socket layer sending - put to 1 if using httpS

;##################################

; Script

;##################################

Global $oMyRet[2]

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

$rc = _INetSmtpMailCom($s_SmtpServer, $s_FromName, $s_FromAddress, $s_ToAddress, $s_Subject, $as_Body, $s_AttachFiles, $s_CcAddress, $s_BccAddress, $s_Username, $s_Password, $IPPort, $ssl)

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 = "",$IPPort=25, $ssl=0)

$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.Bcc = $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") = $IPPort

;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

If $Ssl Then

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

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

Is there a way to call a different function that would create an array of people to send email with attachments to and set the $s_ToAddress and $s_AttachFiles variable based on an array or something?

I have 30 people in my department, each person gets a custom report generated for them weekly and dropped in a folder. I want to email the file out of the individuals folder to the individual.

Thanks in advance!

Link to comment
Share on other sites

? Did you actually write all of that code yourself? Or maybe I just don't understand your question...

You can make an array with all the email address you need and an array with the filename you need... or get a bit more complicated and make a 2-d array.

Dim $rEmails[30], $rFiles[30]
$rEmails[0] = "someemail@whatever.com"
$rFiles[0] = "someemailsfile.txt"
...

Dim $rData[30][2]
$rData[0][0] = "someemail@whatever.com"
$rData[0][1] = "someemailsfile.txt"

Then just loop through with a for-loop, calling INetSmtpMailCom() with the appropriate arguments. Look up for-next in the help file

Does this make sense? Or did I misunderstand your question completely?

My Code:- _TocLib - UDF for TOC protocol (The simplified one used by 3rd party AIM/ICQ clients)

Link to comment
Share on other sites

I found the code on this site.

It am very new at this. I use the Help files where I can, just not familiar with what I should actually be looking for.

I created an array before the send email code. Autoit only does 21 names in an array, so I split it up in to 2 arrays. I then checked out that For...Next and put that in the variable section of the send email code.

It actually works. It is pretty great!

Thanks for the info and Thank Autoit for the Help files!

Link to comment
Share on other sites

<...> Autoit only does 21 names in an array, so I split it up in to 2 arrays.<...>

??? I don't get this. An array can contain sixteen million sevenhundred seventy-seven thousand two hundred and fifteen elements.

Read: 16777215, structurally 255 + 256*255 + 65536 * 255, or even more structurally 255 + 2^8*255 + 2^16*255, or even more more structurally 2^8-1 + 2^8*(2^8-1) + 2^16*(2^8-1).

Roses are FF0000, violets are 0000FF... All my base are belong to you.

Link to comment
Share on other sites

  • Moderators

??? I don't get this. An array can contain sixteen million sevenhundred seventy-seven thousand two hundred and fifteen elements.

Read: 16777215, structurally 255 + 256*255 + 65536 * 255, or even more structurally 255 + 2^8*255 + 2^16*255, or even more more structurally 2^8-1 + 2^8*(2^8-1) + 2^16*(2^8-1).

I don't think he understands how to do arrays, so he's using _ArrayCreate... and is getting confused on the remarks:

Remarks

Arrays up to 21 elements are supported.

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

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