IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Bibliothèques et frameworks PHP Discussion :

PHPMailer: Les messages HTML sont tronqués


Sujet :

Bibliothèques et frameworks PHP

  1. #1
    Membre éclairé
    Homme Profil pro
    Ingénieur en électrotechnique retraité
    Inscrit en
    Décembre 2008
    Messages
    1 577
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 72
    Localisation : France, Bas Rhin (Alsace)

    Informations professionnelles :
    Activité : Ingénieur en électrotechnique retraité

    Informations forums :
    Inscription : Décembre 2008
    Messages : 1 577
    Points : 803
    Points
    803
    Par défaut PHPMailer: Les messages HTML sont tronqués
    Bonjour à tous, merci aux dévoués contributeurs.

    Lors de l'envoi de mails le code <p>Un exemple de texte avec <strong><une partie en gras</strong>.</p> est réceptionné comme ceci: <p>Un exemple de texte avec .</p>
    J'ai créé une classe fille de PHPMailer nommée UsePHPMailer. Je donne ici les codes de la classe fille et du code d'utilisation.

    Classe fille dans classes/moimp/UsePHPMailer.php:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    <?php
     
    namespace UsePHPMailer;
     
    require_once('vendor/phpmailer/phpmailer/src/PHPMailer.php');
    require_once("vendor/phpmailer/phpmailer/src/SMTP.php");
    require_once("vendor/phpmailer/phpmailer/src/Exception.php");
    require_once("classes/moimp/StringTools.php");
     
    use \PHPMailer\PHPMailer\PHPMailer;
    use \PHPMailer\PHPMailer\SMTP;
    use \Exception;
     
    class UsePHPMailer extends PHPMailer
    {
     
    	protected $fromMail;
     
    	protected $fromName;
     
    	protected $languageDir;
     
    	protected $language2;
     
    	public function __construct(array $paramsBase)
    	{
    		extract($paramsBase);	// VOIR LE CAS OU IL MANQUE DES VARIABLES
     
    		if ( ! in_array($CharSet, ['utf-8', 'ASCII']) )
    			throw new Exception( sprintf("Params offset 'CharSet' in method '%s' must be 'utf-8' or 'ASCII'", __METHOD__) );
     
    		if (strlen($language) !== 2)
    			throw new Exception( sprintf("Params offset 'language' in method '%s' must be a valid language code with 2-characters (see RFC 639-1).", __METHOD__) );
     
    		if ( ! is_dir($languageDir) )
    			throw new Exception( sprintf("Params offset 'languageDir' in method '%s' is no valid path. Path must be language path of PHPMailer.", __METHOD__) );
     
    		parent::__construct(true);
    		$this->CharSet		= $CharSet;
    		$this->language2	= $language;
    		$this->setLanguage($language, $languageDir);
    	}
     
    	public function setSMTP(array $paramsSMTP)
    	{
    		extract($paramsSMTP);	// VOIR LE CAS OU IL MANQUE DES VARIABLES
     
    		if ( ! in_array($SMTPDebug, [SMTP::DEBUG_OFF, SMTP::DEBUG_CLIENT, SMTP::DEBUG_SERVER]) )
    			throw new Exception( sprintf("Params offset 'SMTPDebug' must be one of values SMTP::DEBUG_OFF, SMTP::DEBUG_CLIENT or SMTP::DEBUG_SERVER in method '%s'.", __METHOD__) );
     
    		if ( ! is_string($Host) )
    			throw new Exception( sprintf("Params offset 'Host' must be a string in method '%s.", __METHOD__) );
     
    		if ( ! is_string($Username) )
    			throw new Exception( sprintf("Params offset 'Username' must be a string (oft based on email address of sender) in method '%s.", __METHOD__) );
     
    		if ( ! is_string($Password) )
    			throw new Exception( sprintf("Params offset 'password' must be a string in method '%s'.", __METHOD__) );
     
    		if ( ! is_bool($SMTPAuth) )
    			throw new Exception( sprintf("Params offset 'SMTPAuth' must be a boolean in method '%s'.", __METHOD__) );
     
    		if ( ! is_int($Port) )
    			throw new Exception( sprintf("Params offset 'port' must be an integer in method '%s'.", __METHOD__) );
     
    		$this->isSMTP();
     
    		$this->SMTPDebug= $SMTPDebug;
    		$this->Host		= $Host;
    		$this->Port		= $Port;
    		$this->SMTPAuth	= $SMTPAuth;
    		$this->Username	= $Username;
    		$this->Password	= $Password;
    	}
     
    	public function setContent(array $content)
    	{
    		extract($content);	// VOIR LE CAS OU IL MANQUE DES VARIABLES
     
    		if ( ! \StringTools::isMail($fromMail) )
    			throw new Exception(sprintf("Params offset 'fromMail' is not a valid mail in method '%s'.", __METHOD__) );
     
    		if ( ! is_string($fromMail) )
    			throw new Exception(sprintf("Params offset 'fromMail' must be a string in method '%s'.", __METHOD__) );
     
    		if ( ! is_string($fromName) )
    			throw new Exception(sprintf("Params offset 'fromName' must be a string in method '%s'.", __METHOD__) );
     
    		if ( ! is_string($Subject) )
    			throw new Exception(sprintf("Params offset 'Subject' must be a string in method '%s'.", __METHOD__) );
     
    		if ( ! is_string($Body) )
    			throw new Exception(sprintf("Params offset 'Body' must be a string in method '%s'.", __METHOD__) );
     
    		if ( ! is_string($AltBody) )
    			throw new Exception(sprintf("Params offset 'AltBody' must be a string in method '%s'.", __METHOD__) );
     
    $html = <<<HTML
    <!DOCTYPE html>
    <html lang='%s'>
    <head>
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
      <title>%s</title>
    </head>
    <body>
    	<div style="width: 640px; font-family: Arial, Helvetica, sans-serif; font-size: 12px;">
    		%s
    	</div>
    </body>
    </html>
    HTML;
    		$Body = sprintf($html, $this->language2, $Subject, $Body);
    		$this->fromMail	= $fromMail;
    		$this->fromName	= $fromName;
    		$this->Subject	= $Subject;
    		$this->Body		= $Body;
    		$this->AltBody	= $AltBody;
    	}
     
    	public function execute():string
    	{
    		$this->setFrom($this->fromMail, $this->fromName);
    		var_dump($this);
     
    		if( ! parent::send() )
    		{
    			return "Email was not sent.";
    		}
     
    		else
    		{
    			return "Email was sent.";
    		}
     
    	}
     
    }
     
    class_alias('UsePHPMailer\UsePHPMailer', 'UsePHPMailer', false);
    et le code d'utilisation dans _essais.php:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    require_once("classes/moimp/UsePHPMailer.php");
    require_once("vendor/phpmailer/phpmailer/src/SMTP.php");
    use PHPMailer\PHPMailer\SMTP;
     
    $paramsBase = [
    	'CharSet'		=>'utf-8',
    	'language'		=>'fr',
    	'languageDir'	=>'vendor/phpmailer/phpmailer/language/',
    	'isHTML'		=>true,
    	];
     
    $paramsSMTP = [
    	'SMTPDebug'	=>SMTP::DEBUG_SERVER,
    	'Host'		=>'smtp.orange.fr',
    	'Username'	=>'exemple@orange.fr',
    	'Password'	=>'password',
    	'SMTPAuth'	=>true,
    	'Port'		=>587,
    	'SMTPSecure'=>'TLS',
    	];
     
    $content = [
    	'fromMail'	=>exemple@orange.fr',
    	'fromName'	=>'Moimp',
    	'Subject'	=>'Essai',
    	'Body'		=>"<p>Un exemple de texte avec <strong><une partie en gras</strong>.</p>",
    	'AltBody'	=>"Exemple de texte brut",
    	];
     
    $test = new UsePHPMailer($paramsBase);
    $test->setSMTP($paramsSMTP);
    $test->addAddress('destinataire@free.fr', 'Prénom Nom');
    $test->setContent($content);
    echo $test->execute();
    EDIT: J'ai oublié de mettre le rapport de deboggage:
    2023-07-11 13:46:59 SERVER -> CLIENT: 220 opmta1mto24nd1 smtp.orange.fr ESMTP server ready
    2023-07-11 13:46:59 CLIENT -> SERVER: EHLO mp-tools
    2023-07-11 13:46:59 SERVER -> CLIENT: 250-opmta1mto24nd1 hello [88.170.16.41], pleased to meet you250-HELP250-AUTH LOGIN PLAIN250-SIZE 46000000250-ENHANCEDSTATUSCODES250-PIPELINING250-8BITMIME250-STARTTLS250 OK
    2023-07-11 13:46:59 CLIENT -> SERVER: STARTTLS
    2023-07-11 13:46:59 SERVER -> CLIENT: 220 2.0.0 Ready to start TLS
    2023-07-11 13:46:59 CLIENT -> SERVER: EHLO mp-tools
    2023-07-11 13:46:59 SERVER -> CLIENT: 250-opmta1mto24nd1 hello [88.170.16.41], pleased to meet you250-HELP250-AUTH LOGIN PLAIN250-SIZE 46000000250-ENHANCEDSTATUSCODES250-PIPELINING250-8BITMIME250-STARTTLS250 OK
    2023-07-11 13:46:59 CLIENT -> SERVER: AUTH LOGIN
    2023-07-11 13:46:59 SERVER -> CLIENT: 334 VXNlcm5hbWU6
    2023-07-11 13:46:59 CLIENT -> SERVER: [credentials hidden]
    2023-07-11 13:46:59 SERVER -> CLIENT: 334 UGFzc3dvcmQ6
    2023-07-11 13:46:59 CLIENT -> SERVER: [credentials hidden]
    2023-07-11 13:46:59 SERVER -> CLIENT: 235 2.7.0 ... authentication succeeded
    2023-07-11 13:46:59 CLIENT -> SERVER: MAIL FROM:<emetteur@orange.fr>
    2023-07-11 13:46:59 SERVER -> CLIENT: 250 2.1.0 <emetteur@orange.fr> sender ok
    2023-07-11 13:46:59 CLIENT -> SERVER: RCPT TO:<destinataire@free.fr>
    2023-07-11 13:47:00 SERVER -> CLIENT: 250 2.1.5 <destinataire@free.fr> recipient ok
    2023-07-11 13:47:00 CLIENT -> SERVER: DATA
    2023-07-11 13:47:00 SERVER -> CLIENT: 354 OK
    2023-07-11 13:47:00 CLIENT -> SERVER: Date: Tue, 11 Jul 2023 13:46:59 +0000
    2023-07-11 13:47:00 CLIENT -> SERVER: To: Moi <destinataire@free.fr>
    2023-07-11 13:47:00 CLIENT -> SERVER: From: Moimp <emetteu@orange.fr>
    2023-07-11 13:47:00 CLIENT -> SERVER: Subject: Essai
    2023-07-11 13:47:00 CLIENT -> SERVER: Message-ID: <rpP3JoHGIS0VYzIhCJMLoba7ZCghy2eEmukZ3vypSUs@mp-tools>
    2023-07-11 13:47:00 CLIENT -> SERVER: X-Mailer: PHPMailer 6.8.0 (https://github.com/PHPMailer/PHPMailer)
    2023-07-11 13:47:00 CLIENT -> SERVER: MIME-Version: 1.0
    2023-07-11 13:47:00 CLIENT -> SERVER: Content-Type: multipart/alternative;
    2023-07-11 13:47:00 CLIENT -> SERVER: boundary="b1=_rpP3JoHGIS0VYzIhCJMLoba7ZCghy2eEmukZ3vypSUs"
    2023-07-11 13:47:00 CLIENT -> SERVER: Content-Transfer-Encoding: 8bit
    2023-07-11 13:47:00 CLIENT -> SERVER:
    2023-07-11 13:47:00 CLIENT -> SERVER: --b1=_rpP3JoHGIS0VYzIhCJMLoba7ZCghy2eEmukZ3vypSUs
    2023-07-11 13:47:00 CLIENT -> SERVER: Content-Type: text/plain; charset=us-ascii
    2023-07-11 13:47:00 CLIENT -> SERVER:
    2023-07-11 13:47:00 CLIENT -> SERVER: Exemple de texte brut
    2023-07-11 13:47:00 CLIENT -> SERVER:
    2023-07-11 13:47:00 CLIENT -> SERVER: --b1=_rpP3JoHGIS0VYzIhCJMLoba7ZCghy2eEmukZ3vypSUs
    2023-07-11 13:47:00 CLIENT -> SERVER: Content-Type: text/html; charset=us-ascii
    2023-07-11 13:47:00 CLIENT -> SERVER:
    2023-07-11 13:47:00 CLIENT -> SERVER: <!DOCTYPE html>
    2023-07-11 13:47:00 CLIENT -> SERVER: <html lang='fr'>
    2023-07-11 13:47:00 CLIENT -> SERVER: <head>
    2023-07-11 13:47:00 CLIENT -> SERVER: <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    2023-07-11 13:47:00 CLIENT -> SERVER: <title>Essai</title>
    2023-07-11 13:47:00 CLIENT -> SERVER: </head>
    2023-07-11 13:47:00 CLIENT -> SERVER: <body>
    2023-07-11 13:47:00 CLIENT -> SERVER: <div style="width: 640px; font-family: Arial, Helvetica, sans-serif; font-size: 12px;">
    2023-07-11 13:47:00 CLIENT -> SERVER: <p>Un exemple de texte avec <strong><une partie en gras</strong>.</p>
    2023-07-11 13:47:00 CLIENT -> SERVER: </div>
    2023-07-11 13:47:00 CLIENT -> SERVER: </body>
    2023-07-11 13:47:00 CLIENT -> SERVER: </html>
    2023-07-11 13:47:00 CLIENT -> SERVER:
    2023-07-11 13:47:00 CLIENT -> SERVER:
    2023-07-11 13:47:00 CLIENT -> SERVER: --b1=_rpP3JoHGIS0VYzIhCJMLoba7ZCghy2eEmukZ3vypSUs--
    2023-07-11 13:47:00 CLIENT -> SERVER:
    2023-07-11 13:47:00 CLIENT -> SERVER: .
    2023-07-11 13:47:00 SERVER -> CLIENT: 250 2.0.0 JDhkqHCqJP7mwJDhkqz79p mail accepted for delivery
    2023-07-11 13:47:00 CLIENT -> SERVER: QUIT
    2023-07-11 13:47:00 SERVER -> CLIENT: 221 2.0.0 opmta1mto24nd1 smtp.orange.fr closing connection

  2. #2
    Membre éclairé
    Homme Profil pro
    Ingénieur en électrotechnique retraité
    Inscrit en
    Décembre 2008
    Messages
    1 577
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 72
    Localisation : France, Bas Rhin (Alsace)

    Informations professionnelles :
    Activité : Ingénieur en électrotechnique retraité

    Informations forums :
    Inscription : Décembre 2008
    Messages : 1 577
    Points : 803
    Points
    803
    Par défaut
    En poussant plus loin mes investigations, le problème ne vient pas de PHPMailer mais de mon client de messagerie. En effet en affichant le code source du message reçu, je constate que le code html est intégral mais que le rendu du message est tronqué.

    EDIT: Le problème vient ni de PHPMailer ni du client de messagerie mais de moi. Il y a un chevron de trop à la ligne 26 de _essais.php.

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. PhpMailer : les mails ne sont plus recus
    Par enrico83600 dans le forum Langage
    Réponses: 7
    Dernier message: 22/04/2010, 14h21
  2. Réponses: 7
    Dernier message: 15/11/2007, 09h31
  3. Où sont stockées les messages dans QMAIL ?
    Par stanley dans le forum Administration système
    Réponses: 0
    Dernier message: 17/08/2007, 13h51
  4. Réponses: 1
    Dernier message: 04/12/2005, 18h02

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo