2
0
forked from Wavyzz/dolibarr

Fix: Pb with file attachment if mail is HTML

This commit is contained in:
Laurent Destailleur
2008-07-16 23:37:34 +00:00
parent c22c18dbfb
commit 2e18666d5d
6 changed files with 109 additions and 46 deletions

View File

@@ -60,7 +60,7 @@ if (isset($_POST["action"]) && $_POST["action"] == 'update')
/* /*
* Add file * Add file
*/ */
if ($_POST['addfile']) if ($_POST['addfile'] || $_POST['addfilehtml'])
{ {
// Set tmp user directory // Set tmp user directory
$conf->users->dir_tmp=DOL_DATA_ROOT."/users/".$user->id; $conf->users->dir_tmp=DOL_DATA_ROOT."/users/".$user->id;
@@ -74,7 +74,7 @@ if ($_POST['addfile'])
{ {
if (dol_move_uploaded_file($_FILES['addedfile']['tmp_name'], $upload_dir . "/" . $_FILES['addedfile']['name'],0) > 0) if (dol_move_uploaded_file($_FILES['addedfile']['tmp_name'], $upload_dir . "/" . $_FILES['addedfile']['name'],0) > 0)
{ {
$mesg = '<div class="ok">'.$langs->trans("FileTransferComplete").'</div>'; $message = '<div class="ok">'.$langs->trans("FileTransferComplete").'</div>';
//print_r($_FILES); //print_r($_FILES);
include_once(DOL_DOCUMENT_ROOT.'/html.formmail.class.php'); include_once(DOL_DOCUMENT_ROOT.'/html.formmail.class.php');
@@ -84,18 +84,20 @@ if ($_POST['addfile'])
else else
{ {
// Echec transfert (fichier d<>passant la limite ?) // Echec transfert (fichier d<>passant la limite ?)
$mesg = '<div class="error">'.$langs->trans("ErrorFileNotUploaded").'</div>'; $message = '<div class="error">'.$langs->trans("ErrorFileNotUploaded").'</div>';
// print_r($_FILES); // print_r($_FILES);
} }
} }
} }
$_GET["action"]='test'; if ($_POST['addfile']) $_GET["action"]='test';
if ($_POST['addfilehtml']) $_GET["action"]='testhtml';
} }
/* /*
* Send mail * Send mail
*/ */
if ($_POST['action'] == 'send' && ! $_POST['addfile'] && ! $_POST['cancel']) if (($_POST['action'] == 'send' || $_POST['action'] == 'sendhtml')
&& ! $_POST['addfile'] && ! $_POST['addfilehtml'] && ! $_POST['cancel'])
{ {
$error=0; $error=0;
@@ -108,19 +110,14 @@ if ($_POST['action'] == 'send' && ! $_POST['addfile'] && ! $_POST['cancel'])
$subject = $_POST['subject']; $subject = $_POST['subject'];
$body = $_POST['message']; $body = $_POST['message'];
// Get list of attached files // Create form object
$listofpaths=array(); include_once('../html.formmail.class.php');
$listofnames=array(); $formmail = new FormMail($db);
$listofmimes=array();
if (! empty($_SESSION["listofpaths"])) $listofpaths=split(';',$_SESSION["listofpaths"]); $attachedfiles=$formmail->get_attached_files();
if (! empty($_SESSION["listofnames"])) $listofnames=split(';',$_SESSION["listofnames"]); $filepath = $attachedfiles['paths'];
if (! empty($_SESSION["listofmimes"])) $listofmimes=split(';',$_SESSION["listofmimes"]); $filename = $attachedfiles['names'];
if (! empty($_FILES['addedfile']['tmp_name'])) $mimetype = $attachedfiles['mimes'];
{
$listofpaths[] = $_FILES['addedfile']['tmp_name'];
$listofnames[] = $_FILES['addedfile']['name'];
$listofmimes[] = $_FILES['addedfile']['type'];
}
if (empty($_POST["frommail"])) if (empty($_POST["frommail"]))
{ {
@@ -137,8 +134,8 @@ if ($_POST['action'] == 'send' && ! $_POST['addfile'] && ! $_POST['cancel'])
if (! $error) if (! $error)
{ {
// Le message est-il en html // Le message est-il en html
$msgishtml=-1; // Unknown by default $msgishtml=0; // Message is not HTML
if (eregi('[ \t]*<html>',$message)) $msgishtml=1; if ($_POST['action'] == 'sendhtml') $msgishtml=1; // Force message to HTML
// Pratique les substitutions sur le sujet et message // Pratique les substitutions sur le sujet et message
$subject=make_substitutions($subject,$substitutionarrayfortest); $subject=make_substitutions($subject,$substitutionarrayfortest);
@@ -146,7 +143,7 @@ if ($_POST['action'] == 'send' && ! $_POST['addfile'] && ! $_POST['cancel'])
require_once(DOL_DOCUMENT_ROOT."/lib/CMailFile.class.php"); require_once(DOL_DOCUMENT_ROOT."/lib/CMailFile.class.php");
$mailfile = new CMailFile($subject,$sendto,$email_from,$body, $mailfile = new CMailFile($subject,$sendto,$email_from,$body,
$listofpaths,$listofmimes,$listofnames, $filepath,$mimetype,$filename,
'', '', 0, $msgishtml,$errors_to); '', '', 0, $msgishtml,$errors_to);
$result=$mailfile->sendfile(); $result=$mailfile->sendfile();
@@ -253,6 +250,10 @@ else
print '<a class="butAction" href="'.$_SERVER["PHP_SELF"].'?action=testconnect">'.$langs->trans("DoTestServerAvailability").'</a>'; print '<a class="butAction" href="'.$_SERVER["PHP_SELF"].'?action=testconnect">'.$langs->trans("DoTestServerAvailability").'</a>';
} }
print '<a class="butAction" href="'.$_SERVER["PHP_SELF"].'?action=test&amp;mode=init">'.$langs->trans("DoTestSend").'</a>'; print '<a class="butAction" href="'.$_SERVER["PHP_SELF"].'?action=test&amp;mode=init">'.$langs->trans("DoTestSend").'</a>';
if ($conf->fckeditor->enabled)
{
print '<a class="butAction" href="'.$_SERVER["PHP_SELF"].'?action=testhtml&amp;mode=init">'.$langs->trans("DoTestSendHTML").'</a>';
}
print '<a class="butAction" href="'.$_SERVER["PHP_SELF"].'?action=edit">'.$langs->trans("Modify").'</a>'; print '<a class="butAction" href="'.$_SERVER["PHP_SELF"].'?action=edit">'.$langs->trans("Modify").'</a>';
print '</div>'; print '</div>';
@@ -277,7 +278,7 @@ else
print '<br>'; print '<br>';
} }
// Affichage formulaire de TEST // Affichage formulaire de TEST simple
if ($_GET["action"] == 'test') if ($_GET["action"] == 'test')
{ {
print '<br>'; print '<br>';
@@ -315,11 +316,54 @@ else
$formmail->clear_attached_files(); $formmail->clear_attached_files();
} }
$formmail->show_form(); $formmail->show_form('addfile');
print '<br>'; print '<br>';
} }
// Affichage formulaire de TEST HTML
if ($_GET["action"] == 'testhtml')
{
print '<br>';
print_titre($langs->trans("DoTestSendHTML"));
// Cree l'objet formulaire mail
include_once(DOL_DOCUMENT_ROOT."/html.formmail.class.php");
$formmail = new FormMail($db);
$formmail->fromname = $conf->global->MAIN_MAIL_EMAIL_FROM;
$formmail->frommail = $conf->global->MAIN_MAIL_EMAIL_FROM;
$formmail->withfromreadonly=0;
$formmail->withsubstit=0;
$formmail->withfrom=1;
$formmail->witherrorsto=1;
$formmail->withto=$user->email?$user->email:1;
$formmail->withtocc=1;
$formmail->withtopic=$langs->trans("Test");
$formmail->withtopicreadonly=0;
$formmail->withfile=2;
$formmail->withbody=$langs->trans("Test");
$formmail->withbodyreadonly=0;
$formmail->withcancel=1;
$formmail->withdeliveryreceipt=1;
$formmail->withfckeditor=1;
// Tableau des substitutions
$formmail->substit=$substitutionarrayfortest;
// Tableau des parametres complementaires du post
$formmail->param["action"]="sendhtml";
$formmail->param["models"]="body";
$formmail->param["mailid"]=$mil->id;
$formmail->param["returnurl"]=DOL_URL_ROOT."/admin/mails.php";
// Init list of files
if (! empty($_REQUEST["mode"]) && $_REQUEST["mode"]=='init')
{
$formmail->clear_attached_files();
}
$formmail->show_form('addfilehtml');
print '<br>';
}
} }

View File

@@ -154,9 +154,10 @@ class FormMail
/** /**
* \brief Affiche la partie de formulaire pour saisie d'un mail en fonction des propri<72>t<EFBFBD>s * \brief Affiche la partie de formulaire pour saisie d'un mail en fonction des propri<72>t<EFBFBD>s
* \param addfileaction Name of action when posting file attachments
* \remarks this->withfile: 0=No attaches files, 1=Show attached files, 2=Can add new attached files * \remarks this->withfile: 0=No attaches files, 1=Show attached files, 2=Can add new attached files
*/ */
function show_form() function show_form($addfileaction='addfile')
{ {
global $conf, $langs, $user; global $conf, $langs, $user;
@@ -391,7 +392,7 @@ class FormMail
//print '<td><td align="right">'; //print '<td><td align="right">';
print "<input type=\"file\" class=\"flat\" name=\"addedfile\" value=\"".$langs->trans("Upload")."\"/>"; print "<input type=\"file\" class=\"flat\" name=\"addedfile\" value=\"".$langs->trans("Upload")."\"/>";
print ' '; print ' ';
print '<input type="submit" class="button" name="addfile" value="'.$langs->trans("MailingAddFile").'">'; print '<input type="submit" class="button" name="'.$addfileaction.'" value="'.$langs->trans("MailingAddFile").'">';
//print '</td></tr></table>'; //print '</td></tr></table>';
} }
print "</td></tr>\n"; print "</td></tr>\n";
@@ -420,11 +421,22 @@ class FormMail
print '<input type="hidden" name="message" value="'.$defaultmessage.'">'; print '<input type="hidden" name="message" value="'.$defaultmessage.'">';
} }
else else
{
if ($this->withfckeditor)
{
// Editeur wysiwyg
require_once(DOL_DOCUMENT_ROOT."/lib/doleditor.class.php");
$doleditor=new DolEditor('message',$defaultmessage,280,'dolibarr_notes','In',true);
$doleditor->Create();
}
else
{ {
print '<textarea cols="72" rows="8" name="message">'; print '<textarea cols="72" rows="8" name="message">';
print $defaultmessage; print $defaultmessage;
print '</textarea>'; print '</textarea>';
} }
}
print "</td></tr>\n"; print "</td></tr>\n";
} }

View File

@@ -198,6 +198,7 @@ ServerAvailableOnIPOrPort=Server is available at address <b>%s</b> on port <b>%s
ServerNotAvailableOnIPOrPort=Server is not available at address <b>%s</b> on port <b>%s</b> ServerNotAvailableOnIPOrPort=Server is not available at address <b>%s</b> on port <b>%s</b>
DoTestServerAvailability=Test server connectivity DoTestServerAvailability=Test server connectivity
DoTestSend=Test sending DoTestSend=Test sending
DoTestSendHTML=Test sending HTML
ErrorCantUseRazInStartedYearIfNoYearMonthInMask=Error, can't user option @ if sequence {yy}{mm} or {yyyy}{mm} is not in mask. ErrorCantUseRazInStartedYearIfNoYearMonthInMask=Error, can't user option @ if sequence {yy}{mm} or {yyyy}{mm} is not in mask.
# Modules # Modules

View File

@@ -197,6 +197,7 @@ ServerAvailableOnIPOrPort=Serveur disponible a l'adresse <b>%s</b> sur le port <
ServerNotAvailableOnIPOrPort=Serveur non disponible <20> l'adresse <b>%s</b> sur le port <b>%s</b> ServerNotAvailableOnIPOrPort=Serveur non disponible <20> l'adresse <b>%s</b> sur le port <b>%s</b>
DoTestServerAvailability=Tester disponibilit<69> serveur DoTestServerAvailability=Tester disponibilit<69> serveur
DoTestSend=Tester envoi DoTestSend=Tester envoi
DoTestSendHTML=Tester envoi HTML
ErrorCantUseRazInStartedYearIfNoYearMonthInMask=Erreur, ne peut utiliser l'option @ si la s<>quence {yy}{mm} ou {yyyy}{mm} n'est pas dans le masque. ErrorCantUseRazInStartedYearIfNoYearMonthInMask=Erreur, ne peut utiliser l'option @ si la s<>quence {yy}{mm} ou {yyyy}{mm} n'est pas dans le masque.
# Modules # Modules

View File

@@ -73,6 +73,8 @@ class CMailFile
{ {
dolibarr_syslog("CMailFile::CMailfile: from=$from, to=$to, addr_cc=$addr_cc, addr_bcc=$addr_bcc, errors_to=$errors_to"); dolibarr_syslog("CMailFile::CMailfile: from=$from, to=$to, addr_cc=$addr_cc, addr_bcc=$addr_bcc, errors_to=$errors_to");
dolibarr_syslog("CMailFile::CMailfile: subject=$subject, deliveryreceipt=$deliveryreceipt, msgishtml=$msgishtml"); dolibarr_syslog("CMailFile::CMailfile: subject=$subject, deliveryreceipt=$deliveryreceipt, msgishtml=$msgishtml");
// Define if there is at least one file
foreach ($filename_list as $i => $val) foreach ($filename_list as $i => $val)
{ {
if ($filename_list[$i]) if ($filename_list[$i])
@@ -90,7 +92,7 @@ class CMailFile
if (eregi('^win',PHP_OS)) $this->eol="\r\n"; if (eregi('^win',PHP_OS)) $this->eol="\r\n";
if (eregi('^mac',PHP_OS)) $this->eol="\r"; if (eregi('^mac',PHP_OS)) $this->eol="\r";
// Detect if message is HTML // Detect if message is HTML (use fast method)
if ($msgishtml == -1) if ($msgishtml == -1)
{ {
$this->msgishtml = 0; $this->msgishtml = 0;
@@ -132,11 +134,10 @@ class CMailFile
} }
// On defini $this->headers et $this->message // On defini $this->headers et $this->message
//$this->headers = $smtp_headers . $mime_headers . $this->eol;
//$this->message = $text_body . $text_encoded . $this->eol;
$this->headers = $smtp_headers . $mime_headers; $this->headers = $smtp_headers . $mime_headers;
$this->message = $text_body . $text_encoded; $this->message = $text_body . $text_encoded;
// On nettoie le header pour qu'il ne se termine pas un retour chariot.
// On nettoie le header pour qu'il ne se termine pas par un retour chariot.
// Ceci evite aussi les lignes vides en fin qui peuvent etre interpretees // Ceci evite aussi les lignes vides en fin qui peuvent etre interpretees
// comme des injections mail par les serveurs de messagerie. // comme des injections mail par les serveurs de messagerie.
$this->headers = eregi_replace("[\r\n]+$","",$this->headers); $this->headers = eregi_replace("[\r\n]+$","",$this->headers);
@@ -177,7 +178,7 @@ class CMailFile
dolibarr_syslog("CMailFile::sendfile addr_to=".$this->addr_to.", subject=".$this->subject); dolibarr_syslog("CMailFile::sendfile addr_to=".$this->addr_to.", subject=".$this->subject);
dolibarr_syslog("CMailFile::sendfile header=\n".$this->headers); dolibarr_syslog("CMailFile::sendfile header=\n".$this->headers);
//dolibarr_syslog("CMailFile::sendfile message=\n".$message); //dolibarr_syslog("CMailFile::sendfile message=\n".$message);
//$this->dump_mail(); $this->dump_mail();
$errorlevel=error_reporting(); $errorlevel=error_reporting();
error_reporting($errorlevel ^ E_WARNING); // Desactive warnings error_reporting($errorlevel ^ E_WARNING); // Desactive warnings
@@ -245,10 +246,13 @@ class CMailFile
*/ */
function dump_mail() function dump_mail()
{ {
if (@is_writeable("/tmp")) // Avoid fatal error on fopen with open_basedir global $dolibarr_main_data_root;
if (@is_writeable($dolibarr_main_data_root)) // Avoid fatal error on fopen with open_basedir
{ {
$fp = fopen("/tmp/dolibarr_mail","w"); $fp = fopen($dolibarr_main_data_root."/dolibarr_mail","w");
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->message); fputs($fp, $this->message);
fclose($fp); fclose($fp);
} }
@@ -282,12 +286,13 @@ class CMailFile
if ($this->msgishtml) if ($this->msgishtml)
{ {
$out.= "Content-Type: text/html; charset=".$conf->character_set_client.$this->eol; if (! $this->atleastonefile) $out.= "Content-Type: text/html; charset=".$conf->character_set_client.$this->eol;
$out.= "Content-Transfer-Encoding: 8bit".$this->eol; $out.= "Content-Transfer-Encoding: 8bit".$this->eol;
} }
else else
{ {
$out.= "Content-Transfer-Encoding: 7bit".$this->eol; if (! $this->atleastonefile) $out.= "Content-Type: text/plain; charset=".$conf->character_set_client.$this->eol;
$out.= "Content-Transfer-Encoding: 8bit".$this->eol;
} }
dolibarr_syslog("CMailFile::write_smtpheaders smtp_header=\n".$out); dolibarr_syslog("CMailFile::write_smtpheaders smtp_header=\n".$out);
@@ -338,11 +343,11 @@ class CMailFile
$out.= "--" . $this->mime_boundary . $this->eol; $out.= "--" . $this->mime_boundary . $this->eol;
if ($this->msgishtml) if ($this->msgishtml)
{ {
$out.= "Content-Type: text/html; charset=".$conf->charset_output.$this->eol; $out.= "Content-Type: text/html; charset=".$conf->character_set_client.$this->eol;
} }
else else
{ {
$out.= "Content-Type: text/plain; charset=".$conf->charset_output.$this->eol; $out.= "Content-Type: text/plain; charset=".$conf->character_set_client.$this->eol;
} }
$out.= $this->eol; $out.= $this->eol;
} }

View File

@@ -1217,7 +1217,7 @@ function img_mime($file)
if (eregi('\.(png|bmp|jpg|jpeg|gif)',$file)) $mime='image'; if (eregi('\.(png|bmp|jpg|jpeg|gif)',$file)) $mime='image';
if (eregi('\.(mp3|ogg|au)',$file)) $mime='audio'; if (eregi('\.(mp3|ogg|au)',$file)) $mime='audio';
if (eregi('\.(avi|mvw|divx|xvid)',$file)) $mime='video'; if (eregi('\.(avi|mvw|divx|xvid)',$file)) $mime='video';
if (eregi('\.(zip|rar|divx|xvid)',$file)) $mime='archive'; if (eregi('\.(zip|rar|gz|tgz|z|cab)',$file)) $mime='archive';
$alt='Mime type: '.$mime; $alt='Mime type: '.$mime;
$mime.='.png'; $mime.='.png';