Page 1 sur 1

Mail envoyer un message html

Publié : ven. 26/juil./2019 8:45
par microdevweb
Bonjour,

Je cherche à envoyer des messages email en html avec formulaire.

Après plusieurs recherches j'ai trouvé ceci de falsam

https://www.purebasic.fr/french/viewtop ... ml#p157407

Cela date de 2010, et le soucis est que je ne voie pas comment renseigné le login et psw pour le smtp.


J'ai essayé avec les commandes classiques et comme ceci mais le mail reçu est vide

EDIT:
Mauvais fichier html, mais apparaît dans les pièces jointes, ce qui ne me convient pas

J'ai masqué les adresse, login et psw évidement :mrgreen:

Code : Tout sélectionner

If InitNetwork()
  If CreateMail(0,"FROM","Teste html")
    SetMailAttribute(0, #PB_Mail_Custom, "Content-Disposition: inline")
    AddMailAttachment(0,"text/html", "index.html","text/html")
    SetMailBody(0,"")
    AddMailRecipient(0,"TO",#PB_Mail_To)
    Result = SendMail(0,"pro.eu.turbo-smtp.com",465,#PB_Mail_UseSSL,"LOGIN","PSW")
    
    Repeat
      Progress = MailProgress(0)
      Delay(300)
    Until Progress = #PB_Mail_Finished Or Progress = #PB_Mail_Error
    
    If Progress = #PB_Mail_Finished
      Debug "success"
    Else
      Debug "failure"
    EndIf
  EndIf
EndIf
Et le code de index.html

Code : Tout sélectionner

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
	
<html xmlns:v="urn:schemas-microsoft-com:vml">
	<head>
		<meta http-equiv="content-type" content="text/html; charset=utf-8">
		<meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0;"> 
	</head>
	<body leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
		<table bgcolor="#404040" width="100%" border="0" cellpadding="0" cellspacing="0">
			<tbody>
			<tr>
			<div>
			<table bgcolor="#404040" align="center" width="100%" border="0" cellpadding="0" cellspacing="0">
			<tbody>
			<tr>
			<td height="30" style="font-size:30px; line-height: 30px">&nbsp;</td>
			</tr>
			</tbody>
			<tbody>
			<tr>
			<td>
			<a href="https://www.casimages.com">
			<img src="https://nsa40.casimages.com/img/2019/07/26/190726085109986582.png" width="224" border="0" alt="Logo U.D.F.">
			</a>
			</td>
			<td>
				<table bgcolor="#404040" align="center" width="100%" border="0" cellpadding="0" cellspacing="0">
					<tbody>
					<tr>
						<td align="center" style="text-align: center; font-size: 30px; color: #FFF; line-height: 40px">
							Union des démonstrateurs francophones
						</td>
					</tr>
					</tbody>
					<tbody>
					<tr>
						<td align="center" style="text-align: center; font-size: 20px; color: #FFF; line-height: 40px">
							Rue du Tram , 5 &nbsp &nbsp 4300 Waremme
						</td>
					</tr>
					</tbody>
					<tbody>
					<tr>
						<td align="center" style="text-align: center; font-size: 15px; color: #FFF; line-height: 100px">
							Email : @gmail.com &nbsp &nbsp Tél : 
						</td>
					</tr>
					</tbody>
				</table>
			</td>
			</tr>
			</tbody>
			</table>
			</div>
			</tr>
			</tbody>
		</table>	
	</body>
</html>
Merci d'avance

Re: Mail envoyer un message html

Publié : ven. 26/juil./2019 10:30
par Zorro
essaye ce code ? (un code de Venom apparemment )

bien mettre le login dans les variables en debut de code ;)

ps : pour Free le login c'est ce qui se trouve avant "@" exemple : toto.titi@free.fr , le login c'est generalement "toto.titi"

Code : Tout sélectionner


;***********************************************
;Titre  :*vmail (mail+attachement)
;Auteur  : Venom
;Date  :26/07/2019
;Heure  :11:34:09
;Version Purebasic :  PureBasic 5.70 LTS (Windows - x86)
;Version de l'editeur :EPB PHOENIX V2.68
; Libairies necessaire : Aucune 
;***********************************************

;- Window Constants
login$="votre login d'email"
pass$="votre password"

Enumeration
	#Window_0
	#Window_1
EndEnumeration
;- Gadget Constants
Enumeration
	#Frame3D_0
	#Frame3D_1
	#Frame3D_2
	#Frame3D_3
	#Text_0
	#Text_1
	#Text_2
	#Text_3
	#String_0
	#String_1
	#String_2
	#String_3
	#Combo_0
	#Button_0
	#Button_1
	#StatusBar_0
EndEnumeration
InitNetwork()
If OpenWindow(#Window_0, 0, 0, 600, 375, "VMail",  #PB_Window_SystemMenu|#PB_Window_ScreenCentered)
	Frame3DGadget(#Frame3D_0, 10, 10, 580, 260, "")
	Frame3DGadget(#Frame3D_1, 10, 285, 175, 60, "votre fournisseur")
	Frame3DGadget(#Frame3D_2, 415, 285, 175, 60, "Envoyer")
	Frame3DGadget(#Frame3D_3, 210, 285, 175, 60, "Pièce jointe")
	TextGadget(#Text_0, 20, 35, 25, 20, "De:")
	TextGadget(#Text_1, 20, 65, 20, 20, "A:")
	TextGadget(#Text_2, 15, 95, 40, 20, "Objet:")
	StringGadget(#String_0, 60, 30, 520, 20, "")
	StringGadget(#String_1, 60, 60, 520, 20, "")
	StringGadget(#String_2, 60, 90, 520, 20, "")
	EditorGadget(#String_3, 20, 120, 560, 140)
	ComboBoxGadget(#Combo_0, 20, 305, 155, 30)
	AddGadgetItem(#Combo_0, 0, "alice" , 0)
	AddGadgetItem(#Combo_0, 1, "caramail" , 0)
	AddGadgetItem(#Combo_0, 2, "cegetel" , 0)
	AddGadgetItem(#Combo_0, 3, "club-internet" , 0)
	AddGadgetItem(#Combo_0, 4, "free" , 0)
	AddGadgetItem(#Combo_0, 5, "neuf" , 0)
	AddGadgetItem(#Combo_0, 6, "noos" , 0)
	AddGadgetItem(#Combo_0, 7, "orange" , 0)
	AddGadgetItem(#Combo_0, 8, "tele2" , 0)
	AddGadgetItem(#Combo_0, 9, "tiscali" , 0)
	AddGadgetItem(#Combo_0, 10, "wanadoo" , 0)
	SetGadgetState(#Combo_0, 4)
	ButtonGadget(#Button_0, 425, 305, 155, 30, "Envoyer")
	ButtonGadget(#Button_1, 220, 305, 155, 30, "Ajouté un fichier")
	If CreateStatusBar(#StatusBar_0, WindowID(#Window_0))
		AddStatusBarField(300)
		AddStatusBarField(300)
		StatusBarText(#StatusBar_0, 0, "Pas de pièce jointe", #PB_StatusBar_BorderLess)
		StatusBarText(#StatusBar_0, 1, "", #PB_StatusBar_BorderLess)
	EndIf
EndIf
Repeat
	EventID = WaitWindowEvent()
	If EventID = #PB_Event_Gadget
		Select EventGadget()
			Case #Button_1
			piecejointes$ = OpenFileRequester("Choisir un fichier a envoyer", "", "Fichiers|*.*" , 0)
			testpiecejointes = SelectedFilePattern()
			If testpiecejointes = 0
				DisableGadget(#Button_1, 1)
				StatusBarText(#StatusBar_0, 0, "pièce jointe : "+GetFilePart(piecejointes$), #PB_StatusBar_BorderLess)
				ElseIf testpiecejointes = -1
				DisableGadget(#Button_1, 0)
			EndIf
			Case #Button_0
			fournisseur$ = GetGadgetItemText(#Combo_0, GetGadgetState(#Combo_0), 0); on renvoie le fournisseur
			mailserver.s="smtp."+fournisseur$+".fr"
			If CreateMail(0, GetGadgetText(#String_0), GetGadgetText(#String_2))
				SetMailBody(0, GetGadgetText(#String_3))
				AddMailRecipient(0, GetGadgetText(#String_1), #PB_Mail_To)
				AddMailAttachment(0, GetFilePart(piecejointes$), piecejointes$ , MimeType$)
				StatusBarText(#StatusBar_0, 1, "Patientez envoie en cours ...", #PB_StatusBar_BorderLess|#PB_StatusBar_Right)
				Resultat = SendMail(0, mailserver.s, 25, 0,login$,pass$)
				If Resultat = 1
					StatusBarText(#StatusBar_0, 1, "", #PB_StatusBar_BorderLess|#PB_StatusBar_Right)
					MessageRequester("Information", "Mail envoyé avec succée", 0)
					End
					ElseIf Resultat = 0
					StatusBarText(#StatusBar_0, 1, "Erreur d'envoie ...", #PB_StatusBar_BorderLess|#PB_StatusBar_Right)
					MessageRequester("Information", "Le mail n'a pas pu étre envoyé", 0)
				EndIf
			EndIf
		EndSelect
	EndIf
Until EventID = #PB_Event_CloseWindow; EPB

; 
; EPB 


Re: Mail envoyer un message html

Publié : ven. 26/juil./2019 11:00
par Ar-S
@Zorro.
Tu as testé pour voir si le code de Venom envoi du HTML dans le corps du message ?

@microdevweb
Tu as une lib qui a l'air bien compète ICI.
Il y a les sources si ça peut t'aider. J'ai pas testé.

Re: Mail envoyer un message html

Publié : ven. 26/juil./2019 11:31
par Zorro
non ! :lol:

c'est pour ça que je commence mon message par "essaye ce code ? "

Re: Mail envoyer un message html

Publié : ven. 26/juil./2019 12:09
par microdevweb
@Zorro je n'ai pas encore testé

@Ar-s je viens de tester et ça plante.

Le je me tourne ver une solution en Java, l'envoie de mail fonctionne maintenant il faut que link cela avec Pb. C'est une valeur de retour qui me pose problème (pour savoir si l'email à bien été envoyé)

Re: Mail envoyer un message html

Publié : sam. 27/juil./2019 8:46
par microdevweb
@zorro,

En regardant en analysant le code, je constate qu'il utilise les fonction standard de Pb, don pour l'envoie de html c'est mort à mon avis.

Re: Mail envoyer un message html

Publié : sam. 27/juil./2019 9:13
par Zorro
ben , je sais pas, mais je pense qu'il ne faut pas se contenter de regarder le code .... il faut aussi le lancer
des fois .. ça peut le faire , sait on jamais ?

apres, faut voir avec Venom ... c'est son code :)

Re: Mail envoyer un message html

Publié : sam. 27/juil./2019 9:18
par Zorro
sinon test ça :

Code : Tout sélectionner


; www.purearea.net (Sourcecode collection by cnesm)
; Author: PB (updated for PB 4.00 by Andre)
; Date: 22. November 2003
; OS: Windows
; Demo: No

;Modified code originally posted by Paul IIRC  :)

;USAGE:
;PBSendMail(
;                        RecipientEmailAddress as String
;                        SenderEmailAddress as String
;                        MailServerHost as String
;                        Subject as String
;                        Message as String
;                        AttachmentIncluded as Byte (Flag: 0/1)
;                     )

;NOTES:
;When the 'AttachmentIncluded' flag is set to '1', the mail procedure loops through a linked list
;called 'Attachments()' then encodes or processes the attachments. So to send attachments
;you must have a linked list called 'Attachments()'.

;===============================================
;-GLOBAL FLAGS / VARIABLES / STRUCTURES / ARRAYS
;===============================================

Global ConnectionID.l
Global MailResponse.s

Declare PBSendMail(RecipientEmailAddress.s, SenderEmailAddress.s, MailServerHost.s, subject.s, message.s, AttachmentIncluded.b)
Declare.s MailResponse()
Declare SendMailData(msg.s)
Declare.s GetMIMEType(Extension.s)

;Example linked list
Global NewList Attachments.s()
InsertElement(Attachments())
;Attachments() = "C:\Documents And Settings\User\Desktop\Image.jpg"
;InsertElement(Attachments())
;Attachments() = "C:\Documents And Settings\User\Desktop\Archive.zip"
;InsertElement(Attachments())
;Attachments() = "C:\Documents And Settings\User\Desktop\ObscureText.fff"


;Testing:
PBSendMail("****.*****@free.fr", "****.****@free.fr", "smtp.free.fr", "hello test", "rien ne sert de courrir , il faut partir a point", 0)

MessageRequester("ok","envoyé",#PB_MessageRequester_Ok)
End



;===============================================
;-PROCEDURES
;===============================================

;Check to see if the file is binary
Procedure IsBinary(File.s)
		If ReadFile(0, File)
				While Loc(0) <> Lof(0)
						CurrentByte.b = ReadByte(0)
						If CurrentByte <= 9 Or CurrentByte = 127
						CloseFile(0)
						ProcedureReturn 1
				EndIf
				If CurrentByte > 10 And CurrentByte < 13
				CloseFile(0)
				ProcedureReturn 1
		EndIf
		If CurrentByte > 13 And CurrentByte < 32
		CloseFile(0)
		ProcedureReturn 1
EndIf
Wend
EndIf
EndProcedure

;Find the MIME type for a given file extension
Procedure.s GetMIMEType(Extension.s)
		Extension = "." + Extension
		hKey.l = 0
		KeyValue.s = Space(255)
		DataSize.l = 255
		If RegOpenKeyEx_(#HKEY_CLASSES_ROOT, Extension, 0, #KEY_READ, @hKey)
				KeyValue = "application/octet-stream"
		Else
				If RegQueryValueEx_(hKey, "Content Type", 0, 0, @KeyValue, @DataSize)
						KeyValue = "application/octet-stream"
				Else
						KeyValue = Left(KeyValue, DataSize-1)
				EndIf
				RegCloseKey_(hKey)
		EndIf
		ProcedureReturn KeyValue
EndProcedure

;Send a piece of mail data
Procedure SendMailData(msg.s)
		SendNetworkData(ConnectionID, @msg, Len(msg))
EndProcedure




;Check the server responses
Procedure.s MailResponse()
		MailResponse=Space(9999)
		ReceiveNetworkData(ConnectionID,@MailResponse,9999)
		MailResponse=Left(MailResponse,3)
		ProcedureReturn MailResponse
EndProcedure



;Send the mail
Procedure PBSendMail(RecipientEmailAddress.s, SenderEmailAddress.s, MailServerHost.s, subject.s, message.s, AttachmentIncluded.b)
		If InitNetwork()
				ConnectionID = OpenNetworkConnection(MailServerHost, 25)
				If ConnectionID <> 0
						MailResponse()
						If MailResponse = "220"
								Index = FindString(MailServerHost, ".", 1)
								MailServerDomain.s = Mid(MailServerHost, Index + 1, Len(MailServerHost))
								SendMailData("HELO "+MailServerDomain+Chr(13)+Chr(10))
								MailResponse()
								If MailResponse="250"
										Sleep_(125)
										SendMailData("MAIL FROM: <"+SenderEmailAddress+">"+Chr(13)+Chr(10))
										MailResponse()
										If MailResponse="250"
												SendMailData("RCPT TO: <"+RecipientEmailAddress+">"+Chr(13)+Chr(10))
												MailResponse()
												If MailResponse="250"
														SendMailData("DATA"+Chr(13)+Chr(10))
														MailResponse()
														If MailResponse="354"
																Sleep_(125)
																SendMailData("X-Mailer: PBSendMail v1.0" + Chr(13) + Chr(10))
																SendMailData("To: " + RecipientEmailAddress + Chr(13) + Chr(10))
																SendMailData("From: " + SenderEmailAddress + Chr(13) + Chr(10))
																SendMailData("Reply-To:" + SenderEmailAddress + Chr(13) + Chr(10))
																SendMailData("Date: " + FormatDate("%dd/%mm/%yyyy @ %hh:%ii:%ss", Date()) + Chr(13) + Chr(10))
																SendMailData("Subject: " + subject + Chr(13) + Chr(10))
																SendMailData("MIME-Version: 1.0" + Chr(13) + Chr(10))
																;Handle any attachments
																If AttachmentIncluded
																		Debug "Processing 'multipart/mixed' Email..."
																		Boundry.s = "PBSendMailv1.0_Boundry_"+ FormatDate("%dd%mm%yyyy%hh%ii%ss", Date())
																		SendMailData("Content-Type: multipart/mixed; boundary=" + Chr(34) + Boundry + Chr(13) + Chr(10) + Chr(34))
																		SendMailData(Chr(13) + Chr(10))
																		;Main message
																		Debug "Processing Messsage..."
																		SendMailData("--" + Boundry + Chr(13) + Chr(10)) ; Boundry
																		SendMailData("Content-Type: text/plain; charset=" + Chr(34) + "iso-8859-1" + Chr(34) + Chr(13) + Chr(10))
																		SendMailData("Content-Transfer-Encoding: 7bit" + Chr(13) + Chr(10))
																		SendMailData(Chr(13) + Chr(10))
																		Sleep_(125)
																		SendMailData(message + Chr(13) + Chr(10))
																		SendMailData(Chr(13) + Chr(10))
																		Sleep_(125)
																		Debug "Processing Attachments..."
																		ResetList(Attachments())
																		While(NextElement(Attachments()))
																		;Attachment headers
																		SendMailData("--" + Boundry + Chr(13) + Chr(10)) ; Boundry
																		SendMailData("Content-Type: " + GetMIMEType(GetExtensionPart(Attachments())) + "; name=" + Chr(34) + GetFilePart(Attachments()) + Chr(34) + Chr(13) + Chr(10))
																		If IsBinary(Attachments())
																				SendMailData("Content-Transfer-Encoding: base64" + Chr(13) + Chr(10))
																				SendMailData("Content-Disposition: Attachment; filename=" + Chr(34) + GetFilePart(Attachments()) + Chr(34) + Chr(13) + Chr(10))
																				SendMailData(Chr(13) + Chr(10))
																				Sleep_(125)
																				;Encode the Attachments using Base64
																				If ReadFile(0, Attachments())
																						InputBufferLength.l = Lof(0)
																						*memin = AllocateMemory(InputBufferLength)
																						If *mem
																								OutputBufferLength.l = InputBufferLength + InputBufferLength/3 + 2
																								If OutputBufferLength < 64 : OutputBufferLength = 64 : EndIf
																								*memout = AllocateMemory(OutputBufferLength)
																								If *memout
																										ReadData(0, *memin, InputBufferLength)
																										Base64Encoder(@memin, InputBufferLength, @memout, OutputBufferLength)
																										SendMailData(PeekS(*memout, OutputBufferLength) + Chr(13) + Chr(10))
																										Debug GetFilePart(Attachments()) + " (base64) Encoded"
																								Else
																										Debug "ERROR: Unable to allocate memory for Bank 1 to process " + GetFilePart(Attachments())
																										ProcedureReturn 0
																								EndIf
																						Else
																								Debug "ERROR: Unable to allocate memory for Bank 0 to process " + GetFilePart(Attachments())
																								ProcedureReturn 0
																						EndIf
																				Else
																						Debug "ERROR: Unable to read file: " + GetFilePart(Attachments())
																						ProcedureReturn 0
																				EndIf
																				CloseFile(0) : FreeMemory(*memin) : FreeMemory(*memout);[
																				Else
																						SendMailData("Content-Transfer-Encoding: 7bit" + Chr(13) + Chr(10))
																						SendMailData("Content-Disposition: Attachment; filename=" + Chr(34) + GetFilePart(Attachments()) + Chr(34) + Chr(13) + Chr(10))
																						SendMailData(Chr(13) + Chr(10))
																						Sleep_(125)
																						If ReadFile(0, Attachments())
																								InputBufferLength.l = Lof(0)
																								*memin = AllocateMemory(InputBufferLength)
																								If *memin
																										ReadData(0, *memin, InputBufferLength)
																										SendMailData(PeekS(*memin, InputBufferLength) + Chr(13) + Chr(10))
																										Debug GetFilePart(Attachments()) + " (7bit) Processed"
																								Else
																										Debug "ERROR: Unable to allocate memory for Bank 0 to process " + GetFilePart(Attachments())
																										ProcedureReturn 0
																								EndIf
																						Else
																								Debug "ERROR: Unable to read file: " + GetFilePart(Attachments())
																								ProcedureReturn 0
																						EndIf
																				EndIf
																				
																				Sleep_(125)
																				SendMailData(Chr(13) + Chr(10))
																		Wend
																		SendMailData("--" + Boundry + "--" + Chr(13) + Chr(10)) ; End Boundry
																Else
																		Debug "Processing messsage..."
																		SendMailData("Content-Type: text/plain; charset=" + Chr(34) + "iso-8859-1" + Chr(34) + Chr(13) + Chr(10))
																		SendMailData("Content-Transfer-Encoding: 7bit" + Chr(13) + Chr(10))
																		SendMailData(Chr(13) + Chr(10))
																		Sleep_(125)
																		SendMailData(message + Chr(13) + Chr(10))
																EndIf
																Sleep_(125)
																SendMailData(Chr(13)+Chr(10))
																SendMailData("."+Chr(13)+Chr(10))
																MailResponse()
																If MailResponse="250"
																		Sleep_(125)
																		SendMailData("QUIT"+Chr(13)+Chr(10))
																		MailResponse()
																		Debug "Mail sent successfully."
																		ProcedureReturn 1
																EndIf
														EndIf
												EndIf
										EndIf
								EndIf
						EndIf
						CloseNetworkConnection(ConnectionID)
				EndIf
		EndIf
EndProcedure
; EPB


Re: Mail envoyer un message html

Publié : sam. 27/juil./2019 10:54
par microdevweb
@Zorro,

Il ne fonctionne pas.

Re: Mail envoyer un message html

Publié : sam. 27/juil./2019 11:34
par venom
Bonjour,

En effet ce code est de moi. Il fonctionne très bien, mais il se contente d'envoyer un texte brut via EditorGadget. Pas de code HTML.

@microdevweb
Tu souhaite envoyer un mail qui a du "graphisme" genre des titres plus ou moins gros, des images etc ?






@++

Re: Mail envoyer un message html

Publié : sam. 27/juil./2019 11:59
par Ar-S
venom a écrit :@microdevweb
Tu souhaite envoyer un mail qui a du "graphisme" genre des titres plus ou moins gros, des images etc ?
Il veut du HTML dans le corps du mail, pas du texte brut. ça permet de modifier l'apparence mais aussi d'autres choses.. Mettre du html quoi :D

Re: Mail envoyer un message html

Publié : sam. 27/juil./2019 14:15
par microdevweb
Je suis en train de travailler sur une version Java (qui fonctionne bien), maintenant je dois linker avec pb. Si j'y arrive je partagerais cela.

Re: Mail envoyer un message html

Publié : sam. 27/juil./2019 20:21
par Ar-S
D'avance merci. :mrgreen:

Re: Mail envoyer un message html

Publié : sam. 27/juil./2019 21:02
par djes
Il semble qu'il manque un petit truc pour pouvoir changer le format d'un email ce qui est incompatible avec certains clients email. Ça devrait être corrigé un de ces 4.
En attendant il y a cette nouvelle lib : PB.Ex Mail https://www.purebasic.fr/english/viewto ... 27&t=72729
D'autre part certains utilisent chilkat https://www.purebasic.fr/english/viewto ... =5&t=30595