/**
  *	CMailFile
  *
  *	@param 	string	$subject             Topic/Subject of mail
  *	@param 	string	$to                  Recipients emails (RFC 2822: "Nom firstname <email>[, ...]" ou "email[, ...]" ou "<email>[, ...]")
  *	@param 	string	$from                Sender email      (RFC 2822: "Nom firstname <email>[, ...]" ou "email[, ...]" ou "<email>[, ...]")
  *	@param 	string	$msg                 Message
  *	@param 	array	$filename_list       List of files to attach (full path of filename on file system)
  *	@param 	array	$mimetype_list       List of MIME type of attached files
  *	@param 	array	$mimefilename_list   List of attached file name in message
  *	@param 	string	$addr_cc             Email cc
  *	@param 	string	$addr_bcc            Email bcc (Note: This is autocompleted with MAIN_MAIL_AUTOCOPY_TO if defined)
  *	@param 	int		$deliveryreceipt     Ask a delivery receipt
  *	@param 	int		$msgishtml           1=String IS already html, 0=String IS NOT html, -1=Unknown make autodetection (with fast mode, not reliable)
  *	@param 	string	$errors_to      	 Email errors
  *	@param	string	$css                 Css option
  */
 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 = '')
 {
     global $conf;
     // We define end of line (RFC 821).
     $this->eol = "\r\n";
     // We define end of line for header fields (RFC 822bis section 2.3 says header must contains \r\n).
     $this->eol2 = "\r\n";
     if (!empty($conf->global->MAIN_FIX_FOR_BUGGED_MTA)) {
         $this->eol = "\n";
         $this->eol2 = "\n";
     }
     // On defini mixed_boundary
     $this->mixed_boundary = "multipart_x." . time() . ".x_boundary";
     // On defini related_boundary
     $this->related_boundary = 'mul_' . dol_hash(uniqid("dolibarr2"));
     // On defini alternative_boundary
     $this->alternative_boundary = 'mul_' . dol_hash(uniqid("dolibarr3"));
     // If ending method not defined
     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}", LOG_DEBUG);
     dol_syslog("CMailFile::CMailfile: subject={$subject}, deliveryreceipt={$deliveryreceipt}, msgishtml={$msgishtml}", LOG_DEBUG);
     // Detect if message is HTML (use fast method)
     if ($msgishtml == -1) {
         $this->msgishtml = 0;
         if (dol_textishtml($msg)) {
             $this->msgishtml = 1;
         }
     } else {
         $this->msgishtml = $msgishtml;
     }
     // Detect images
     if ($this->msgishtml) {
         $this->html = $msg;
         $findimg = $this->findHtmlImages($conf->fckeditor->dir_output);
         // Define if there is at least one file
         if ($findimg) {
             foreach ($this->html_images as $i => $val) {
                 if ($this->html_images[$i]) {
                     $this->atleastoneimage = 1;
                     dol_syslog("CMailFile::CMailfile: html_images[{$i}]['name']=" . $this->html_images[$i]['name'], LOG_DEBUG);
                 }
             }
         }
     }
     // Define if there is at least one file
     foreach ($filename_list as $i => $val) {
         if ($filename_list[$i]) {
             $this->atleastonefile = 1;
             dol_syslog("CMailFile::CMailfile: filename_list[{$i}]=" . $filename_list[$i] . ", mimetype_list[{$i}]=" . $mimetype_list[$i] . " mimefilename_list[{$i}]=" . $mimefilename_list[$i], LOG_DEBUG);
         }
     }
     // Add 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
     if ($conf->global->MAIN_MAIL_SENDMODE == 'mail') {
         // Use mail php function (default PHP method)
         // ------------------------------------------
         $smtp_headers = "";
         $mime_headers = "";
         $text_body = "";
         $files_encoded = "";
         // Define smtp_headers
         $this->subject = $subject;
         $this->addr_from = $from;
         $this->errors_to = $errors_to;
         $this->addr_to = $to;
         $this->addr_cc = $addr_cc;
         $this->addr_bcc = $addr_bcc;
         $this->deliveryreceipt = $deliveryreceipt;
         $smtp_headers = $this->write_smtpheaders();
         // Define mime_headers
         $mime_headers = $this->write_mimeheaders($filename_list, $mimefilename_list);
         if (!empty($this->html)) {
             if (!empty($css)) {
                 $this->css = $css;
                 $this->buildCSS();
                 // Build a css style (mode = all) into this->styleCSS and this->bodyCSS
             }
             $msg = $this->html;
         }
         // Define body in text_body
         $text_body = $this->write_body($msg);
         // Encode images
         $images_encoded = '';
         if ($this->atleastoneimage) {
             $images_encoded .= $this->write_images($this->images_encoded);
             // always end related and end alternative after inline images
             $images_encoded .= "--" . $this->related_boundary . "--" . $this->eol;
             $images_encoded .= $this->eol . "--" . $this->alternative_boundary . "--" . $this->eol;
             $images_encoded .= $this->eol;
         }
         // Add attachments to text_encoded
         if ($this->atleastonefile) {
             $files_encoded = $this->write_files($filename_list, $mimetype_list, $mimefilename_list);
         }
         // We now define $this->headers and $this->message
         $this->headers = $smtp_headers . $mime_headers;
         // 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
         // comme des injections mail par les serveurs de messagerie.
         $this->headers = preg_replace("/([\r\n]+)\$/i", "", $this->headers);
         $this->message = 'This is a message with multiple parts in MIME format.' . $this->eol;
         $this->message .= $text_body . $images_encoded . $files_encoded;
         $this->message .= "--" . $this->mixed_boundary . "--" . $this->eol;
     } else {
         if ($conf->global->MAIN_MAIL_SENDMODE == 'smtps') {
             // Use SMTPS library
             // ------------------------------------------
             require_once DOL_DOCUMENT_ROOT . '/core/class/smtps.class.php';
             $smtps = new SMTPs();
             $smtps->setCharSet($conf->file->character_set_client);
             $smtps->setSubject($this->encodetorfc2822($subject));
             $smtps->setTO($this->getValidAddress($to, 0, 1));
             $smtps->setFrom($this->getValidAddress($from, 0, 1));
             if (!empty($this->html)) {
                 if (!empty($css)) {
                     $this->css = $css;
                     $this->buildCSS();
                 }
                 $msg = $this->html;
                 $msg = $this->checkIfHTML($msg);
             }
             if ($this->msgishtml) {
                 $smtps->setBodyContent($msg, 'html');
             } else {
                 $smtps->setBodyContent($msg, 'plain');
             }
             if ($this->atleastoneimage) {
                 foreach ($this->images_encoded as $img) {
                     $smtps->setImageInline($img['image_encoded'], $img['name'], $img['content_type'], $img['cid']);
                 }
             }
             if ($this->atleastonefile) {
                 foreach ($filename_list as $i => $val) {
                     $content = file_get_contents($filename_list[$i]);
                     $smtps->setAttachment($content, $mimefilename_list[$i], $mimetype_list[$i]);
                 }
             }
             $smtps->setCC($addr_cc);
             $smtps->setBCC($addr_bcc);
             $smtps->setErrorsTo($errors_to);
             $smtps->setDeliveryReceipt($deliveryreceipt);
             $this->smtps = $smtps;
         } else {
             if ($conf->global->MAIN_MAIL_SENDMODE == 'phpmailer') {
                 // Use PHPMailer library
                 // ------------------------------------------
                 require_once DOL_DOCUMENT_ROOT . '/includes/phpmailer/class.phpmailer.php';
                 $this->phpmailer = new PHPMailer();
                 $this->phpmailer->CharSet = $conf->file->character_set_client;
                 $this->phpmailer->Subject($this->encodetorfc2822($subject));
                 $this->phpmailer->setTO($this->getValidAddress($to, 0, 1));
                 $this->phpmailer->SetFrom($this->getValidAddress($from, 0, 1));
                 if (!empty($this->html)) {
                     if (!empty($css)) {
                         $this->css = $css;
                         $this->buildCSS();
                     }
                     $msg = $this->html;
                     $msg = $this->checkIfHTML($msg);
                 }
                 if ($this->msgishtml) {
                     $smtps->setBodyContent($msg, 'html');
                 } else {
                     $smtps->setBodyContent($msg, 'plain');
                 }
                 if ($this->atleastoneimage) {
                     foreach ($this->images_encoded as $img) {
                         $smtps->setImageInline($img['image_encoded'], $img['name'], $img['content_type'], $img['cid']);
                     }
                 }
                 if ($this->atleastonefile) {
                     foreach ($filename_list as $i => $val) {
                         $content = file_get_contents($filename_list[$i]);
                         $smtps->setAttachment($content, $mimefilename_list[$i], $mimetype_list[$i]);
                     }
                 }
                 $smtps->setCC($addr_cc);
                 $smtps->setBCC($addr_bcc);
                 $smtps->setErrorsTo($errors_to);
                 $smtps->setDeliveryReceipt($deliveryreceipt);
                 $this->smtps = $smtps;
             } else {
                 // Send mail method not correctly defined
                 // --------------------------------------
                 return 'Bad value for MAIN_MAIL_SENDMODE constant';
             }
         }
     }
 }
Ejemplo n.º 2
0
	/**
	 *	CMailFile
	 *
	 *	@param 	subject             Topic/Subject of mail
	 *	@param 	to                  Recipients emails (RFC 2822: "Nom prenom <email>[, ...]" ou "email[, ...]" ou "<email>[, ...]")
	 *	@param 	from                Sender email      (RFC 2822: "Nom prenom <email>[, ...]" ou "email[, ...]" ou "<email>[, ...]")
	 *	@param 	msg                 Message
	 *	@param 	filename_list       List of files to attach (full path of filename on file system)
	 *	@param 	mimetype_list       List of MIME type of attached files
	 *	@param 	mimefilename_list   List of attached file name in message
	 *	@param 	addr_cc             Email cc
	 *	@param 	addr_bcc            Email bcc
	 *	@param 	deliveryreceipt		Ask a delivery receipt
	 *	@param 	msgishtml       	1=String IS already html, 0=String IS NOT html, -1=Unknown need autodetection
	 *	@param 	errors_to      		Email errors
	 *	@param	css			        Css option
	 */
	function CMailFile($subject,$to,$from,$msg,
	$filename_list=array(),$mimetype_list=array(),$mimefilename_list=array(),
	$addr_cc="",$addr_bcc="",$deliveryreceipt=0,$msgishtml=0,$errors_to='',$css='')
	{
		global $conf;

		// We define end of line (RFC 822bis section 2.3)
		$this->eol="\r\n";
		//if (preg_match('/^win/i',PHP_OS)) $this->eol="\r\n";
		//if (preg_match('/^mac/i',PHP_OS)) $this->eol="\r";

		// On defini mixed_boundary
		$this->mixed_boundary = md5(uniqid("dolibarr1"));

		// On defini related_boundary
		$this->related_boundary = md5(uniqid("dolibarr2"));

		// On defini alternative_boundary
		$this->alternative_boundary = md5(uniqid("dolibarr3"));

		// If ending method not defined
		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", LOG_DEBUG);
		dol_syslog("CMailFile::CMailfile: subject=$subject, deliveryreceipt=$deliveryreceipt, msgishtml=$msgishtml", LOG_DEBUG);

		// Detect if message is HTML (use fast method)
		if ($msgishtml == -1)
		{
			$this->msgishtml = 0;
			if (dol_textishtml($msg)) $this->msgishtml = 1;
		}
		else
		{
			$this->msgishtml = $msgishtml;
		}

		// Detect images
		if ($this->msgishtml)
		{
			$this->html = $msg;
			$findimg = $this->findHtmlImages($conf->fckeditor->dir_output);
			// Define if there is at least one file
			if ($findimg)
			{
				foreach ($this->html_images as $i => $val)
				{
					if ($this->html_images[$i])
					{
						$this->atleastoneimage=1;
						dol_syslog("CMailFile::CMailfile: html_images[$i]['name']=".$this->html_images[$i]['name'], LOG_DEBUG);
					}
				}
			}
		}

		// Define if there is at least one file
		foreach ($filename_list as $i => $val)
		{
			if ($filename_list[$i])
			{
				$this->atleastonefile=1;
				dol_syslog("CMailFile::CMailfile: filename_list[$i]=".$filename_list[$i].", mimetype_list[$i]=".$mimetype_list[$i]." mimefilename_list[$i]=".$mimefilename_list[$i], LOG_DEBUG);
			}
		}

		// Add 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
		if ($conf->global->MAIN_MAIL_SENDMODE == 'mail')
		{
			// Use mail php function (default PHP method)
			// ------------------------------------------

			$smtp_headers = "";
			$mime_headers = "";
			$text_body = "";
			$files_encoded = "";

			// Define smtp_headers
			$this->subject = $subject;
			$this->addr_from = $from;
			$this->errors_to = $errors_to;
			$this->addr_to = $to;
			$this->addr_cc = $addr_cc;
			$this->addr_bcc = $addr_bcc;
			$this->deliveryreceipt = $deliveryreceipt;
			$smtp_headers = $this->write_smtpheaders();

			// Define mime_headers
			$mime_headers = $this->write_mimeheaders($filename_list, $mimefilename_list);

			if (! empty($this->html))
			{
				if (!empty($css))
				{
					$this->css = $css;
					$this->buildCSS();
				}

				$msg = $this->html;
			}

			// Define body in text_body
			$text_body = $this->write_body($msg);

			// Encode images
			if ($this->atleastoneimage)
			{
				$images_encoded = $this->write_images($this->images_encoded);
				// always end related and end alternative after inline images
				$images_encoded.= "--" . $this->related_boundary . "--" . $this->eol;
				$images_encoded.= $this->eol . "--" . $this->alternative_boundary . "--" . $this->eol;
				$images_encoded.= $this->eol;
			}

			// Add attachments to text_encoded
			if ($this->atleastonefile)
			{
				$files_encoded = $this->write_files($filename_list,$mimetype_list,$mimefilename_list);
			}

			// We now define $this->headers et $this->message
			$this->headers = $smtp_headers . $mime_headers;

			$this->message = $text_body . $images_encoded . $files_encoded;
			$this->message.= "--" . $this->mixed_boundary . "--" . $this->eol;

			// 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
			// comme des injections mail par les serveurs de messagerie.
			$this->headers = preg_replace("/([\r\n]+)$/i","",$this->headers);
		}
		else if ($conf->global->MAIN_MAIL_SENDMODE == 'smtps')
		{
			// Use SMTPS library
			// ------------------------------------------

			require_once(DOL_DOCUMENT_ROOT."/includes/smtps/SMTPs.php");
			$smtps = new SMTPs();
			$smtps->setCharSet($conf->file->character_set_client);

			$smtps->setSubject($this->encodetorfc2822($subject));
			$smtps->setTO($this->getValidAddress($to,0,1));
			$smtps->setFrom($this->getValidAddress($from,0,1));

			if (! empty($this->html))
			{
				if (!empty($css))
				{
					$this->css = $css;
					$this->styleCSS = $this->buildCSS();
				}
				$msg = $this->html;
				$msg = $this->checkIfHTML($msg);
			}

			if ($this->msgishtml) $smtps->setBodyContent($msg,'html');
			else $smtps->setBodyContent($msg,'plain');

			if ($this->atleastoneimage)
			{
				foreach ($this->images_encoded as $img)
				{
					$smtps->setImageInline($img['image_encoded'],$img['name'],$img['content_type'],$img['cid']);
				}
			}

			if ($this->atleastonefile)
			{
				foreach ($filename_list as $i => $val)
				{
					$content=file_get_contents($filename_list[$i]);
					$smtps->setAttachment($content,$mimefilename_list[$i],$mimetype_list[$i]);
				}
			}

			$smtps->setCC($addr_cc);
			$smtps->setBCC($addr_bcc);
			$smtps->setErrorsTo($errors_to);
			$smtps->setDeliveryReceipt($deliveryreceipt);

			$this->smtps=$smtps;
		}
		else
		{
			// Send mail method not correctly defined
			// --------------------------------------

			return 'Bad value for MAIN_MAIL_SENDMODE constant';
		}

	}