Prepare code to use different SMTP servers.

This commit is contained in:
Laurent Destailleur 2017-04-18 01:39:06 +02:00
parent 0994622bf8
commit 3da0aea168
3 changed files with 351 additions and 338 deletions

View File

@ -27,7 +27,7 @@
*/ */
/** /**
* \file htdocs/admin/dict.php * \file htdocs/admin/mails_templates.php
* \ingroup setup * \ingroup setup
* \brief Page to administer data tables * \brief Page to administer data tables
*/ */
@ -119,12 +119,14 @@ $formmail=new FormMail($db);
if (empty($conf->global->MAIN_EMAIL_TEMPLATES_FOR_OBJECT_LINES)) if (empty($conf->global->MAIN_EMAIL_TEMPLATES_FOR_OBJECT_LINES))
{ {
$tmp=$formmail->getAvailableSubstitKey('form'); $tmp=$formmail->getAvailableSubstitKey('form');
$tmp['__(AnyTransKey)__']='__(AnyTransKey)__';
$helpsubstit = $langs->trans("AvailableVariables").':<br>'.implode('<br>', $tmp); $helpsubstit = $langs->trans("AvailableVariables").':<br>'.implode('<br>', $tmp);
$helpsubstitforlines = $langs->trans("AvailableVariables").':<br>'.implode('<br>', $tmp); $helpsubstitforlines = $langs->trans("AvailableVariables").':<br>'.implode('<br>', $tmp);
} }
else else
{ {
$tmp=$formmail->getAvailableSubstitKey('formwithlines'); $tmp=$formmail->getAvailableSubstitKey('formwithlines');
$tmp['__(AnyTransKey)__']='__(AnyTransKey)__';
$helpsubstit = $langs->trans("AvailableVariables").':<br>'.implode('<br>', $tmp); $helpsubstit = $langs->trans("AvailableVariables").':<br>'.implode('<br>', $tmp);
$tmp=$formmail->getAvailableSubstitKey('formforlines'); $tmp=$formmail->getAvailableSubstitKey('formforlines');
$helpsubstitforlines = $langs->trans("AvailableVariables").':<br>'.implode('<br>', $tmp); $helpsubstitforlines = $langs->trans("AvailableVariables").':<br>'.implode('<br>', $tmp);

View File

@ -787,8 +787,8 @@ else
if ($action == 'sendall') if ($action == 'sendall')
{ {
// Define message to recommand from command line // Define message to recommand from command line
$sendingmode=$conf->global->EMAILING_MAIL_SENDMODE;
$sendingmode=$conf->global->MAIN_MAIL_SENDMODE; if (empty($sendingmode)) $sendingmode=$conf->global->MAIN_MAIL_SENDMODE;
if (empty($sendingmode)) $sendingmode='mail'; // If not defined, we use php mail function if (empty($sendingmode)) $sendingmode='mail'; // If not defined, we use php mail function
// MAILING_NO_USING_PHPMAIL may be defined or not. // MAILING_NO_USING_PHPMAIL may be defined or not.

View File

@ -31,11 +31,15 @@
/** /**
* Class to send emails (with attachments or not) * Class to send emails (with attachments or not)
* Usage: $mailfile = new CMailFile($subject,$sendto,$replyto,$message,$filepath,$mimetype,$filename,$cc,$ccc,$deliveryreceipt,$msgishtml,$errors_to,$css,$trackid); * Usage: $mailfile = new CMailFile($subject,$sendto,$replyto,$message,$filepath,$mimetype,$filename,$cc,$ccc,$deliveryreceipt,$msgishtml,$errors_to,$css,$trackid,$moreinheader,$sendcontext);
* $mailfile->sendfile(); * $mailfile->sendfile();
*/ */
class CMailFile class CMailFile
{ {
public $sendcontext;
public $sendmode;
public $sendsetup;
var $subject; // Topic: Subject of email var $subject; // Topic: Subject of email
var $addr_from; // From: Label and EMail of sender (must include '<>'). For example '<myemail@example.com>' or 'John Doe <myemail@example.com>' or '<myemail+trackingid@example.com>'). Note that with gmail smtps, value here is forced by google to account (but not the reply-to). var $addr_from; // From: Label and EMail of sender (must include '<>'). For example '<myemail@example.com>' or 'John Doe <myemail@example.com>' or '<myemail+trackingid@example.com>'). Note that with gmail smtps, value here is forced by google to account (but not the reply-to).
// Sender: Who send the email ("Sender" has sent emails on behalf of "From"). // Sender: Who send the email ("Sender" has sent emails on behalf of "From").
@ -104,13 +108,21 @@ class CMailFile
* @param string $css Css option * @param string $css Css option
* @param string $trackid Tracking string (contains type and id of related element) * @param string $trackid Tracking string (contains type and id of related element)
* @param string $moreinheader More in header. $moreinheader must contains the "\r\n" (TODO not supported for other MAIL_SEND_MODE different than 'phpmail' and 'smtps' for the moment) * @param string $moreinheader More in header. $moreinheader must contains the "\r\n" (TODO not supported for other MAIL_SEND_MODE different than 'phpmail' and 'smtps' for the moment)
* @param string $sendingcontext 'standard', 'emailing', ... * @param string $sendcontext 'standard', 'emailing', ...
*/ */
function __construct($subject,$to,$from,$msg,$filename_list=array(),$mimetype_list=array(),$mimefilename_list=array(),$addr_cc="",$addr_bcc="",$deliveryreceipt=0,$msgishtml=0,$errors_to='',$css='',$trackid='',$moreinheader='',$sendingcontext='standard') function __construct($subject,$to,$from,$msg,$filename_list=array(),$mimetype_list=array(),$mimefilename_list=array(),$addr_cc="",$addr_bcc="",$deliveryreceipt=0,$msgishtml=0,$errors_to='',$css='',$trackid='',$moreinheader='',$sendcontext='standard')
{ {
global $conf, $dolibarr_main_data_root; global $conf, $dolibarr_main_data_root;
$this->sendingcontext = $sendingcontext; $this->sendcontext = $sendcontext;
$this->sendmode = '';
if ($this->sendcontext == 'emailing') $this->sendmode = $conf->global->EMAILING_MAIL_SENDMODE;
if (empty($this->sendmode)) $this->sendmode=$conf->global->MAIN_MAIL_SENDMODE;
if (empty($this->sendmode)) $this->sendmode='mail';
$this->sendsetup = array();
// We define end of line (RFC 821). // We define end of line (RFC 821).
$this->eol="\r\n"; $this->eol="\r\n";
@ -132,10 +144,7 @@ class CMailFile
// On defini alternative_boundary // On defini alternative_boundary
$this->alternative_boundary = 'mul_'.dol_hash(uniqid("dolibarr3"), 3); // Force md5 hash (does not contains special chars) $this->alternative_boundary = 'mul_'.dol_hash(uniqid("dolibarr3"), 3); // Force md5 hash (does not contains special chars)
// If ending method not defined dol_syslog("CMailFile::CMailfile: sendmode=".$this->sendmode." charset=".$conf->file->character_set_client." from=$from, to=$to, addr_cc=$addr_cc, addr_bcc=$addr_bcc, errors_to=$errors_to, trackid=$trackid sendcontext=$sendcontext", LOG_DEBUG);
if (empty($conf->global->MAIN_MAIL_SENDMODE)) $conf->global->MAIN_MAIL_SENDMODE='mail';
dol_syslog("CMailFile::CMailfile: MAIN_MAIL_SENDMODE=".$conf->global->MAIN_MAIL_SENDMODE." charset=".$conf->file->character_set_client." from=$from, to=$to, addr_cc=$addr_cc, addr_bcc=$addr_bcc, errors_to=$errors_to, trackid=$trackid sendingcontext=$sendingcontext", LOG_DEBUG);
dol_syslog("CMailFile::CMailfile: subject=$subject, deliveryreceipt=$deliveryreceipt, msgishtml=$msgishtml", LOG_DEBUG); dol_syslog("CMailFile::CMailfile: subject=$subject, deliveryreceipt=$deliveryreceipt, msgishtml=$msgishtml", LOG_DEBUG);
if (empty($subject)) if (empty($subject))
@ -196,7 +205,7 @@ class CMailFile
if (! empty($conf->global->MAIN_MAIL_AUTOCOPY_TO)) $addr_bcc.=($addr_bcc?', ':'').$conf->global->MAIN_MAIL_AUTOCOPY_TO; if (! empty($conf->global->MAIN_MAIL_AUTOCOPY_TO)) $addr_bcc.=($addr_bcc?', ':'').$conf->global->MAIN_MAIL_AUTOCOPY_TO;
// Action according to choosed sending method // Action according to choosed sending method
if ($conf->global->MAIN_MAIL_SENDMODE == 'mail') if ($this->sendmode == 'mail')
{ {
// Use mail php function (default PHP method) // Use mail php function (default PHP method)
// ------------------------------------------ // ------------------------------------------
@ -254,7 +263,7 @@ class CMailFile
$this->message.= $text_body . $files_encoded; $this->message.= $text_body . $files_encoded;
$this->message.= "--" . $this->mixed_boundary . "--" . $this->eol; $this->message.= "--" . $this->mixed_boundary . "--" . $this->eol;
} }
else if ($conf->global->MAIN_MAIL_SENDMODE == 'smtps') else if ($this->sendmode == 'smtps')
{ {
// Use SMTPS library // Use SMTPS library
// ------------------------------------------ // ------------------------------------------
@ -310,7 +319,7 @@ class CMailFile
$this->smtps=$smtps; $this->smtps=$smtps;
} }
// TODO not stable, in progress // TODO not stable, in progress
else if ($conf->global->MAIN_MAIL_SENDMODE == 'phpmailer') else if ($this->sendmode == 'phpmailer')
{ {
// Use PHPMailer library // Use PHPMailer library
// ------------------------------------------ // ------------------------------------------
@ -362,7 +371,7 @@ class CMailFile
$this->phpmailer->setErrorsTo($errors_to); $this->phpmailer->setErrorsTo($errors_to);
$this->phpmailer->setDeliveryReceipt($deliveryreceipt); $this->phpmailer->setDeliveryReceipt($deliveryreceipt);
} }
else if ($conf->global->MAIN_MAIL_SENDMODE == 'swiftmailer') else if ($this->sendmode == 'swiftmailer')
{ {
// Use Swift Mailer library // Use Swift Mailer library
// ------------------------------------------ // ------------------------------------------
@ -449,7 +458,7 @@ class CMailFile
{ {
// Send mail method not correctly defined // Send mail method not correctly defined
// -------------------------------------- // --------------------------------------
$this->error = 'Bad value for MAIN_MAIL_SENDMODE constant'; $this->error = 'Bad value for sendmode';
} }
} }
@ -519,7 +528,7 @@ class CMailFile
} }
// Action according to choosed sending method // Action according to choosed sending method
if ($conf->global->MAIN_MAIL_SENDMODE == 'mail') if ($this->sendmode == 'mail')
{ {
// Use mail php function (default PHP method) // Use mail php function (default PHP method)
// ------------------------------------------ // ------------------------------------------
@ -600,7 +609,7 @@ class CMailFile
if (! empty($conf->global->MAIN_MAIL_SMTP_SERVER)) ini_restore('SMTP'); if (! empty($conf->global->MAIN_MAIL_SMTP_SERVER)) ini_restore('SMTP');
if (! empty($conf->global->MAIN_MAIL_SMTP_PORT)) ini_restore('smtp_port'); if (! empty($conf->global->MAIN_MAIL_SMTP_PORT)) ini_restore('smtp_port');
} }
else if ($conf->global->MAIN_MAIL_SENDMODE == 'smtps') else if ($this->sendmode == 'smtps')
{ {
// Use SMTPS library // Use SMTPS library
@ -668,7 +677,7 @@ class CMailFile
} }
} }
} }
else if ($conf->global->MAIN_MAIL_SENDMODE == 'swiftmailer') else if ($this->sendmode == 'swiftmailer')
{ {
// Use Swift Mailer library // Use Swift Mailer library
@ -724,7 +733,7 @@ class CMailFile
// Send mail method not correctly defined // Send mail method not correctly defined
// -------------------------------------- // --------------------------------------
return 'Bad value for MAIN_MAIL_SENDMODE constant'; return 'Bad value for sendmode';
} }
} }
@ -792,17 +801,17 @@ class CMailFile
$outputfile=$dolibarr_main_data_root."/dolibarr_mail.log"; $outputfile=$dolibarr_main_data_root."/dolibarr_mail.log";
$fp = fopen($outputfile,"w"); $fp = fopen($outputfile,"w");
if ($conf->global->MAIN_MAIL_SENDMODE == 'mail') if ($this->sendmode == 'mail')
{ {
fputs($fp, $this->headers); fputs($fp, $this->headers);
fputs($fp, $this->eol); // This eol is added by the mail function, so we add it in log fputs($fp, $this->eol); // This eol is added by the mail function, so we add it in log
fputs($fp, $this->message); fputs($fp, $this->message);
} }
elseif ($conf->global->MAIN_MAIL_SENDMODE == 'smtps') elseif ($this->sendmode == 'smtps')
{ {
fputs($fp, $this->smtps->log); // this->smtps->log is filled only if MAIN_MAIL_DEBUG was set to on fputs($fp, $this->smtps->log); // this->smtps->log is filled only if MAIN_MAIL_DEBUG was set to on
} }
elseif ($conf->global->MAIN_MAIL_SENDMODE == 'swiftmailer') elseif ($this->sendmode == 'swiftmailer')
{ {
fputs($fp, $this->logger->dump()); // this->logger is filled only if MAIN_MAIL_DEBUG was set to on fputs($fp, $this->logger->dump()); // this->logger is filled only if MAIN_MAIL_DEBUG was set to on
} }
@ -1074,7 +1083,9 @@ class CMailFile
if ($encoded >= 0) if ($encoded >= 0)
{ {
if ($mimefilename_list[$i]) $filename_list[$i] = $mimefilename_list[$i]; if ($mimefilename_list[$i]) $filename_list[$i] = $mimefilename_list[$i];
if (! $mimetype_list[$i]) { $mimetype_list[$i] = "application/octet-stream"; } if (! $mimetype_list[$i]) {
$mimetype_list[$i] = "application/octet-stream";
}
$out.= "--" . $this->mixed_boundary . $this->eol; $out.= "--" . $this->mixed_boundary . $this->eol;
$out.= "Content-Disposition: attachment; filename=\"".$filename_list[$i]."\"".$this->eol; $out.= "Content-Disposition: attachment; filename=\"".$filename_list[$i]."\"".$this->eol;