Classes

File io/mail/Mail.class.php

File io/mail/Mail.class.php

  1:   2:   3:   4:   5:   6:   7:   8:   9:  10:  11:  12:  13:  14:  15:  16:  17:  18:  19:  20:  21:  22:  23:  24:  25:  26:  27:  28:  29:  30:  31:  32:  33:  34:  35:  36:  37:  38:  39:  40:  41:  42:  43:  44:  45:  46:  47:  48:  49:  50:  51:  52:  53:  54:  55:  56:  57:  58:  59:  60:  61:  62:  63:  64:  65:  66:  67:  68:  69:  70:  71:  72:  73:  74:  75:  76:  77:  78:  79:  80:  81:  82:  83:  84:  85:  86:  87:  88:  89:  90:  91:  92:  93:  94:  95:  96:  97:  98:  99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213: 214: 215: 216: 217: 218: 219: 220: 221: 222: 223: 224: 225: 226: 227: 228: 229: 230: 231: 232: 233: 234: 235: 236: 237: 238: 239: 240: 241: 242: 243: 244: 245: 246: 247: 248: 249: 250: 251: 252: 253: 254: 255: 256: 257: 258: 259: 260: 261: 262: 263: 264: 265: 266: 267: 268: 269: 270: 271: 272: 273: 274: 275: 276: 277: 278: 279: 280: 281: 282: 283: 284: 285: 286: 287: 288: 289: 290: 291: 292: 293: 294: 295: 296: 297: 298: 299: 
<?php
/**
 * This class allows you to send mails without having to deal with the mail headers and parameters.
 * @package     IO
 * @subpackage  Mail
 * @copyright   &copy; 2005-2019 PHPBoost
 * @license     https://www.gnu.org/licenses/gpl-3.0.html GNU/GPL-3.0
 * @author      Benoit SAUTEL <ben.popeye@phpboost.com>
 * @version     PHPBoost 5.2 - last update: 2016 10 24
 * @since       PHPBoost 3.0 - 2010 03 08
 * @contributor Julien BRISWALTER <j1.seth@phpboost.com>
 * @contributor Arnaud GENET <elenwii@phpboost.com>
*/

class Mail
{
    const SENDER_ADMIN = 'admin';

    const SENDER_USER = 'user';

    /**
     * @var sting object of the mail
     */
    var $subject = '';

    /**
     * @var string content of the mail
     */
    var $content = '';

    /**
     * @var string Address of the mail sender.
     */
    var $sender_mail = '';

    /**
     * @var string The mail sender name.
     */
    var $sender_name = '';

    /**
     * @var string Address of the mail to reply to.
     */
    var $reply_to_mail = '';

    /**
     * @var string The reply to mail sender name.
     */
    var $reply_to_name = '';

    /**
     * @var The mail headers.
     */
    var $headers = '';

    /**
     * @var string[] Recipients of the mail. If they are more than one, a comma separates their addresses.
     */
    var $recipients = array();

    /**
     * @var string[] Cc recipients of the mail. If they are more than one, a comma separates their addresses.
     */
    var $cc_recipients = array();

    /**
     * @var string[] Bcc recipients of the mail. If they are more than one, a comma separates their addresses.
     */
    var $bcc_recipients = array();

    /**
     * @var string Tells whether the content contains HTML code
     */
    var $is_html = false;


    /**
     * Builds a Mail subject.
     */
    public function __construct()
    {
    }

    /**
     * Sets the mail sender.
     * @param string $sender The mail sender address.
     * @param string $sender_name SENDER_ADMIN constante if the mail is sent by the administrator, SENDER_USER constante for user or string for other name
     */
    public function set_sender($sender, $sender_name = self::SENDER_ADMIN)
    {
        $site_name = GeneralConfig::load()->get_site_name();

        if ($sender_name == self::SENDER_ADMIN || $sender_name == self::SENDER_USER)
        {
            $sender_name = $sender_name == self::SENDER_ADMIN ? LangLoader::get_message('administrator', 'user-common') : LangLoader::get_message('user', 'user-common');
        }

        $this->sender_name = str_replace('"', '', $site_name . ' - ' . $sender_name);

        $this->sender_mail = $sender;
    }

    /**
     * Sets the mail to reply to.
     * @param string $reply_to The mail address to reply to.
     * @param string $reply_to_name SENDER_ADMIN constante if the mail is sent by the administrator, SENDER_USER constante for user or string for other name
     */
    public function set_reply_to($reply_to, $reply_to_name = '')
    {
        if ($reply_to_name == self::SENDER_ADMIN || $reply_to_name == self::SENDER_USER)
        {
            $reply_to_name = $reply_to_name == self::SENDER_ADMIN ? LangLoader::get_message('administrator', 'user-common') : LangLoader::get_message('user', 'user-common');
        }

        $this->reply_to_name = $reply_to_name;

        $this->reply_to_mail = $reply_to;
    }

    /**
     * Adds a recipient to the list
     * @param string $address The address to which the mail must be sent
     * @param string $name Name of the recipient (facultative)
     */
    public function add_recipient($address, $name = '')
    {
        if (self::check_mail($address))
        {
            $this->recipients[$address] = $name;
        }
    }

    public function clear_recipients()
    {
        $this->recipients = array();
    }

    /**
     * Adds a cc recipient to the list
     * @param string $address The address to which the mail must be sent
     * @param string $name Name of the recipient (facultative)
     */
    public function add_cc_recipient($address, $name = '')
    {
        if (self::check_mail($address))
        {
            $this->cc_recipients[$address] = $name;
        }
    }

    public function clear_cc_recipients()
    {
        $this->cc_recipients = array();
    }

    /**
     * Adds a bcc recipient to the list
     * @param string $address The address to which the mail must be sent
     * @param string $name Name of the recipient (facultative)
     */
    public function add_bcc_recipient($address, $name = '')
    {
        if (self::check_mail($address))
        {
            $this->bcc_recipients[$address] = $name;
        }
    }

    public function clear_bcc_recipients()
    {
        $this->bcc_recipients = array();
    }

    private static function check_mail($mail)
    {
        return AppContext::get_mail_service()->is_mail_valid($mail);
    }

    /**
     * Returns a map associating email addresses to the corresponding names (can be empty).
     * @return string[string]
     */
    public function get_recipients()
    {
        return $this->recipients;
    }

    /**
     * Returns a map associating email addresses to the corresponding names (can be empty).
     * @return string[string]
     */
    public function get_cc_recipients()
    {
        return $this->cc_recipients;
    }

    /**
     * Returns a map associating email addresses to the corresponding names (can be empty).
     * @return string[string]
     */
    public function get_bcc_recipients()
    {
        return $this->bcc_recipients;
    }

    /**
     * Sets the mail subject
     * @param string $subject Mail subject
     */
    public function set_subject($subject)
    {
        $this->subject = $subject;
    }

    /**
     * The mail content.
     * @param string $content The mail content
     */
    public function set_content($content)
    {
        $this->content = $content;
        $this->set_is_html($content != strip_tags($content));
    }

    /**
     * Sets the headers. Forces them, they won't be generated automatically.
     * @param string $headers The mail headers.
     */
    public function set_headers($headers)
    {
        $this->headers = $headers;
    }

    /**
     * Returns the mail address of the sender.
     * @return string the sender's mail address
     */
    public function get_sender_mail()
    {
        return $this->sender_mail;
    }

    /**
     * Returns the mail sender's name.
     * @return string The mail sender's name.
     */
    public function get_sender_name()
    {
        return $this->sender_name;
    }

    /**
     * Returns the mail address to reply to.
     * @return string the reply to mail address
     */
    public function get_reply_to_mail()
    {
        return $this->reply_to_mail;
    }

    /**
     * Returns the reply to mail sender's name.
     * @return string The reply to mail sender's name.
     */
    public function get_reply_to_name()
    {
        return $this->reply_to_name;
    }

    /**
     * Returns the mail subject.
     * @return string The mail subject.
     */
    public function get_subject()
    {
        return $this->subject;
    }

    /**
     * Returns the mail content.
     * @return string The mail content.
     */
    public function get_content()
    {
        return $this->content;
    }

    public function set_is_html($is)
    {
        $this->is_html = $is;
    }

    public function is_html()
    {
        return $this->is_html;
    }
}
?>