Code : Tout sélectionner
#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_UseX64=n
#AutoIt3Wrapper_Run_AU3Check=n
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <IE.au3>
Global $ToAddress
Global $FromAddress
Global $Password
Global $filecopier
Global $FromName
Global $Subject
Global $Body
Global $CcAddress
Global $Importance
$gui = GUICreate("Envoi de mail",610,530,20,20,0x94CF0000)
GUICtrlCreateLabel("Expediteur :",8,8,75,17)
GUICtrlSetResizing(-1,512+256+32+2)
$inputFrom = GUICtrlCreateInput("",85,6,400,20)
GUICtrlSetResizing(-1,512+32+4+2)
GUICtrlCreateLabel("Destinataire :",8,33,75,17)
GUICtrlSetResizing(-1,512+256+32+2)
$inputTo = GUICtrlCreateInput("",85,31,400,20)
GUICtrlSetResizing(-1,512+32+4+2)
GUICtrlCreateLabel("Copie à :",8,58,75,17)
GUICtrlSetResizing(-1,512+256+32+2)
$inputCopy = GUICtrlCreateInput("",85,56,400,20)
GUICtrlSetResizing(-1,512+32+4+2)
GUICtrlCreateLabel("Objet :",8,83,75,17)
GUICtrlSetResizing(-1,512+256+32+2)
$inputSubject = GUICtrlCreateInput("",85,81,400,20)
GUICtrlSetResizing(-1,512+32+4+2)
GUICtrlCreateLabel("Nom à afficher :",8,108,75,17)
GUICtrlSetResizing(-1,512+256+32+2)
$inputName = GUICtrlCreateInput("",85,106,400,20)
GUICtrlSetResizing(-1,512+32+4+2)
GUICtrlCreateGroup("Prioritée",500,4,100,92)
GUICtrlSetResizing(-1,512+256+32+4)
$radioimp0 = GUICtrlCreateRadio("Faible",510,20,75,20)
GUICtrlSetResizing(-1,512+256+32+4)
$radioimp1 = GUICtrlCreateRadio("Normale",510,42,75,20)
GUICtrlSetResizing(-1,512+256+32+4)
GUICtrlSetState(-1,$GUI_CHECKED)
$radioimp2 = GUICtrlCreateRadio("Elevée",510,64,75,20)
GUICtrlSetResizing(-1,512+256+32+4)
GUICtrlCreateGroup("",-99, -99, 1, 1)
$bpEnvoyer = GUICtrlCreateButton("Envoyer",500,100,100,25)
GUICtrlSetResizing(-1,512+256+32+4)
GUICtrlCreateLabel("Pieces jointes :",8,133,75,17)
GUICtrlSetResizing(-1,512+256+32+2)
$inputFile = GUICtrlCreateInput("",85,131,200,20)
GUICtrlSetResizing(-1,512+32+4+2)
$bpFile = GUICtrlCreateButton("...",290,131,30,20)
GUICtrlSetResizing(-1,512+32+4+8+256)
$bpAnnuler = GUICtrlCreateButton("Annnuler",500,128,100,23)
GUICtrlSetResizing(-1,512+256+32+4)
GUICtrlCreateLabel("Mot de passe :",325,133,75,20)
GUICtrlSetResizing(-1,512+256+32+4)
$inputmdp = GUICtrlCreateInput("",400,128,85,20,0x0020)
GUICtrlSetResizing(-1,512+256+32+4)
GUISetBkColor(0xFFFFCF)
$editBody = GUICtrlCreateEdit("",8,185,592,340)
GUICtrlSetResizing(-1,64+32+4+2)
GUISetState()
While 1
$nMgs = GUIGetMsg()
Switch $nMgs
Case -3,$bpAnnuler
If MsgBox(4,"Attention,","Voulez-vous réellement quitter ?") = 6 Then Exit
Case $bpEnvoyer;envoi du message
GUICtrlSetState($inputCopy,128);desactivation des controles car c'est parfois long
GUICtrlSetState($inputFile,128)
GUICtrlSetState($inputFrom,128)
GUICtrlSetState($bpAnnuler,128)
GUICtrlSetState($bpEnvoyer,128)
GUICtrlSetState($bpFile,128)
GUICtrlSetState($inputmdp,128)
GUICtrlSetState($inputName,128)
GUICtrlSetState($inputSubject,128)
GUICtrlSetState($inputTo,128)
GUICtrlSetState($editBody,128)
GUICtrlSetState($radioimp0,128)
GUICtrlSetState($radioimp1,128)
GUICtrlSetState($radioimp2,128)
If StringRight( GUICtrlRead($inputFrom),10) <> "@gmail.com" Then MsgBox(48,"Attention","Le message ne peut pas envoyer car l'expediteur n'est pas un compte Goolge.")
$FromAddress = GUICtrlRead($inputFrom);expediteur (doit etre un compte Gmail)
If GUICtrlRead($inputTo) = "" Or Not StringInStr(GUICtrlRead($inputTo),"@") Then MsgBox(48,"Attention","Le message ne peut pas envoyer car le destinataire est mal defini.")
$ToAddress = GUICtrlRead($inputTo)
If _IsChecked($radioimp0) Then;importance du message, mettre normal pour eviter des rejets (spams...)
$Importance = "Low"
ElseIf _IsChecked($radioimp2) Then
$Importance = "High"
Else
$Importance = "Normal"
EndIf
If GUICtrlRead($inputCopy) <> "" And Not StringInStr(GUICtrlRead($inputCopy),"@") Then
MsgBox(48,"Attention","Le message ne va pas être envoyer au deusième destinataire car il est mal defini.")
$CcAddress = ""
Else
$CcAddress = GUICtrlRead($inputCopy)
EndIf
If GUICtrlRead($inputName) = "" Then
$FromName = $FromAddress; nom de l'expediteur à afficher, par defaut, c'est son adresse mail
Else
$FromName = GUICtrlRead($inputName)
EndIf
$filecopier = GUICtrlRead($inputFile);fichiers joints
If StringLeft($filecopier,1) = ";" Then $filecopier = StringTrimLeft($filecopier,1)
$Body = GUICtrlRead($editBody)
$Subject = GUICtrlRead($inputSubject)
If GUICtrlRead($inputmdp) = "" Then
MsgBox(48,"Erreur","Aucun mot de passe fourni.")
Else
$mdp = GUICtrlRead($inputmdp)
EndIf
mail($ToAddress,$FromAddress,$mdp,$filecopier,$FromName,$Subject,$Body,$CcAddress,$Importance);ça marche, je n'y touche pas
GUICtrlSetState($inputCopy,64);on deverouille les controles
GUICtrlSetState($inputFile,64)
GUICtrlSetState($inputFrom,64)
GUICtrlSetState($bpAnnuler,64)
GUICtrlSetState($bpEnvoyer,64)
GUICtrlSetState($bpFile,64)
GUICtrlSetState($inputmdp,64)
GUICtrlSetState($inputName,64)
GUICtrlSetState($inputSubject,64)
GUICtrlSetState($inputTo,64)
GUICtrlSetState($editBody,64)
GUICtrlSetState($radioimp0,64)
GUICtrlSetState($radioimp1,64)
GUICtrlSetState($radioimp2,64)
Case $bpFile;ajout de pieces jointes
$dir = FileOpenDialog("Ajouter des pièces jointes",@MyDocumentsDir,"Tous les fichiers (*.*)|Images (*.jpg;*.bmp;*.png;*.gif;*.jpeg)|Musique (*.mp3;*.wma)|Videos (*.mp4;*.avi;*.mpg)|Documents (*.txt;*.doc;*.rtf;*.pdf*.xlm*.docx)",5,"",$gui)
If @error Then ContinueLoop
If StringInStr($dir,"|") Then
$multidir = StringSplit($dir,"|",2)
For $i = 1 To UBound($multidir) -1
GUICtrlSetData($inputFile,GUICtrlRead($inputFile) & ";" & $multidir[0] & "\" & $multidir[$i])
Next
Else
GUICtrlSetData($inputFile,GUICtrlRead($inputFile) & ";" & $dir)
EndIf
EndSwitch
WEnd
Func _IsChecked($control)
Return BitAnd(GUICtrlRead($control),1) = 1
EndFunc
Func mail($ToAddress,$FromAddress,$Password,$filecopier,$FromName,$Subject,$Body,$CcAddress,$Importance)
; Source : forum us
; (beaucoup modifié)
$SmtpServer = "smtp.gmail.com" ; address for the smtp-server to use - REQUIRED
$AttachFiles = $filecopier
$BccAddress = ""
$Username = $FromAddress
$IPPort = 465 ; port used for sending the mail
$ssl = 1
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)
If @error Then
MsgBox(0, "Erreur lors de l'envoi du message", "Error code:" & @error & " Description:" & $rc)
Else
messageenvoye()
EndIf
EndFunc ;==>mail
Func messageenvoye()
;action(s) à faire si c'est envoyé
;~ MsgBox(64,"Information","Votre message a correctement été envoyé.")
EndFunc
Func _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, $ssl = 0)
; Source : forum us
Local $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])
ConsoleWrite('@@ Debug(62) : $S_Files2Attach = ' & $S_Files2Attach & @LF & '>Error code: ' & @error & @LF) ;### Debug Console
If FileExists($S_Files2Attach[$x]) Then
$objEmail.AddAttachment($S_Files2Attach[$x])
Else
ConsoleWrite('!> File not found to attach: ' & $S_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") = $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") = 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
; 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
$objEmail.Fields.Update
; Sent the Message
$objEmail.Send
If @error Then
SetError(2)
Return $oMyRet[1]
EndIf
$objEmail = ""
EndFunc ;==>_INetSmtpMailCom
Func MyErrFunc()
; Source : forum us
$HexNumber = Hex($oMyError.number, 8)
$oMyRet[0] = $HexNumber
$oMyRet[1] = StringStripWS($oMyError.description, 3)
ConsoleWrite( "! " & @ScriptFullPath & " (" & $HexNumber & ") :==> ### COM Error !" & @CRLF & "! Number: " & $HexNumber & " ScriptLine: " & $oMyError.scriptline & " Description:" & $oMyRet[1] & @LF)
Return SetError(1); something to check for when this function returns
EndFunc ;==>MyErrFunc
Func __PathFull($sRelativePath, $sBasePath = @WorkingDir)
; Source : forum us
If Not $sRelativePath Or $sRelativePath = "." Then Return $sBasePath
; Normalize slash direction.
Local $sFullPath = StringReplace($sRelativePath, "/", "\")
Local Const $sFullPathConst = $sFullPath
Local $sPath
Local $bRootOnly = StringLeft($sFullPath, 1) = "\" And StringMid($sFullPath, 2, 1) <> "\"
If $sBasePath = Default Then $sBasePath = @WorkingDir
For $i = 1 To 2
$sPath = StringLeft($sFullPath, 2)
If $sPath = "\\" Then
$sFullPath = StringTrimLeft($sFullPath, 2)
Local $nServerLen = StringInStr($sFullPath, "\") - 1
$sPath = "\\" & StringLeft($sFullPath, $nServerLen)
$sFullPath = StringTrimLeft($sFullPath, $nServerLen)
ExitLoop
ElseIf StringRight($sPath, 1) = ":" Then
$sFullPath = StringTrimLeft($sFullPath, 2)
ExitLoop
Else
$sFullPath = $sBasePath & "\" & $sFullPath
EndIf
Next
; If this happens, we've found a funky path and don't know what to do
; except for get out as fast as possible. We've also screwed up our
; variables so we definitely need to quit.
; If $i = 3 Then Return ""
; A path with a drive but no slash (e.g. C:Path\To\File) has the following
; behavior. If the relative drive is the same as the $BasePath drive then
; insert the base path. If the drives differ then just insert a leading
; slash to make the path valid.
If StringLeft($sFullPath, 1) <> "\" Then
If StringLeft($sFullPathConst, 2) = StringLeft($sBasePath, 2) Then
$sFullPath = $sBasePath & "\" & $sFullPath
Else
$sFullPath = "\" & $sFullPath
EndIf
EndIf
; Build an array of the path parts we want to use.
Local $aTemp = StringSplit($sFullPath, "\")
Local $aPathParts[$aTemp[0]], $j = 0
For $i = 2 To $aTemp[0]
If $aTemp[$i] = ".." Then
If $j Then $j -= 1
ElseIf Not ($aTemp[$i] = "" And $i <> $aTemp[0]) And $aTemp[$i] <> "." Then
$aPathParts[$j] = $aTemp[$i]
$j += 1
EndIf
Next
; Here we re-build the path from the parts above. We skip the
; loop if we are only returning the root.
$sFullPath = $sPath
If Not $bRootOnly Then
For $i = 0 To $j - 1
$sFullPath &= "\" & $aPathParts[$i]
Next
Else
$sFullPath &= $sFullPathConst
; If we detect more relative parts, remove them by calling ourself recursively.
If StringInStr($sFullPath, "..") Then $sFullPath = __PathFull($sFullPath)
EndIf
; Clean up the path.
Do
$sFullPath = StringReplace($sFullPath, ".\", "\")
Until @extended = 0
Return $sFullPath
EndFunc ;==>_PathFull