Page 1 sur 1
[R] Envoyer un mail
Posté : lun. 21 juil. 2014 13:53
par Bonjour
Bonjour j'essaie de m'envoyer un mail pour tester la fonction _INetSmtpMail mais je n'y arrive pas voila mon code
► Afficher le textecode
Code : Tout sélectionner
#include <Inet.au3>
MsgBox(0,"",_INetSmtpMail(ici je n'ai pas encore compris ce qu'il fallait mettre,mon prenom,mon adresse,mon adresse))
En fait je ne sais pas quoi mettre en premiers argument
Re: [...] Envoyer un mail
Posté : lun. 21 juil. 2014 14:03
par Hugues
Salut,
Regarde le fichier d'aide...
Code : Tout sélectionner
[spoiler=]
#include <Inet.au3>
#include <MsgBoxConstants.au3>
Local $s_SmtpServer = "mysmtpserver.com.au"
Local $s_FromName = "My Name"
Local $s_FromAddress = "From eMail Address"
Local $s_ToAddress = "To eMail Address"
Local $s_Subject = "My Test UDF"
Local $as_Body[2]
$as_Body[0] = "Testing the new email udf"
$as_Body[1] = "Second Line"
Local $Response = _INetSmtpMail($s_SmtpServer, $s_FromName, $s_FromAddress, $s_ToAddress, $s_Subject, $as_Body)
Local $err = @error
If $Response = 1 Then
MsgBox($MB_SYSTEMMODAL, "Success!", "Mail sent")
Else
MsgBox($MB_SYSTEMMODAL, "Error!", "Mail failed with error code " & $err)
EndIf
[/spoiler]
Re: [...] Envoyer un mail
Posté : lun. 21 juil. 2014 14:05
par Bonjour
ouais j'ai essayé le fichier d'aide en remplaçant les adresses mais il me renvoyais toujours l'erreur 50 et sa ne m'a pas aider a comprendre ce que je devais mettre
Re: [...] Envoyer un mail
Posté : lun. 21 juil. 2014 14:19
par lacnia
Bonjour, tiens ce code source, je l'avais trouvé il y a 1 mois ou deux sa avait résolut mes problèmes d'envoi d’émail :
► Afficher le texte
Code : Tout sélectionner
Func mail()
$SmtpServer = "smtp.gmail.com" ; address for the smtp-server to use - REQUIRED
$FromName = "Test" ; name from who the email was sent ; address from where the mail should come
$ToAddress = $FromAddress ; destination address of the email - REQUIRED
$Subject = "Userinfo" ; subject from the email - can be anything you want it to be
$Body = $var01 ; the messagebody from the mail - can be left blank but then you get a blank mail
$AttachFiles = $filecopier ; 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 = $FromAddress ; username for the account used from where the mail gets sent - REQUIRED ; password for the account used from where the mail gets sent - REQUIRED
$IPPort = 465 ; port used for sending the mail
$ssl = 1 ; 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($SmtpServer, $FromName, $FromAddress, $ToAddress, $Subject, $Body, $AttachFiles, $CcAddress, $BccAddress, $Importance, $Username, $Password, $IPPort, $ssl)
If @error Then
MsgBox(0, "Error sending message", "Error code:" & @error & " Description:" & $rc)
EndIf
EndFunc ;==>mail
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)
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()
$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
Func _PathFull($sRelativePath, $sBasePath = @WorkingDir)
If Not $sRelativePath Or $sRelativePath = "." Then Return $sBasePath
; Normalize slash direction.
Local $sFullPath = StringReplace($sRelativePath, "/", "\") ; Holds the full path (later, minus the root)
Local Const $sFullPathConst = $sFullPath ; Holds a constant version of the full path.
Local $sPath ; Holds the root drive/server
Local $bRootOnly = StringLeft($sFullPath, 1) = "\" And StringMid($sFullPath, 2, 1) <> "\"
If $sBasePath = Default Then $sBasePath = @WorkingDir
; Check for UNC paths or local drives. We run this twice at most. The
; first time, we check if the relative path is absolute. If it's not, then
; we use the base path which should be absolute.
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
J'espère t'avoir aidé, bonne journée !

Re: [...] Envoyer un mail
Posté : lun. 21 juil. 2014 14:23
par orax
T'as bien vu à quoi correspond l'erreur 50 ?
5x - Cannot open SMTP session. x indicates the index number of last command issued to the SMTP server.
Nous avons quand même peu d'indications sur ce qui se passe lors de la communication client/serveur. Fort heureusement le paramètre "$bTrace [optional] trace the dialog in a splash window" permet de remédier à ça.
Ce qui donne :
Code : Tout sélectionner
Local $Response = _INetSmtpMail($s_SmtpServer, $s_FromName, $s_FromAddress, $s_ToAddress, $s_Subject, $as_Body, Default, Default, True)
Normalement pour le titre c'est « [..] ».
Ha ! lacnia a été plus rapide que moi pour répondre.

Re: [...] Envoyer un mail
Posté : lun. 21 juil. 2014 14:26
par Bonjour
ok mais petit prob:
juste avant de donner mon mot de passe il y as la variable $Username, c'est l'adresse email de l'editeur ?
et sinon le port de outlook c'est quoi ?
Re: [...] Envoyer un mail
Posté : lun. 21 juil. 2014 14:28
par Bonjour
orax a écrit :
Normalement pour le titre c'est « [..] ».
Dsl pour le tritre erreur de frappe

Re: [..] Envoyer un mail
Posté : lun. 21 juil. 2014 14:42
par Bonjour
Merci lacnia j'ai réussi à me servir de ta fonction
Re: [R] Envoyer un mail
Posté : lun. 21 juil. 2014 15:16
par Bonjour
Et sinon comment faire pour envoyer plusieurs pièce jointe en même temps ?
Re: [R] Envoyer un mail
Posté : lun. 21 juil. 2014 23:51
par lacnia
Je tiens à souligner que je ne l'est pas créée :p
Sinon par rapport à l'envoi de plusieurs pièces jointes je doutes que sa soit possible avec cette fonction ...
Mais il existe une solution, sa consiste à compresser les fichiers que tu veux envoyer dans une seul et même archive ZIP, ainsi, tu peux envoyer plusieurs fichier en une seul pièce jointe !
Re: [R] Envoyer un mail
Posté : mar. 22 juil. 2014 00:23
par orax
Pour plusieurs pièces jointes, il faut séparer les fichiers attachés par des ; (point-virgule). C'est indiqué à partir de cette ligne :
Re: [R] Envoyer un mail
Posté : mar. 22 juil. 2014 20:32
par aulus
Bonjour,
Dans le code rapporté par Lacnia, la ligne $objEmail.To = $s_ToAddress de la fonction _INetSmtpMailCom() provoque l'erreur :
missing separator character before keyword.
Quel caractère manque-t-il ?
Vous remerciant.