<commit_before><?php
/*
 * Limb PHP Framework
 *
 * @link http://limb-project.com
 * @copyright  Copyright &copy; 2004-2007 BIT(http://bit-creative.com)
 * @license    LGPL http://www.gnu.org/copyleft/lesser.html
 */
@define('PHPMAILER_DIR', dirname(__FILE__) . '/../lib/phpmailer-1.73/');
@define('LIMB_USE_PHPMAIL', false);
@define('LIMB_SMTP_PORT', '25');
@define('LIMB_SMTP_HOST', 'localhost');
@define('LIMB_SMTP_AUTH', false);
@define('LIMB_SMTP_USER', '');
@define('LIMB_SMTP_PASSWORD', '');

/**
 * class lmbMailer.
 *
 * @package mail
 * @version $Id: lmbMailer.class.php 6856 2008-03-25 12:58:31Z korchasa $
 */
class lmbMailer
{
  protected $attachments = array();

  protected $_config_properties_map = array(
    'phpmailer_dir' => 'PHPMAILER_DIR',
    'use_phpmail' => 'LIMB_USE_PHPMAIL',
    'smtp_host' => 'LIMB_SMTP_HOST',
    'smtp_port' => 'LIMB_SMTP_PORT',
    'smtp_auth' => 'LIMB_SMTP_AUTH',
    'smtp_user' => 'LIMB_SMTP_USER',
    'smtp_password' => '<PASSWORD>'
  );

  public $phpmailer_dir;
  public $use_phpmail;
  public $smtp_host;
  public $smtp_port;
  public $smtp_auth;
  public $smtp_user;
  public $smtp_password;

  function __construct($config = false)
  {
    $this->_setConfigFromDefinedContants();

    if($config)
      $this->setConfig($config);

  }

  protected function _setConfigFromDefinedContants()
  {
    foreach($this->_config_properties_map as $property_name => $define_name)
      $this->$property_name = constant($define_name);
  }

  public function setConfig($config = array())
  {
    foreach($config as $property_name => $property_value)
      $this->$property_name = $property_value;
  }

  protected function _createMailer()
  {
    include_once($this->phpmailer_dir . '/class.phpmailer.php');

    $mailer = new PHPMailer();
    $mailer->LE = "\r\n";

    if($this->use_phpmail)
      return $mailer;

    $mailer->IsSMTP();
    $mailer->Host = $this->smtp_host;
    $mailer->Port = $this->smtp_port;

    if($this->smtp_auth == true)
    {
      $mailer->SMTPAuth = true;
      $mailer->Username = $this->smtp_user;
      $mailer->Password = $<PASSWORD>;
    }
    return $mailer;
  }

  function addAttachment($path, $name = "", $encoding = "base64", $type = "application/octet-stream")
  {
    $this->attachments[] = array(
      'path' => $path,
      'name' => $name,
      'encoding' => $encoding,
      'type' => $type
    );
  }

  function sendPlainMail($recipients, $sender, $subject, $body, $charset = 'utf-8')
  {
    $mailer = $this->_createMailer();

    $mailer->IsHTML(false);
    $mailer->CharSet = $charset;

    if(!empty($this->attachments))
      $this->_addAttachments($mailer);

    $recipients = $this->processMailRecipients($recipients);

    foreach($recipients as $recipient)
      $mailer->AddAddress($recipient['address'], $recipient['name']);

    if(!$sender = $this->processMailAddressee($sender))
      return false;

    $mailer->From = $sender['address'];
    $mailer->FromName = $sender['name'];
    $mailer->Subject = $subject;
    $mailer->Body    = $body;

    return $mailer->Send();
  }

  function sendHtmlMail($recipients, $sender, $subject, $html, $text = null, $charset = 'utf-8')
  {
    $mailer = $this->_createMailer();

    $mailer->IsHTML(true);
    $mailer->CharSet = $charset;

    $mailer->Body = $html;

    if(!empty($this->attachments))
      $this->_addAttachments($mailer);

    if(!is_null($text))
      $mailer->AltBody = $text;

    $recipients = $this->processMailRecipients($recipients);

    foreach($recipients as $recipient)
      $mailer->AddAddress($recipient['address'], $recipient['name']);

    if(!$sender = $this->processMailAddressee($sender))
      return false;

    $mailer->From = $sender['address'];
    $mailer->FromName = $sender['name'];
    $mailer->Subject = $subject;

    return $mailer->Send();
  }

  function processMailRecipients($recipients)
  {
    if(!is_array($recipients))
       $recipients = array($recipients);
    $result = array();
    foreach($recipients as $recipient)
    {
      if($recipient = $this->processMailAddressee($recipient))
        $result[] = $recipient;
    }

    return $result;
  }

  function processMailAddressee($adressee)
  {
    if(is_array($adressee))
    {
      if(isset($adressee['address']) && isset($adressee['name']))
        return $adressee;

      return null;
    }
    elseif(preg_match('~("|\')?([^"\']+)("|\')?\s+<([^>]+)>~', $adressee, $matches))
      return array('address' => $matches[4], 'name' => $matches[2]);
    else
      return array('address' => $adressee, 'name' => '');
  }

  protected function _addAttachments($mailer)
  {
    foreach($this->attachments as $attachment)
      $mailer->AddAttachment($attachment['path'],
                             $attachment['name'],
                             $attachment['encoding'],
                             $attachment['type']);
  }
}<commit_msg>[svn r6865] -- ability to send letters with embedded images added<commit_after><?php
/*
 * Limb PHP Framework
 *
 * @link http://limb-project.com
 * @copyright  Copyright &copy; 2004-2007 BIT(http://bit-creative.com)
 * @license    LGPL http://www.gnu.org/copyleft/lesser.html
 */
@define('PHPMAILER_DIR', dirname(__FILE__) . '/../lib/phpmailer-1.73/');
@define('LIMB_USE_PHPMAIL', false);
@define('LIMB_SMTP_PORT', '25');
@define('LIMB_SMTP_HOST', 'localhost');
@define('LIMB_SMTP_AUTH', false);
@define('LIMB_SMTP_USER', '');
@define('LIMB_SMTP_PASSWORD', '');

/**
 * class lmbMailer.
 *
 * @package mail
 * @version $Id: lmbMailer.class.php 6865 2008-03-28 10:47:33Z svk $
 */
class lmbMailer
{
  protected $attachments = array();
  protected $images = array();

  protected $_config_properties_map = array(
    'phpmailer_dir' => 'PHPMAILER_DIR',
    'use_phpmail' => 'LIMB_USE_PHPMAIL',
    'smtp_host' => 'LIMB_SMTP_HOST',
    'smtp_port' => 'LIMB_SMTP_PORT',
    'smtp_auth' => 'LIMB_SMTP_AUTH',
    'smtp_user' => 'LIMB_SMTP_USER',
    'smtp_password' => '<PASSWORD>'
  );

  public $phpmailer_dir;
  public $use_phpmail;
  public $smtp_host;
  public $smtp_port;
  public $smtp_auth;
  public $smtp_user;
  public $smtp_password;

  function __construct($config = false)
  {
    $this->_setConfigFromDefinedContants();

    if($config)
      $this->setConfig($config);

  }

  protected function _setConfigFromDefinedContants()
  {
    foreach($this->_config_properties_map as $property_name => $define_name)
      $this->$property_name = constant($define_name);
  }

  public function setConfig($config = array())
  {
    foreach($config as $property_name => $property_value)
      $this->$property_name = $property_value;
  }

  protected function _createMailer()
  {
    include_once($this->phpmailer_dir . '/class.phpmailer.php');

    $mailer = new PHPMailer();
    $mailer->LE = "\r\n";

    if($this->use_phpmail)
      return $mailer;

    $mailer->IsSMTP();
    $mailer->Host = $this->smtp_host;
    $mailer->Port = $this->smtp_port;

    if($this->smtp_auth == true)
    {
      $mailer->SMTPAuth = true;
      $mailer->Username = $this->smtp_user;
      $mailer->Password = $this->smtp_password;
    }
    return $mailer;
  }

  function addAttachment($path, $name = "", $encoding = "base64", $type = "application/octet-stream")
  {
    $this->attachments[] = array(
      'path' => $path,
      'name' => $name,
      'encoding' => $encoding,
      'type' => $type
    );
  }
  
  function embedImage($path, $cid, $name="", $encoding="base64", $type="application/octet-stream")
  {
    $this->images[] = array(
      'path' => $path,
      'cid' => $cid,
      'name' => $name,
      'encoding' => $encoding,
      'type' => $type
    );
  }

  function sendPlainMail($recipients, $sender, $subject, $body, $charset = 'utf-8')
  {
    $mailer = $this->_createMailer();

    $mailer->IsHTML(false);
    $mailer->CharSet = $charset;

    if(!empty($this->attachments))
      $this->_addAttachments($mailer);

    if(!empty($this->images))
      $this->_addEmbeddedImages($mailer);
      
    $recipients = $this->processMailRecipients($recipients);

    foreach($recipients as $recipient)
      $mailer->AddAddress($recipient['address'], $recipient['name']);

    if(!$sender = $this->processMailAddressee($sender))
      return false;

    $mailer->From = $sender['address'];
    $mailer->FromName = $sender['name'];
    $mailer->Subject = $subject;
    $mailer->Body    = $body;

    return $mailer->Send();
  }

  function sendHtmlMail($recipients, $sender, $subject, $html, $text = null, $charset = 'utf-8')
  {
    $mailer = $this->_createMailer();

    $mailer->IsHTML(true);
    $mailer->CharSet = $charset;

    $mailer->Body = $html;

    if(!empty($this->attachments))
      $this->_addAttachments($mailer);

    if(!empty($this->images))
      $this->_addEmbeddedImages($mailer);
      
    if(!is_null($text))
      $mailer->AltBody = $text;

    $recipients = $this->processMailRecipients($recipients);

    foreach($recipients as $recipient)
      $mailer->AddAddress($recipient['address'], $recipient['name']);

    if(!$sender = $this->processMailAddressee($sender))
      return false;

    $mailer->From = $sender['address'];
    $mailer->FromName = $sender['name'];
    $mailer->Subject = $subject;

    return $mailer->Send();
  }

  function processMailRecipients($recipients)
  {
    if(!is_array($recipients))
       $recipients = array($recipients);
    $result = array();
    foreach($recipients as $recipient)
    {
      if($recipient = $this->processMailAddressee($recipient))
        $result[] = $recipient;
    }

    return $result;
  }

  function processMailAddressee($adressee)
  {
    if(is_array($adressee))
    {
      if(isset($adressee['address']) && isset($adressee['name']))
        return $adressee;

      return null;
    }
    elseif(preg_match('~("|\')?([^"\']+)("|\')?\s+<([^>]+)>~', $adressee, $matches))
      return array('address' => $matches[4], 'name' => $matches[2]);
    else
      return array('address' => $adressee, 'name' => '');
  }

  protected function _addAttachments($mailer)
  {
    foreach($this->attachments as $attachment)
      $mailer->AddAttachment($attachment['path'],
                             $attachment['name'],
                             $attachment['encoding'],
                             $attachment['type']);
  }
  
  protected function _addEmbeddedImages($mailer)
  {
    foreach ($this->images as $image)
    {
      $mailer->AddEmbeddedImage($image['path'],
                                $image['cid'],
                                $image['name'],
                                $image['encoding'],
                                $image['type']);
    }
  }
}
{'fraction_non_alphanumeric': 0.18146934955545158, 'fraction_numerical': 0.007487131492746842, 'mean_word_length': 3.233755942947702, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 155, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5304177', 'n_tokens_mistral': 3873, 'n_tokens_neox': 3665, 'n_words': 580}