<commit_before><?php
/**
 * Contains the mail class
 */

/**
 * The mail class
 * @author <NAME>
 */
class Mail_Core extends Model {

  /**
   * Sender e-mail address
   * @var string
   */
  public $from;
  
  /**
   * Sender name
   * @var string
   */
  public $from_name;

  /**
   * Receiver e-mail address
   * @var string
   */
  public $to;
  
  /**
   * Receiver name
   * @var string
   */
  public $to_name;

  /**
   * The e-mail subject
   * @var string
   */
  public $subject;

  /**
   * The e-mail body
   * @var string
   */
  public $message;

  /**
   * If true, adds a signature to the end of the message
   * @see signature
   * @var boolean
   */
  public $include_signature = true;

  /**
   * File attachments
   * @var array
   */
  public $attachments = [];

  /**
   * If true, send e-mail as HTML
   * @var bool
   */
  public $html = true;
  
  /**
   * Use an API
   * @var string
   */
  public $api = null;
  
  /**
   * API credentials
   * @var string
   */
  public $api_credentials = [];


  /**
   * Constructor
   */
  public function construct() {
    $this->setDefault();
    $this->setDefaultHeaders();
  }
  
  
  /**
   * Set default values
   */
  public function setDefault() {
    $this->from = "info@".BASE_DOMAIN;
  }

  /**
   * The signature to append to the end of the message
   * @return string
   */
  public function signature() {
    return '<hr><p>'.SITE_URL.'</p>';
  }

  /**
   * Prepare and send e-mail
   * @return bool
   */
  public function send() {

    if (!$this->from || !$this->to || !$this->message || !$this->subject) {
      addlog("mail", "Mail failed: Missing parameters", ["from" => $this->from, "to" => $this->to, "message" => $this->message, "subject" => $this->subject], "error");
      return false;
    }
    
    $message = $this->message;
    if ($this->include_signature)
      $message.= $this->signature();
    
    if ($this->api == "mandrill") {
      
      if (empty($this->api_credentials["key"])) {
        addlog("mail", "Mail failed: Missing Mandrill API-key", ["credentials" => $this->api_credentials], "error");
        return false;
      }
      
      $data = [
        "subject" => $this->subject,
        "from_email" => $this->from,
        "to" => [[
          "email" => $this->to,
          "type" => "to",
        ]],
        "headers" => [
          "Reply-To" => $this->from,
        ],
      ];
      if ($this->from_name)
        $data["from_name"] = $this->from_name;
      if ($this->to_name)
        $data["to"]["name"] = $this->to_name;
      if ($this->html)
        $data["html"] = $message;
      else
        $data["text"] = $message;
      
      if (!empty($this->attachments)) {
        foreach ($this->attachments as $attachment) {
          $file = $this->attachmentData($attachment);
          if (strlen($file["content"])) {
            $data["attachments"][] = [<commit_msg>Added ability to set custom mandrill data in mail.<commit_after><?php
/**
 * Contains the mail class
 */

/**
 * The mail class
 * @author <NAME>
 */
class Mail_Core extends Model {

  /**
   * Sender e-mail address
   * @var string
   */
  public $from;
  
  /**
   * Sender name
   * @var string
   */
  public $from_name;

  /**
   * Receiver e-mail address
   * @var string
   */
  public $to;
  
  /**
   * Receiver name
   * @var string
   */
  public $to_name;

  /**
   * The e-mail subject
   * @var string
   */
  public $subject;

  /**
   * The e-mail body
   * @var string
   */
  public $message;

  /**
   * If true, adds a signature to the end of the message
   * @see signature
   * @var boolean
   */
  public $include_signature = true;

  /**
   * File attachments
   * @var array
   */
  public $attachments = [];

  /**
   * If true, send e-mail as HTML
   * @var bool
   */
  public $html = true;
  
  /**
   * Use an API
   * @var string
   */
  public $api = null;
  
  /**
   * API credentials
   * @var string
   */
  public $api_credentials = [];
  
  /**
   * Mandrill data to merge with message
   * @var array
   */
  public $mandrill_data = [];


  /**
   * Constructor
   */
  public function construct() {
    $this->setDefault();
    $this->setDefaultHeaders();
  }
  
  
  /**
   * Set default values
   */
  public function setDefault() {
    $this->from = "info@".BASE_DOMAIN;
  }

  /**
   * The signature to append to the end of the message
   * @return string
   */
  public function signature() {
    return '<hr><p>'.SITE_URL.'</p>';
  }

  /**
   * Prepare and send e-mail
   * @return bool
   */
  public function send() {

    if (!$this->from || !$this->to || !$this->message || !$this->subject) {
      addlog("mail", "Mail failed: Missing parameters", ["from" => $this->from, "to" => $this->to, "message" => $this->message, "subject" => $this->subject], "error");
      return false;
    }
    
    $message = $this->message;
    if ($this->include_signature)
      $message.= $this->signature();
    
    if ($this->api == "mandrill") {
      
      if (empty($this->api_credentials["key"])) {
        addlog("mail", "Mail failed: Missing Mandrill API-key", ["credentials" => $this->api_credentials], "error");
        return false;
      }
      
      $data = $this->mandrill_data + [
        "subject" => $this->subject,
        "from_email" => $this->from,
        "to" => [[
          "email" => $this->to,
          "type" => "to",
        ]],
        "headers" => [
          "Reply-To" => $this->from,
        ],
      ];
      if ($this->from_name)
        $data["from_name"] = $this->from_name;
      if ($this->to_name)
        $data["to"]["name"] = $this->to_name;
      if ($this->html)
        $data["html"] = $message;
      else
        $data["text"] = $message;
      
      if (!empty($this->attachments)) {
        foreach ($this->attachments as $attachment) {
          $file = $this->attachmentData($attachment);
          if (strlen($file["content"])) {
            $data["attachments"][] = [
{'fraction_non_alphanumeric': 0.18041582830315225, 'fraction_numerical': 0.0, 'mean_word_length': 2.032536858159634, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 94, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '4780765', 'n_tokens_mistral': 2014, 'n_tokens_neox': 1980, 'n_words': 469}