Quelle signature pour cette fonction de mail ?

WRInaute accro
Bonjour

Avec PHP Pear Mail et PHP Pear Mail_mime, je me suis fait une petite classe PHP dont j'hésite pour la signature.

Les mails sont censés être convertis en UTF-8 ( subject, texte et html compris ), et sont capables d'acheminer des fichiers.

Voici la signature du constructeur, qui me paraît trop compliquée :

PHP:
<?php

class sendMail extends Sanitize
{
function __construct(string $param_to, string $param_from, bool $is_html, string $host, array $username_pass, string $param_subject, string $param_text = "", string $param_body = "", array $param_attachments = null)
{
  //
}

}
?>

Avec la syntaxe :

PHP:
<?php

 $USER = 0;
 $PASS = 1;

 $username_pass[$USER] et $username[$PASS]

  Et puis, pour $param_attachments :

$attachment = array();
$attachment["file"]             = $file_pathname;
$attachment["contentType"]      = $type;
$attachment["name"]             = $filename;
$attachment["isFile"]           = bool;
$attachment["encoding"]         = $encoding;

$param_attachments = array($attachment);

  /**** Il peut y avoir plusieurs  fichiers attachés. **/

?>

Qu'en pensez vous ?

Quelle signature serait plus simple ?

Merci beaucoup de votre aide.

Respectueusement.
 
Dernière édition:
WRInaute accro
Bonsoir Spout

Ah oui, des getters et des setters...

Et si je veux tout faire en même temps ?

Je vais étudier la chose...

Merci Spout. ;)

Respectueusement.
 
WRInaute accro
Bonjour spout ;)

J'ai pratiquement terminé le soft équivalent ( moins de fonctionnalités ) à SwifMailer.

L'exemple utilise la même email to et from, mais çà marche quand même vers n'importe qui.

Le serveur est : pronostics-courses.fr , en mode connecté login / password et en ssl ( port 587 ).

Voilà, j'arrive à envoyer avec ou sans fichier attaché ( un seul fichier pour l'instant ), mais je n'arrive pas à l'afficher inline dans Thunderbird ( seulement comme attachment ).

Voici la source du mail, d'après Thunderbird.

La disposition est bien inline, mais le fichier est reçu ( correctement d'ailleurs ), en pièce jointe.

Le source est foireux.

Merci beaucoup de votre aide.

Amicalement.


Code:
Return-Path: <webmaster@pronostics-courses.fr>
X-Original-To: webmaster@pronostics-courses.fr
Delivered-To: webmaster@pronostics-courses.fr
Received: from localhost (unknown [78.194.232.180])
    (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits))
    (No client certificate requested)
    by mail.pronostics-courses.fr (Postfix) with ESMTPSA id 75A195DE31
    for <webmaster@pronostics-courses.fr>; Fri, 19 Oct 2018 11:39:30 +0200 (CEST)
MIME-Version: 1.0
Content-Type: multipart/mixed;
 boundary="=_042f1d040b2b0bf329a035042dde83c8"
To: Webmaster du site pronostics-courses_fr
 <webmaster@pronostics-courses.fr>
From: Webmaster du site pronostics-courses_fr
 <webmaster@pronostics-courses.fr>
Content-Transfer-Encoding: 8bit
--=_042f1d040b2b0bf329a035042dde83c8
Content-Type: multipart/alternative;
 boundary="=_c0162f2c94c961f4631437cb0a2fda5f"
--=_c0162f2c94c961f4631437cb0a2fda5f
Content-Transfer-Encoding: 8bit
Content-Type: text/plain; charset=utf-8
Essai réussi ?
--=_c0162f2c94c961f4631437cb0a2fda5f
Content-Transfer-Encoding: 8bit
Content-Type: text/html; charset=utf-8
<!DOCTYPE html>
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Titre Essai</title>
    </head>
    <body>
    <p>Hello World !</p>
    </body>
    </html>
--=_c0162f2c94c961f4631437cb0a2fda5f--
--=_042f1d040b2b0bf329a035042dde83c8
Content-Transfer-Encoding: base64
Content-Type: application/php; charset=utf-8;
 name=PHP_PEAR_MAIL.php
Content-Disposition: inline;
 filename=PHP_PEAR_MAIL.php;
 size=30756
//  <--- Contenu en Base64 ---->
--=_042f1d040b2b0bf329a035042dde83c8--
 
WRInaute accro
Pardon

Maintenant, tout marche ( inline ou attachment ) , mais quand je veux envoyer à deux emails, le soft envoie à une des emails seulement.

Voici le source du mail, avec une petite portion du fichier attaché :

Le fichier attaché, est le programme php d'envoi de mail.

J'aurais simplement besoin, d'une correction du source.

Merci beaucoup de votre aide.

Respectueusement.

Code:
Return-Path: <webmaster@pronostics-courses.fr>
X-Original-To: postmaster@pronostics-courses.fr
Delivered-To: postmaster@pronostics-courses.fr
Received: from localhost (unknown [78.194.232.180])
   (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits))
   (No client certificate requested)
   by mail.pronostics-courses.fr (Postfix) with ESMTPSA id 5D16A5DD5E
   for <postmaster@pronostics-courses.fr>; Tue, 23 Oct 2018 20:19:25 +0200 (CEST)
MIME-Version: 1.0
Content-Type: multipart/mixed;
 boundary="=_3a66c851fcc55af1eb8bd4ce00dacdfe"
To: Postmaster du site pronostics-courses_fr
 <postmaster@pronostics-courses.fr>, Abuse du site pronostics-courses_fr
 <abuse@pronostics-courses.fr>
From: Webmaster du site pronostics-courses_fr
 <webmaster@pronostics-courses.fr>
X-Mailer: PHP/7.2.11

--=_3a66c851fcc55af1eb8bd4ce00dacdfe
Content-Type: multipart/alternative;
 boundary="=_516d800168dd5682dc0e097e7c8b5012"

--=_516d800168dd5682dc0e097e7c8b5012
Content-Transfer-Encoding: 8bit
Content-Type: text/plain; charset=utf-8

Essai réussi ?
--=_516d800168dd5682dc0e097e7c8b5012
Content-Transfer-Encoding: 8bit
Content-Type: text/html; charset=utf-8

<!DOCTYPE html>
   <html>
   <head>
   <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
   <meta name="viewport" content="width=device-width, initial-scale=1.0">
   <title>Titre Essai</title>
   </head>
   <body>
   <p>Hello World !</p>
   </body>
   </html>
--=_516d800168dd5682dc0e097e7c8b5012--

--=_3a66c851fcc55af1eb8bd4ce00dacdfe
Content-Transfer-Encoding: quoted-printable
Content-Type: text/plain; charset=utf8;
 name=PHP_PEAR_MAIL.php
Content-Disposition: inline;
 filename=PHP_PEAR_MAIL.php;
 size=35120

<?php

ini_set('display_errors', true);

//    error_reporting(0);
//    error_reporting(E_ALL|E_STRICT);
//    error_reporting(E_ALL);
error_reporting(E_ALL|E_STRICT|E_DEPRECATED);

// Require Pear Mail Packages
require_once ("Mail.php");
require_once ("Mail/mime.php");

require_once ("constantes.php");

class MyMailTransportData
{
   protected $USERNAME    =3D null;
   protected $PASSWORD    =3D null;

   private $HOSTNAME    =3D null;
   private $PORT        =3D 587;
   private $PROTOCOL    =3D 'smtp';

   public function setHost(string $host)
   {
       $this->HOSTNAME =3D $host;
   }

   public function setPort(integer $port)
   {
       $this->PORT =3D $port;
   }

   public function setProtocol(string $proto)
   {
       $this->PROTOCOL =3D $proto;
   }

   public function setUser(string $user)
   {
       $this->USERNAME =3D $user;
   }

   public function setPass(string $pass)
   {
       $this->PASSWORD =3D $pass;
   }

   public function getHost()
   {
       return $this->HOSTNAME;
   }

   public function getPort()
   {
       return $this->PORT;
   }

   public function getProtocol()
   {
       return $this->PROTOCOL;
   }

   public function getUser()
   {
       return $this->USERNAME;
   }

   public function getPass()
   {
       return $this->PASSWORD;
   }

   public function start()
   {
       /*
        * ? What
        * to implement
        * for testing
        * SMTP ?
        */
   }

   function __construct(string $host, $port =3D null, string  $protocol =3D '=
smtp')
   {
       /**
        * Verification
        * du $host ?
        **/
       $this->setHost($host);

       if($port !=3D=3D null)
       {
           $this->setPort($port);
       }

       $this->setProtocol($protocol);

       return $this;
   }
}

class MyMailTransport
{
   private $backendParams =3D array();

   private $recip =3D null;

   private $sender =3D null;

   private $MAIL =3D null;

   private function affic($type, $param)
   {
       echo "\t**********************************\n";

       echo "\t$type : \n";

       echo "\t**********************************\n\n";

       if(!is_array($param))
       {
           echo "\tEn-Tete : " . $param;

           echo "\n\n";

           echo "\t**********************************\n\n\n";

           return;
       }

       foreach($param as $key1 =3D> $headers)
       {
           if(is_array($headers))
           {
               foreach($headers as $key2 =3D> $header)
               {
                   if(is_array($header))
                   {
                       foreach($header as $key3 =3D> $value)
                       {
                           if(is_array($value))
                           {
                               foreach($value as $key4 =3D> $value2)
                               {
                                   echo  "\tEn-Tete[ " . $key1  . ' ][ ' . $key2  . ' ][ ' . $key3  =
=2E ' ][ ' . $key4  . ' ] =3D ' .  $value2;

                                   echo "\n\n";
                               }
                           }
                           else
                           {
                               echo  "\tEn-Tete[ " . $key1  . ' ][ ' . $key2  . ' ][ ' . $key3  =
=2E ' ] =3D ' .  $value;

                               echo "\n\n";

                           }
                       }
                   }
                   else
                   {
                       echo  "\tEn-Tete[ " . $key1  . ' ][ ' . $key2  . ' ] =3D ' .  $header=
;

                       echo "\n\n";
                   }
               }
           }
           else
           {
               echo  "\tEn-Tete[ " . $key1  . ' ] =3D ' .  $headers;

               echo "\n\n";
           }
       }

       echo "\t**********************************\n\n\n";

       return;
   }

   function send($message)
   {
       $to =3D $message->getTo();
       $from =3D $message->getFrom();


       if(is_array($to))
       {
           $liste_to =3D implode(', ', $to);

           $this->recip =3D $to[0];
       }
       else
       {
           $liste_to =3D $to;

           $this->recip =3D $to;
       }

       echo "\t\t" . '$liste_to =3D ' . $liste_to;

       echo "\n\n";

       if(is_array($from))
       {
           $liste_from =3D implode(', ', $from);

           $array_email =3D $message->validate_email($from[0]);

           $this->sender =3D $array_email['email_name'];

           $message->setSender($this->sender);
       }
       else
       {
           $liste_from =3D $from;
       }

       echo "\t\t" . '$liste_from =3D ' . $liste_from;

       echo "\n\n";

       $subject =3D $message->getSubject();

       $return_path =3D $message->getReturnPath();

       /*
        * Param=C3=A8tres
        * du Mime.
        */
       $build_params =3D array();

       /*
        * Param=C3=A8tres
        * du Body.
        */
       $body_params =3D array();

       /*
        * En-tete
        * Mailer.
        */
       $message->setHeader('X-Mailer', 'PHP/' . PHP_VERSION_MON_ID);

       /*
        * Array lue
        * des headers.
        */
       $tmp_array =3D array();
       $tmp_array =3D $message->getHeaders();

       $bad_headers =3D array('Content-Type', 'Charset', 'Content-Transfer-Encod=
ing');

       foreach($tmp_array as $key1 =3D> $value1)
       {
           if(is_array($value1))
           {
               foreach($value1 as $key2 =3D> $value2)
               {
                   if(!in_array($key2, $bad_headers))
                   {
                       $build_params[$key2] =3D $value2;
                   }
               }
           }
           else
           {
               $build_params[$key1] =3D $value1;
           }
       }

       $this->affic('Build_params', $build_params);

       /*
        * Mime.
        */
       $crlf =3D "\n";

       $mime =3D new Mail_mime(array('eol' =3D> $crlf));


       $num_max_part =3D $message->getPartNum();

       /**
        * Avec html
        * et texte.
        **/
       if($num_max_part > 0)
       {
           $message->setPartNum(0);

           $body =3D $message->getBody();
           $mime->setHTMLBody($body);=20

           $body_params['html_charset'] =3D $message->getCharset();
           $body_params['html_encoding'] =3D $message->getEncoder();
           $body_params['head_charset'] =3D $message->getCharset();

           $message->setPartNum($num_max_part);

           $part =3D    $message->getPart();
           $mime->setTXTBody($part);=20

           $body_params['text_charset'] =3D $message->getCharset();
           $body_params['text_encoding'] =3D $message->getEncoder();
       }
       /**
        * Avec texte
        * seulement.
        **/
       else
       {
           $message->setPartNum(0);

           $body =3D $message->getBody();
           $mime->setTXTBody($body);=20

           $body_params['text_charset'] =3D $message->getCharset();
           $body_params['text_encoding'] =3D $message->getEncoder();
       }

       $body_params['delay_file_io'] =3D false;

       $this->affic('Body_params', $body_params);

       $num_max =3D $message->getAttachNum();

       $array_attachments =3D array();
       $array_attachments =3D $message->getAttachments();

       for($i =3D 0; $i <=3D $num_max; $i++)
       {
           unset($attachments);
           $attachments =3D array();

           $attachments =3D $array_attachments[$i];

           $mime->addAttachment(
               $attachments->getFilePath(),
               $attachments->getContentType(),
               $attachments->getFilename(),=20
               $attachments->getIsFile(),
               $attachments->getEncoder(),
               $attachments->getDisposition(),
               $attachments->getCharset(),
               $attachments->getLanguage(),
               $attachments->getLocation(),
               $attachments->getNencoding(),
               $attachments->getFencoding(),
               $attachments->getDescription(),
               $attachments->getHcharset()
           );
       }

       /*
        * Elements
        * du message.
        */
       $mailBody =3D $mime->get($body_params);
       $mailHeaders =3D $mime->headers($build_params);

       $this->recip =3D $mime->encodeRecipients($this->recip);

       $this->affic("mailHeaders", (array)$mailHeaders);

       $this->affic('mailBody', (array)$mailBody);

       $res =3D $this->MAIL->send($this->recip, $mailHeaders, $mailBody);

       if (PEAR::isError($res))
       {
           echo '<p>' . $res->getMessage() . '</p>' . "\n\n";

           return false;
       }
       else
       {
           //            echo '<p>Message successfully sent!</p>' . "\n\n" ;

           return true;

           return $number_of_sent_emails;
       }
   }

   function __construct(MyMailTransportData $transport)
   {
       $backend =3D $transport->getProtocol();

       $user =3D $transport->getUser();
       $pass =3D $transport->getPass();

       /*
        * Donn=C3=A9es
        * SMTP.
        */
       $this->backendParams['host'] =3D $transport->getHost();
       $this->backendParams['port'] =3D $transport->getPort();
       $this->backendParams['auth'] =3D 'CRAM-MD5';

       if($user !=3D=3D null)
       {
           $this->backendParams['username'] =3D $user;
       }

       if($pass !=3D=3D null)
       {
           $this->backendParams['password'] =3D $pass;
       }

       $this->backendParams['debug'] =3D true;

       // Create the mail object using the Mail::factory method=20
       $this->MAIL =3D Mail::factory($backend, $this->backendParams);

       if (PEAR::isError($this->MAIL))
       {
           echo '<p>' . $this->MAIL->getMessage() . '</p>' . "\n\n";

           return false;
       }
       else
       {
           //            echo '<p>SMTP Transport successfully created! </p>' . "\n\n" ;

           return $this;
       }
   }
}

class MyMailAttachment
 
WRInaute accro
Bon

Cà marche, ( le premier paramètre de Mail::send() n'avait qu'une email ), maintenant j'aurais besoin que mon soft soit testé sous toutes les coutures, je ne sais pas comment faire pour laisser ce programme PHP ( 35 kilo-octets ) à disposition pour tests.

Quand j'aurai corrigé les erreurs résiduelles, qu'est-ce que je fais ?

L'interface PHP c'est du SwiftMail plus facile.

4 classes :

class MyMailTransportData
class MyMailTransport
class MyMailAttachment
class MyMailMessage

Merci beaucoup pour vos réponses.

Amicalement.
 
Discussions similaires
Haut