Codeigniter通过电子邮件发送 - 添加BCC

时间:2017-05-05 17:38:27

标签: php codeigniter email

我是新人所以请耐心等待我。 我正在尝试将一个BCC收件人添加到一个codeigniter应用程序' email.php配置文件。原始版本是由一名员工创建的,该员工离开了我们的业务,我很难在代码中添加密件抄送收件人。 我搜索了stackoverflow并尝试了无穷无尽的变化,但我没有运气。我想要做的就是定义一个密送邮件收件人。 我真的非常感谢任何人的帮助:)。

以下是我认为相关的当前代码的一部分:

 <?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * CodeIgniter
 *
 * An open source application development framework for PHP 5.1.6 or newer
 *
 * @package     CodeIgniter
 * @author      ExpressionEngine Dev Team
 * @copyright   Copyright (c) 2008 - 2011, EllisLab, Inc.
 * @license     http://codeigniter.com/user_guide/license.html
 * @link        http://codeigniter.com
 * @since       Version 1.0
 * @filesource
 */

// ------------------------------------------------------------------------

/**
 * CodeIgniter Email Class
 *
 * Permits email to be sent using Mail, Sendmail, or SMTP.
 *
 * @package     CodeIgniter
 * @subpackage  Libraries
 * @category    Libraries
 * @author      ExpressionEngine Dev Team
 * @link        http://codeigniter.com/user_guide/libraries/email.html
 */
class CI_Email {

var $useragent      = "CodeIgniter";
var $mailpath       = "/usr/sbin/sendmail"; // Sendmail path
var $protocol       = "mail";   // mail/sendmail/smtp
var $smtp_host      = "";       // SMTP Server.  Example: mail.earthlink.net
var $smtp_user      = "";       // SMTP Username
var $smtp_pass      = "";       // SMTP Password
var $smtp_port      = "25";     // SMTP Port
var $smtp_timeout   = 5;        // SMTP Timeout in seconds
var $smtp_crypto    = "";       // SMTP Encryption. Can be null, tls or ssl.
var $wordwrap       = TRUE;     // TRUE/FALSE  Turns word-wrap on/off
var $wrapchars      = "76";     // Number of characters to wrap at.
var $mailtype       = "text";   // text/html  Defines email formatting
var $charset        = "utf-8";  // Default char set: iso-8859-1 or us-ascii
var $multipart      = "mixed";  // "mixed" (in the body) or "related" (separate)
var $alt_message    = '';       // Alternative message for HTML emails
var $validate       = FALSE;    // TRUE/FALSE.  Enables email validation
var $priority       = "3";      // Default priority (1 - 5)
var $newline        = "\n";     // Default newline. "\r\n" or "\n" (Use "\r\n" to comply with RFC 822)
var $crlf           = "\n";     // The RFC 2045 compliant CRLF for quoted-printable is "\r\n".  Apparently some servers,
                                // even on the receiving end think they need to muck with CRLFs, so using "\n", while
                                // distasteful, is the only thing that seems to work for all environments.
var $send_multipart = TRUE;     // TRUE/FALSE - Yahoo does not like multipart alternative, so this is an override.  Set to FALSE for Yahoo.
var $bcc_batch_mode = FALSE;    // TRUE/FALSE  Turns on/off Bcc batch feature
var $bcc_batch_size = 200;      // If bcc_batch_mode = TRUE, sets max number of Bccs in each batch
var $_safe_mode     = FALSE;
var $_subject       = "";
var $_body          = "";
var $_finalbody     = "";
var $_alt_boundary  = "";
var $_atc_boundary  = "";
var $_header_str    = "";
var $_smtp_connect  = "";
var $_encoding      = "8bit";
var $_IP            = FALSE;
var $_smtp_auth     = FALSE;
var $_replyto_flag  = FALSE;
var $_debug_msg     = array();
var $_recipients    = array();
var $_cc_array      = array();
var $_bcc_array     = array();
var $_headers       = array();
var $_attach_name   = array();
var $_attach_type   = array();
var $_attach_disp   = array();
var $_protocols     = array('mail', 'sendmail', 'smtp');
var $_base_charsets = array('us-ascii', 'iso-2022-');   // 7-bit charsets (excluding language suffix)
var $_bit_depths    = array('7bit', '8bit');
var $_priorities    = array('1 (Highest)', '2 (High)', '3 (Normal)', '4 (Low)', '5 (Lowest)');


/**
 * Constructor - Sets Email Preferences
 *
 * The constructor can be passed an array of config values
 */
public function __construct($config = array())
{
    if (count($config) > 0)
    {
        $this->initialize($config);
    }
    else
    {
        $this->_smtp_auth = ($this->smtp_user == '' AND $this->smtp_pass == '') ? FALSE : TRUE;
        $this->_safe_mode = ((boolean)@ini_get("safe_mode") === FALSE) ? FALSE : TRUE;
    }

    log_message('debug', "Email Class Initialized");
}

// --------------------------------------------------------------------

/**
 * Initialize preferences
 *
 * @access  public
 * @param   array
 * @return  void
 */
public function initialize($config = array())
{
    foreach ($config as $key => $val)
    {
        if (isset($this->$key))
        {
            $method = 'set_'.$key;

            if (method_exists($this, $method))
            {
                $this->$method($val);
            }
            else
            {
                $this->$key = $val;
            }
        }
    }
    $this->clear();

    $this->_smtp_auth = ($this->smtp_user == '' AND $this->smtp_pass == '') ? FALSE : TRUE;
    $this->_safe_mode = ((boolean)@ini_get("safe_mode") === FALSE) ? FALSE : TRUE;

    return $this;
}

// --------------------------------------------------------------------

/**
 * Initialize the Email Data
 *
 * @access  public
 * @return  void
 */
public function clear($clear_attachments = FALSE)
{
    $this->_subject     = "";
    $this->_body        = "";
    $this->_finalbody   = "";
    $this->_header_str  = "";
    $this->_replyto_flag = FALSE;
    $this->_recipients  = array();
    $this->_cc_array    = array();
    $this->_bcc_array   = array();
    $this->_headers     = array();
    $this->_debug_msg   = array();

    $this->_set_header('User-Agent', $this->useragent);
    $this->_set_header('Date', $this->_set_date());

    if ($clear_attachments !== FALSE)
    {
        $this->_attach_name = array();
        $this->_attach_type = array();
        $this->_attach_disp = array();
    }

    return $this;
}

// --------------------------------------------------------------------

/**
 * Set FROM
 *
 * @access  public
 * @param   string
 * @param   string
 * @return  void
 */
public function from($from, $name = '')
{
    if (preg_match( '/\<(.*)\>/', $from, $match))
    {
        $from = $match['1'];
    }

    if ($this->validate)
    {
        $this->validate_email($this->_str_to_array($from));
    }

    // prepare the display name
    if ($name != '')
    {
        // only use Q encoding if there are characters that would require it
        if ( ! preg_match('/[\200-\377]/', $name))
        {
            // add slashes for non-printing characters, slashes, and double quotes, and surround it in double quotes
            $name = '"'.addcslashes($name, "\0..\37\177'\"\\").'"';
        }
        else
        {
            $name = $this->_prep_q_encoding($name, TRUE);
        }
    }

    $this->_set_header('From', $name.' <'.$from.'>');
    $this->_set_header('Return-Path', '<'.$from.'>');

    return $this;
}

// --------------------------------------------------------------------

/**
 * Set Reply-to
 *
 * @access  public
 * @param   string
 * @param   string
 * @return  void
 */
public function reply_to($replyto, $name = '')
{
    if (preg_match( '/\<(.*)\>/', $replyto, $match))
    {
        $replyto = $match['1'];
    }

    if ($this->validate)
    {
        $this->validate_email($this->_str_to_array($replyto));
    }

    if ($name == '')
    {
        $name = $replyto;
    }

    if (strncmp($name, '"', 1) != 0)
    {
        $name = '"'.$name.'"';
    }

    $this->_set_header('Reply-To', $name.' <'.$replyto.'>');
    $this->_replyto_flag = TRUE;

    return $this;
}

// --------------------------------------------------------------------

/**
 * Set Recipients
 *
 * @access  public
 * @param   string
 * @return  void
 */
public function to($to)
{
    $to = $this->_str_to_array($to);
    $to = $this->clean_email($to);

    if ($this->validate)
    {
        $this->validate_email($to);
    }

    if ($this->_get_protocol() != 'mail')
    {
        $this->_set_header('To', implode(", ", $to));
    }

    switch ($this->_get_protocol())
    {
        case 'smtp'     :
            $this->_recipients = $to;
        break;
        case 'sendmail' :
        case 'mail'     :
            $this->_recipients = implode(", ", $to);
        break;
    }

    return $this;
}

// --------------------------------------------------------------------

/**
 * Set CC
 *
 * @access  public
 * @param   string
 * @return  void
 */
public function cc($cc)
{
    $cc = $this->_str_to_array($cc);
    $cc = $this->clean_email($cc);

    if ($this->validate)
    {
        $this->validate_email($cc);
    }

    $this->_set_header('Cc', implode(", ", $cc));

    if ($this->_get_protocol() == "smtp")
    {
        $this->_cc_array = $cc;
    }

    return $this;
}

// --------------------------------------------------------------------

/**
 * Set BCC
 *
 * @access  public
 * @param   string
 * @param   string
 * @return  void
 */
public function bcc($bcc, $limit = '')
{
    if ($limit != '' && is_numeric($limit))
    {
        $this->bcc_batch_mode = TRUE;
        $this->bcc_batch_size = $limit;
    }

    $bcc = $this->_str_to_array($bcc);
    $bcc = $this->clean_email($bcc);

    if ($this->validate)
    {
        $this->validate_email($bcc);
    }

    if (($this->_get_protocol() == "smtp") OR ($this->bcc_batch_mode && count($bcc) > $this->bcc_batch_size))
    {
        $this->_bcc_array = $bcc;
    }
    else
    {
        $this->_set_header('Bcc', implode(", ", $bcc));
    }

    return $this;
}

// --------------------------------------------------------------------

2 个答案:

答案 0 :(得分:4)

你可以在任何控制器索引函数中尝试下面的代码并运行它。

$this->load->library('email');

$this->email->from('your@example.com', 'Your Name');
$this->email->to('someone@example.com');
$this->email->cc('another@another-example.com');
$this->email->bcc('them@their-example.com');

$this->email->subject('Email Test');
$this->email->message('Testing the email class.');

$this->email->send();

echo $this->email->print_debugger();

答案 1 :(得分:-1)

简单var $bcc_batch_mode = TRUE;

FALSE修改为TRUE,然后试一试!!