Comment envoyer des mails en php

Résolu/Fermé
mobrobot Messages postés 173 Date d'inscription vendredi 13 janvier 2017 Statut Membre Dernière intervention 8 mai 2023 - Modifié le 13 déc. 2017 à 17:09
 Mobrobot - 14 déc. 2017 à 21:25
Bonsoir à tous le monde s'il vous plait je n'arrive pas à envoyé un mail dans ma boit de réception Gmail. si quelqu'un pourrait m'aider ça serait un grand plaisir pour moi.
j'ai deux code que j'avais écrit pour envoyé un mail. mais malheureusement aucun d'eu ne fonction

je travaille avec wampserver localhost.

voici mon code que j'avais écrit il y'a 3jours
<?php
if(isset($_POST['mailform'])){

$header="MIME-Version: 1.0\r\n";
$header.='From:"Datamobme"<monadresse@gmail.com>'."\n";
$header.='Content-Type:text/html; charset="uft-8"'."\n";
$header.='Content-Transfer-Encoding: 8bit';

$message='
<html>
 <body>
  <div align="center">
   <br />
   J\'ai envoyé ce mail avec PHP !
   <br />
  </div>
 </body>
</html>
';

mail("monautreadresse@gmail.com", "Salut tout le monde !", $message, $header);
 }
?>
<form method="POST" action="">
 <input type="submit" value="Recevoir un mail !" name="mailform"/>
</form>


<?php


et aujourd’hui suivez le tutoriel de openclassrooms Envoyer un e-mail en php

j'ai suivie le cour a la lettre le problème est que le message ne par pas dans ma boit mail

voici le code du tutoriel :
<?php
$mail = 'monadresse22@gmail.com'; // Déclaration de l'adresse de destination.
if (!preg_match("#^[a-z0-9._-]+@(hotmail|live|msn).[a-z]{2,4}$#", $mail)) // On filtre les serveurs qui présentent des bogues.
{
 $passage_ligne = "\r\n";
}
else
{
 $passage_ligne = "\n";
}
//=====Déclaration des messages au format texte et au format HTML.
$message_txt = "Salut à tous, voici un e-mail envoyé par un script PHP.";
$message_html = "<html><head></head><body><b>Salut à tous</b>, voici un e-mail envoyé par un <i>script PHP</i>.</body></html>";
//==========
 
//=====Lecture et mise en forme de la pièce jointe.
$fichier   = fopen("swordigo.png", "r");
$attachement = fread($fichier, filesize("swordigo.png"));
$attachement = chunk_split(base64_encode($attachement));
fclose($fichier);
//==========
 
//=====Création de la boundary.
$boundary = "-----=".md5(rand());
$boundary_alt = "-----=".md5(rand());
//==========
 
//=====Définition du sujet.
$sujet = "Hey mon ami !";
//=========
 
//=====Création du header de l'e-mail.
$header = "From: \"Mobrobot\"<monadresse22@gmail.com>".$passage_ligne;
$header.= "Reply-to: \"Mobrobot\" <monadresse22@gmail.com>".$passage_ligne;
$header.= "MIME-Version: 1.0".$passage_ligne;
$header.= "Content-Type: multipart/mixed;".$passage_ligne." boundary=\"$boundary\"".$passage_ligne;
//==========
 
//=====Création du message.
$message = $passage_ligne."--".$boundary.$passage_ligne;
$message.= "Content-Type: multipart/alternative;".$passage_ligne." boundary=\"$boundary_alt\"".$passage_ligne;
$message.= $passage_ligne."--".$boundary_alt.$passage_ligne;
//=====Ajout du message au format texte.
$message.= "Content-Type: text/plain; charset=\"ISO-8859-1\"".$passage_ligne;
$message.= "Content-Transfer-Encoding: 8bit".$passage_ligne;
$message.= $passage_ligne.$message_txt.$passage_ligne;
//==========
 
$message.= $passage_ligne."--".$boundary_alt.$passage_ligne;
 
//=====Ajout du message au format HTML.
$message.= "Content-Type: text/html; charset=\"ISO-8859-1\"".$passage_ligne;
$message.= "Content-Transfer-Encoding: 8bit".$passage_ligne;
$message.= $passage_ligne.$message_html.$passage_ligne;
//==========
 
//=====On ferme la boundary alternative.
$message.= $passage_ligne."--".$boundary_alt."--".$passage_ligne;
//==========
 
 
 
$message.= $passage_ligne."--".$boundary.$passage_ligne;
 
//=====Ajout de la pièce jointe.
$message.= "Content-Type: image/jpeg; name=\"swordigo.png\"".$passage_ligne;
$message.= "Content-Transfer-Encoding: base64".$passage_ligne;
$message.= "Content-Disposition: attachment; filename=\"swordigo.png\"".$passage_ligne;
$message.= $passage_ligne.$attachement.$passage_ligne.$passage_ligne;
$message.= $passage_ligne."--".$boundary."--".$passage_ligne; 
//========== 
//=====Envoi de l'e-mail.
mail($mail,$sujet,$message,$header);
 
//==========

?>


voici la configuration de sendmail.ini:

[sendmail]
smtp_server*smtp.gmail.com
smtp_port-587
default_domain=gmail.com
error_logfile=error.log
auth_username=monadresse@gmail.com
auth_password=monpassword
pop3_server=
pop3_username=
pop3_password=
force_sender=monadresse@gmail.com
force_recipient=
hostname=


je vous remercie

1 réponse

jordane45 Messages postés 38138 Date d'inscription mercredi 22 octobre 2003 Statut Modérateur Dernière intervention 17 avril 2024 4 649
13 déc. 2017 à 20:29
Bonjour,

je pense déjà voir une erreur au niveau du fichier de config de sendmail.
Il devrait ressembler au minimum à ça :

[sendmail]
smtp_server=smtp.gmail.com
smtp_port=587
default_domain=gmail.com
error_logfile=error.log
auth_username=********@gmail.com
auth_password=******
pop3_server=
pop3_username=
pop3_password=
force_sender=****@gmail.com
force_recipient=
hostname=


ou mieux :

; configuration for fake sendmail

; if this file doesn't exist, sendmail.exe will look for the settings in
; the registry, under HKLM\Software\Sendmail

[sendmail]

; you must change mail.mydomain.com to your smtp server,
; or to IIS's "pickup" directory. (generally C:\Inetpub\mailroot\Pickup)
; emails delivered via IIS's pickup directory cause sendmail to
; run quicker, but you won't get error messages back to the calling
; application.

smtp_server=smtp.gmail.com

; smtp port (normally 25)

smtp_port=465

; SMTPS (SSL) support
; auto = use SSL for port 465, otherwise try to use TLS
; ssl = alway use SSL
; tls = always use TLS
; none = never try to use SSL

smtp_ssl=auto

; the default domain for this server will be read from the registry
; this will be appended to email addresses when one isn't provided
; if you want to override the value in the registry, uncomment and modify

default_domain=smtp.gmail.com

; log smtp errors to error.log (defaults to same directory as sendmail.exe)
; uncomment to enable logging

error_logfile=error.log

; create debug log as debug.log (defaults to same directory as sendmail.exe)
; uncomment to enable debugging

;debug_logfile=debug.log

; if your smtp server requires authentication, modify the following two lines

auth_username=xxxxx@gmail.com
auth_password=xxxxxxx


; force the sender to always be the following email address
; this will only affect the "MAIL FROM" command, it won't modify
; the "From: " header of the message content

force_sender=xxxxx@gmail.com

; force the sender to always be the following email address
; this will only affect the "RCTP TO" command, it won't modify
; the "To: " header of the message content

force_recipient=

; sendmail will use your hostname and your default_domain in the ehlo/helo
; smtp greeting. you can manually set the ehlo/helo name if required

hostname=


Pour le port, faut tester soit le smtp_port=465 soit le smtp_port=587

Il faut également que tu édites ton fichier PHP.INI

[mail function]
; For Win32 only.
; https://www.php.net/manual/en/mail.configuration.php#ini.smtp
;SMTP =
; https://www.php.net/manual/en/mail.configuration.php#ini.smtp-port
smtp_port = 465

; For Win32 only.
; https://www.php.net/manual/en/mail.configuration.php#ini.sendmail-from
;sendmail_from =

; For Unix only. You may supply arguments as well (default: "sendmail -t -i").
; https://www.php.net/manual/en/mail.configuration.php#ini.sendmail-path
sendmail_path = "C:\Wamp\sendmail\sendmail.exe"


Je t'invite à lire ce tuto
https://dfarnier.fr/serveur-mail-wampserver/

Ou celui la
https://www.grafikart.fr/blog/mail-local-wamp




NB : Bien entendu, après toute modification du fichier INI ..... il faut redémarrer les service APACHE

0
Merci beaucoup jordane45 sa fonction well well
0