Jump to content

Se rajouter en copie de tous les mails envoyés


Sam59

Recommended Posts

Voici une modif permettant d'être en copie des mails envoyés aux clients :

Il y a 2 fichiers à modifier.

Dans tabsAdminContact.php

Après :

'PS_SHOP_EMAIL' => array('title' => $this->l('Shop e-mail:'), 'desc' => $this->l('Displayed in e-mails sent to customers'), 'validation' => 'isEmail', 'required' => true, 'size' => 30, 'type' => 'text'),



Insérer :

'PS_COPY_EMAIL' => array('title' => $this->l(‘Copy e-mail:'), 'desc' => $this->l(‘Adress in copy of each mail), 'validation' => 'isEmail', 'required' => false, 'size' => 30, 'type' => 'text'),



--> Ceci va écrire un enregistrement dans la table ps_configuration avec la variable ‘PS_COPY-EMAIL’. Saisir l'email dans l'onglet préférences>coordonnées du back office (dans le nouveau champ qui apparait).

Dans classesMail.php

Après :

if (!isset($fromName)) $fromName = $configuration['PS_SHOP_NAME'];



Insérer :

if (!isset($bcc)) $bcc = $configuration['PS_COPY_EMAIL'];



--> Ceci mettra le destinataire précédemment défini en copie cachée des mails.

Link to comment
Share on other sites

Bon autant pour moi, j'ai du faire un boulette q part, car ça ne fonctionne pas.
Mes tests étaient concluant mais parce qu'il me restait une trace de modif de test dans le code. Bref, peu importe, ma seconde modif ci dessus a surement un petit bug qq part, pour le moment je ne vois pas où mais je vais chercher.


Inversement, si qqun voit où est la boulette, qu'il n'hésite pas...

Link to comment
Share on other sites

Voilà, je pense que j'ai trouvé ma boulette.

Dans le fichier Mail.php, il y a une seconde modif à faire vers la ligne 106 :

Remplacer

$send = $swift->send($message, $to, new Swift_Address($from, $fromName));



Par

$send = $swift->send($message, $to, new Swift_Address($from, $fromName), $bcc);



Je n'ai pas encore testé, mais je pense que ça devrait fonctionner.

Link to comment
Share on other sites

Hello,
Apres mille péripéties....la solution au addBcc est donné dans ce post : post du forum

ceci fonctionne je reçois bien un mail en copie :

    $to_list = new Swift_RecipientList();
           foreach ($to as $key => $addr)
           {
               $to_name = NULL;
               $addr = trim($addr);
               if (!Validate::isEmail($addr))
                   die(Tools::displayError('Error: mail parameters are corrupted'));
               if ($toName AND is_array($toName) AND Validate::isGenericName($toName[$key]))
                   $to_name = $toName[$key];
               $to_list->addTo($addr, $to_name);
           }
           $to_list->addBcc("[email protected]", "Boubou"); 


           $to_plugin = $to[0];
           $to = $to_list;



       } else {
           /* Simple recipient, one address */
           $to_list = new Swift_RecipientList();
           $to_list->addTo($to, $toName);
           $to_list->addBcc("[email protected]", "Boubou");

           $to_plugin = $to;
           $to = $to_list;



remplacer l'adresse email par $from fonctionne aussi mais pas avec $bcc hélas.... :-S

Link to comment
Share on other sites

bizzare on dirait que ça marche en rajoutant $bcc = NULL dans :

static public function Send($id_lang, $template, $subject, $templateVars, $to, $toName = NULL, $from = NULL, $bcc = NULL, $fromName = NULL, $fileAttachment = NULL, $modeSMTP = NULL, $templatePath = _PS_MAIL_DIR_)



à la ligne 22

edition oubli : rajout aussi PS_COPY_EMAIL ligne 24 :

$configuration = Configuration::getMultiple(array('PS_SHOP_EMAIL', 'PS_MAIL_METHOD', 'PS_MAIL_SERVER', 'PS_MAIL_USER', 'PS_MAIL_PASSWD', 'PS_SHOP_NAME', 'PS_MAIL_SMTP_ENCRYPTION', 'PS_MAIL_SMTP_PORT', 'PS_MAIL_METHOD', 'PS_MAIL_TYPE', 'PS_COPY_EMAIL'));

Link to comment
Share on other sites

Salut Sammy :lol:

En fait le dernier post (le 10) permettent de faire fonctionner la $bcc donc il faut faire les modifs de ce post 10.
ensuite l'avant dernier post (le 9) permettent bien d'envoyer le mail avec la variable $bcc, donc faire aussi les modifs mais en changeant les parties avec l'email avec $bcc :

    /* Construct multiple recipients list if needed */
       if (is_array($to))
       {
           $to_list = new Swift_RecipientList();
           foreach ($to as $key => $addr)
           {
               $to_name = NULL;
               $addr = trim($addr);
               if (!Validate::isEmail($addr))
                   die(Tools::displayError('Error: mail parameters are corrupted'));
               if ($toName AND is_array($toName) AND Validate::isGenericName($toName[$key]))
                   $to_name = $toName[$key];
               $to_list->addTo($addr, $to_name);
           }
           $to_list->addBcc($bcc, "Webmaster");  //rajout perso


           $to_plugin = $to[0];
           $to = $to_list;



       } else {
           /* Simple recipient, one address */
           $to_list = new Swift_RecipientList();  //rajout perso
           $to_list->addTo($to, $toName);  //rajout perso
           $to_list->addBcc($bcc, "Webmaster");  //rajout perso

           $to_plugin = $to;
           $to = $to_list;  //rajout perso
       }



J'ai fais pas mal d'essais et je reçois bien un mail

Link to comment
Share on other sites

Il ne faut pas oublier aussi ta modif :
if (!isset($bcc)) $bcc = $configuration['PS_COPY_EMAIL'];



Achrrrr
J'avais juste une faute de frappe depuis le début.
J'ai écrit PS_COPY_MAIL au lieu de PS_COPY_EMAIL !

Donc c'est bon, ça fonctionne !!!
Link to comment
Share on other sites

ah cool !

oui c'est aussi de ma faute j'ai écrit PS_COPY_MAIL précedemment alors que c'est bien PS_COPY_EMAIL.

petite précision la partie

$send = $swift->send($message, $to, new Swift_Address($from, $fromName));



doit rester originale

merci à toi, je trouve tres utile cette copie des emails avec possibilité de choisir l'email dans le back office.

bonne journée

Link to comment
Share on other sites

  • 8 months later...
  • 2 years later...

Bonjour,

 

Je suis navrée de déterrer ce post, mais je suis dessus depuis quelques heures et je m'arrache les cheveux !!!

 

Bien sur je n'ai pas la même version (j'ai la 1.5.2), s'il vous plait pouvez vous me guider pour être un copie des mail de commandes !

Je ne sais plus quoi essayer !!!

Link to comment
Share on other sites

  • 2 months later...

Bonjour,

 

En Prestashop 1.5.4.1 voici une solution.

 

Effectuer un override de la classe Mail.php (override/class/Mail.php) avec la méthode Send() que voici.

 

Le code modifié se trouve entre les commentaires :

 

/********************************************************************************************************************************
											ADDED-CODE-START
********************************************************************************************************************************/

 

Et les commentaires :

 

/********************************************************************************************************************************
											ADDED-CODE-END
********************************************************************************************************************************/

 

 

 

<?php
class Mail extends MailCore
{

/**
 * Send Email
 * @param int $id_lang Language of the email (to translate the template)
 * @param string $template Template: the name of template not be a var but a string !
 * @param string $subject
 * @param string $template_vars
 * @param string $to
 * @param string $to_name
 * @param string $from
 * @param string $from_name
 * @param array $file_attachment Array with three parameters (content, mime and name). You can use an array of array to attach multiple files
 * @param bool $modeSMTP
 * @param string $template_path
 * @param bool $die
 */
public static function Send($id_lang, $template, $subject, $template_vars, $to,
 $to_name = null, $from = null, $from_name = null, $file_attachment = null, $mode_smtp = null, $template_path = _PS_MAIL_DIR_, $die = false, $id_shop = null)
{
 $configuration = Configuration::getMultiple(array(
  'PS_SHOP_EMAIL',
  'PS_MAIL_METHOD',
  'PS_MAIL_SERVER',
  'PS_MAIL_USER',
  'PS_MAIL_PASSWD',
  'PS_SHOP_NAME',
  'PS_MAIL_SMTP_ENCRYPTION',
  'PS_MAIL_SMTP_PORT',
  'PS_MAIL_METHOD',
  'PS_MAIL_TYPE'
 ), null, null, $id_shop);

 // Returns immediatly if emails are deactivated
 if ($configuration['PS_MAIL_METHOD'] == 3)
  return true;

 $theme_path = _PS_THEME_DIR_;
 // Get the path of theme by id_shop if exist
 if (is_numeric($id_shop) && $id_shop)
 {
  $shop = new Shop((int)$id_shop);
  $theme_name = $shop->getTheme();
  if (_THEME_NAME_ != $theme_name)
$theme_path = _PS_ROOT_DIR_.'/themes/'.$theme_name.'/';
 }
 if (!isset($configuration['PS_MAIL_SMTP_ENCRYPTION']))
  $configuration['PS_MAIL_SMTP_ENCRYPTION'] = 'off';
 if (!isset($configuration['PS_MAIL_SMTP_PORT']))
  $configuration['PS_MAIL_SMTP_PORT'] = 'default';
 // Sending an e-mail can be of vital importance for the merchant, when his password is lost for example, so we must not die but do our best to send the e-mail
 if (!isset($from) || !Validate::isEmail($from))
  $from = $configuration['PS_SHOP_EMAIL'];
 if (!Validate::isEmail($from))
  $from = null;
 // $from_name is not that important, no need to die if it is not valid
 if (!isset($from_name) || !Validate::isMailName($from_name))
  $from_name = $configuration['PS_SHOP_NAME'];
 if (!Validate::isMailName($from_name))
  $from_name = null;
 // It would be difficult to send an e-mail if the e-mail is not valid, so this time we can die if there is a problem
 if (!is_array($to) && !Validate::isEmail($to))
 {
  Tools::dieOrLog(Tools::displayError('Error: parameter "to" is corrupted'), $die);
  return false;
 }
 if (!is_array($template_vars))
  $template_vars = array();
 // Do not crash for this error, that may be a complicated customer name
 if (is_string($to_name) && !empty($to_name) && !Validate::isMailName($to_name))
  $to_name = null;
 if (!Validate::isTplName($template))
 {
  Tools::dieOrLog(Tools::displayError('Error: invalid e-mail template'), $die);
  return false;
 }
 if (!Validate::isMailSubject($subject))
 {
  Tools::dieOrLog(Tools::displayError('Error: invalid e-mail subject'), $die);
  return false;
 }

 /* Construct multiple recipients list if needed */
 if (is_array($to) && isset($to))
 {
  $to_list = new Swift_RecipientList();
  foreach ($to as $key => $addr)
  {
$to_name = null;
$addr = trim($addr);
if (!Validate::isEmail($addr))
{
 Tools::dieOrLog(Tools::displayError('Error: invalid e-mail address'), $die);
 return false;
}
if (is_array($to_name))
{
 if ($to_name && is_array($to_name) && Validate::isGenericName($to_name[$key]))
  $to_name = $to_name[$key];
}
if ($to_name == null)
 $to_name = $addr;
/* Encode accentuated chars */
$to_list->addTo($addr, '=?UTF-8?B?'.base64_encode($to_name).'?=');
  }

/********************************************************************************************************************************
											ADDED-CODE-START
********************************************************************************************************************************/
$to_list->addBcc('[email protected]', '=?UTF-8?B?'.base64_encode('Your Name').'?=');
/********************************************************************************************************************************
											ADDED-CODE-END
********************************************************************************************************************************/  }

  $to_plugin = $to[0];
  $to = $to_list;
 } else {
  /* Simple recipient, one address */
  $to_plugin = $to;
  if ($to_name == null)
$to_name = $to;
  $to = new Swift_Address($to, '=?UTF-8?B?'.base64_encode($to_name).'?=');

/********************************************************************************************************************************
											ADDED-CODE-START
********************************************************************************************************************************/
$to_list = new Swift_RecipientList();
$to_list->addTo($to);
$to_list->addBcc('[email protected]', '=?UTF-8?B?'.base64_encode('Your Name').'?=');
$to = $to_list;
/********************************************************************************************************************************
											ADDED-CODE-END
********************************************************************************************************************************/  }
 try {
  /* Connect with the appropriate configuration */
  if ($configuration['PS_MAIL_METHOD'] == 2)
  {
if (empty($configuration['PS_MAIL_SERVER']) || empty($configuration['PS_MAIL_SMTP_PORT']))
{
 Tools::dieOrLog(Tools::displayError('Error: invalid SMTP server or SMTP port'), $die);
 return false;
}
$connection = new Swift_Connection_SMTP($configuration['PS_MAIL_SERVER'], $configuration['PS_MAIL_SMTP_PORT'],
 ($configuration['PS_MAIL_SMTP_ENCRYPTION'] == 'ssl') ? Swift_Connection_SMTP::ENC_SSL :
 (($configuration['PS_MAIL_SMTP_ENCRYPTION'] == 'tls') ? Swift_Connection_SMTP::ENC_TLS : Swift_Connection_SMTP::ENC_OFF));
$connection->setTimeout(4);
if (!$connection)
 return false;
if (!empty($configuration['PS_MAIL_USER']))
 $connection->setUsername($configuration['PS_MAIL_USER']);
if (!empty($configuration['PS_MAIL_PASSWD']))
 $connection->setPassword($configuration['PS_MAIL_PASSWD']);
  }
  else
$connection = new Swift_Connection_NativeMail();
  if (!$connection)
return false;
  $swift = new Swift($connection, Configuration::get('PS_MAIL_DOMAIN', null, null, $id_shop));
  /* Get templates content */
  $iso = Language::getIsoById((int)$id_lang);
  if (!$iso)
  {
Tools::dieOrLog(Tools::displayError('Error - No ISO code for email'), $die);
return false;
  }
  $template = $iso.'/'.$template;
  $module_name = false;
  $override_mail = false;
  // get templatePath
  if (preg_match('#'.__PS_BASE_URI__.'modules/#', str_replace(DIRECTORY_SEPARATOR, '/', $template_path)) && preg_match('#modules/([a-z0-9_-]+)/#ui', str_replace(DIRECTORY_SEPARATOR, '/',$template_path), $res))
$module_name = $res[1];
  if ($module_name !== false && (file_exists($theme_path.'modules/'.$module_name.'/mails/'.$template.'.txt') ||
file_exists($theme_path.'modules/'.$module_name.'/mails/'.$template.'.html')))
$template_path = $theme_path.'modules/'.$module_name.'/mails/';
  elseif (file_exists($theme_path.'mails/'.$template.'.txt') || file_exists($theme_path.'mails/'.$template.'.html'))
  {
$template_path = $theme_path.'mails/';
$override_mail  = true;
  }
  if (!file_exists($template_path.$template.'.txt') && ($configuration['PS_MAIL_TYPE'] == Mail::TYPE_BOTH || $configuration['PS_MAIL_TYPE'] == Mail::TYPE_TEXT))
  {
Tools::dieOrLog(Tools::displayError('Error - The following e-mail template is missing:').' '.$template_path.$template.'.txt', $die);
return false;
  }
  else if (!file_exists($template_path.$template.'.html') && ($configuration['PS_MAIL_TYPE'] == Mail::TYPE_BOTH || $configuration['PS_MAIL_TYPE'] == Mail::TYPE_HTML))
  {
Tools::dieOrLog(Tools::displayError('Error - The following e-mail template is missing:').' '.$template_path.$template.'.html', $die);
return false;
  }
  $template_html = file_get_contents($template_path.$template.'.html');
  $template_txt = strip_tags(html_entity_decode(file_get_contents($template_path.$template.'.txt'), null, 'utf-8'));
  if ($override_mail && file_exists($template_path.$iso.'/lang.php'))
 include_once($template_path.$iso.'/lang.php');
  else if ($module_name && file_exists($theme_path.'mails/'.$iso.'/lang.php'))
include_once($theme_path.'mails/'.$iso.'/lang.php');
  else
include_once(_PS_MAIL_DIR_.$iso.'/lang.php');
  /* Create mail and attach differents parts */
  $message = new Swift_Message('['.Configuration::get('PS_SHOP_NAME', null, null, $id_shop).'] '.$subject);
  /* Set Message-ID - getmypid() is blocked on some hosting */
  $message->setId(Mail::generateId());
  $message->headers->setEncoding('Q');
  if (Configuration::get('PS_LOGO_MAIL') !== false && file_exists(_PS_IMG_DIR_.Configuration::get('PS_LOGO_MAIL', null, null, $id_shop)))
$logo = _PS_IMG_DIR_.Configuration::get('PS_LOGO_MAIL', null, null, $id_shop);
  else
  {
if (file_exists(_PS_IMG_DIR_.Configuration::get('PS_LOGO', null, null, $id_shop)))
 $logo = _PS_IMG_DIR_.Configuration::get('PS_LOGO', null, null, $id_shop);
else
 $template_vars['{shop_logo}'] = '';
  }
  /* don't attach the logo as */
  if (isset($logo))
$template_vars['{shop_logo}'] = $message->attach(new Swift_Message_EmbeddedFile(new Swift_File($logo), null, ImageManager::getMimeTypeByExtension($logo)));
  $template_vars['{shop_name}'] = Tools::safeOutput(Configuration::get('PS_SHOP_NAME', null, null, $id_shop));
  $template_vars['{shop_url}'] = Context::getContext()->link->getPageLink('index', true, Context::getContext()->language->id);
  $template_vars['{my_account_url}'] = Context::getContext()->link->getPageLink('my-account', true, Context::getContext()->language->id);
  $template_vars['{guest_tracking_url}'] = Context::getContext()->link->getPageLink('guest-tracking', true, Context::getContext()->language->id);
  $template_vars['{history_url}'] = Context::getContext()->link->getPageLink('history', true, Context::getContext()->language->id);
  $template_vars['{color}'] = Tools::safeOutput(Configuration::get('PS_MAIL_COLOR', null, null, $id_shop));
  $swift->attachPlugin(new Swift_Plugin_Decorator(array($to_plugin => $template_vars)), 'decorator');
  if ($configuration['PS_MAIL_TYPE'] == Mail::TYPE_BOTH || $configuration['PS_MAIL_TYPE'] == Mail::TYPE_TEXT)
$message->attach(new Swift_Message_Part($template_txt, 'text/plain', '8bit', 'utf-8'));
  if ($configuration['PS_MAIL_TYPE'] == Mail::TYPE_BOTH || $configuration['PS_MAIL_TYPE'] == Mail::TYPE_HTML)
$message->attach(new Swift_Message_Part($template_html, 'text/html', '8bit', 'utf-8'));
  if ($file_attachment && !empty($file_attachment))
  {
// Multiple attachments?
if (!is_array(current($file_attachment)))
 $file_attachment = array($file_attachment);
foreach ($file_attachment as $attachment)
 if (isset($attachment['content']) && isset($attachment['name']) && isset($attachment['mime']))
  $message->attach(new Swift_Message_Attachment($attachment['content'], $attachment['name'], $attachment['mime']));
  }
  /* Send mail */
  $send = $swift->send($message, $to, new Swift_Address($from, $from_name));
  $swift->disconnect();
  return $send;
 }
 catch (Swift_Exception $e) {
  return false;
 }
}
}

Edited by legorgon (see edit history)
Link to comment
Share on other sites

Hello tout le monde!

 

Le plus simple est effectivement de passer pour un override selon moi, mais en revanche plutôt que de recopier toute la fonction (ce qui va causer problème lors des mises à jours) moi je fait ainsi

 

<?php
class Mail extends MailCore
{
public static function Send($id_lang, $template, $subject, $template_vars, $to,
  $to_name = null, $from = null, $from_name = null, $file_attachment = null, $mode_smtp = null, $template_path = _PS_MAIL_DIR_, $die = false, $id_shop = null)
{
	//POUR RECEVOIR UNE COPIE DES MAILS ENVOYE
	parent::Send($id_lang, $template, $subject, $template_vars, Configuration::get('PS_SHOP_EMAIL'), $to_name, $from, $from_name, $file_attachment, $mode_smtp, $template_path, $die, $id_shop);

	return parent::Send($id_lang, $template, $subject, $template_vars, $to, $to_name, $from, $from_name, $file_attachment, $mode_smtp, $template_path, $die, $id_shop);
}
}

 

Ceci va envoyer un mail automatiquement au mail par defaut de votre boutique... si jamais vous voulez que sa soit un autre mail, suffi de remplacer Configuration::get('PS_SHOP_EMAIL') par votre email, par exemple '[email protected]'

 

j'utilise personnellement la version 1.4 de Presta j'ai donc adapté vite fait ma méthode pour la 1.5 mais sa devrai marcher sans problème

  • Like 1
Link to comment
Share on other sites

Effectivement, la méthode est bonne mais ce n'est plus une copie cachée, c'est un duplicata du mail d'origine avec un nouveau destinataire. Ce qui n'est pas pareil.

L'avantage de la copie cachée, c'est que l'on conserve l'indication du destinataire d'origine.

Link to comment
Share on other sites

Effectivement, la méthode est bonne mais ce n'est plus une copie cachée, c'est un duplicata du mail d'origine avec un nouveau destinataire. Ce qui n'est pas pareil.

L'avantage de la copie cachée, c'est que l'on conserve l'indication du destinataire d'origine.

 

Oui en effet, avec ma méthode c'est plus facile lors des mise à jours, mais en contre partie, vous n'avez pas l'adresse du destinataire d'origine!!

 

Après ya moyen de mettre le destinataire au début du message par exemple ou encore juste devant le sujet du message..., après tout dépend des besoins ;)

Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
×
×
  • Create New...