PHP using PHPMailer plug-in implementation of the e-mail transmission function

First, download PHPMailer files, and the "Choose File" into the corresponding directory of your project

Download: https: //github.com/PHPMailer/PHPMailer/ 
select File: PHPMailer \ src \

Second, the configuration file (configuration services as well as the sender, content configuration)

/**
 * 文件:dict/dict_email.php
 * 邮件发送服务配置及内容相关配置
 * @Author NangongYi
 * @Time 2019/10/12
 */


/** 服务器参数配置 **/

$config['distribute_mail_config'] = [
    'protocol' => 'smtp',
    'smtp_crypto' => 'ssl',
    'smtp_host' => 'smtp.mxhichina.com',
    'smtp_user' => '[email protected]',
    'smtp_pass' => '123456',
    'smtp_port' => '465',
    'mailtype' => 'text',
    'validate' => 'true',
    'charset' => 'utf-8',
    'wordwrap' => 'TRUE',
    'smtp_timeout' => '30',
    'newline' => PHP_EOL,
    'crlf' => "\r\n"
];

/** 内容等相关配置 **/

$config['distribute_mail_info'] = [
    'from' => [
        'address' => '[email protected]',
        'name' => 'Master'
    ],
    'to' => [
        'to1' => '[email protected]'
    ],
    'cc' => [
        'cc1' => '[email protected]',
        'cc2' => '[email protected]',
    ],
    'bcc' => [
        'bcc1' => '[email protected]'
    ],
    'subject' => [
        'sub1' => '主题1,
        'sub2' => '主题2',
    ],
    'content' => [
        'cont1' => "内容1",
        'cont2' => "内容2",
    ]
];

Third, the code portions (content profile calls, data processing, sending e-mail operations)

/**
 * 发送邮件
 * @param int $type : 邮件内容类型  1,初次分账,失败发邮件;2,查询分账,失败发邮件
 * @param string $msg : 邮件内容
 * @return object    $msg='', $type
 */
public function SendMail($msg, $type)
{
    // 引入配置文件
    $this->load->config('dict/dict_email');
    // 邮件服务配置
    $mail_config = $this->config->item('distribute_mail_config');
    // 邮件信息相关
    $mail_info = $this->config->item('distribute_mail_info');
    // 发送邮件操作
    return $this->send($mail_config, $mail_info, $type, $msg);
}

/**
 * 发送邮件
 */
private function send($email_config, $email_info, $type, $msg)
{
    // 加载邮件服务类文件
    $this->mail_class_autolod();
    // 实例化邮件类
    $mail = new PHPMailer\PHPMailer\PHPMailer(true);
    // 对邮件服务配置
    $this->mail_config($mail, $email_config);
    // 邮件发送信息配置
    $mail->setFrom($email_config['smtp_user'], 'WB-Master');  //发件人
    // 发送抄送用户处理
    $this->receiver($mail, $email_info);
    // 邮件内容信息配置
    $this->mail_info($mail, $email_info, $type, $msg);
    // 邮件发送操作
    return $mail->send()?'SUCCESS': $mail->ErrorInfo;
}

/**
 * 加载邮件服务类文件
 */
private function mail_class_autolod()
{
    // 加载文件
    include_once(APPPATH.'services/base/PHPMailer/Exception.php');
    include_once(APPPATH.'services/base/PHPMailer/PHPMailer.php');
    include_once(APPPATH.'services/base/PHPMailer/SMTP.php');
}

/**
 * 邮件服务配置
 */
private function mail_config($mail, $email_config)
{
    // 服务器配置
    $mail->CharSet =$email_config['charset'];                     //设定邮件编码
    $mail->SMTPDebug = 0;                        // 调试模式输出
    $mail->isSMTP();                             // 使用SMTP
    $mail->Host = $email_config['smtp_host'];                // SMTP服务器
    $mail->SMTPAuth = true;                      // 允许 SMTP 认证
    $mail->Username = $email_config['smtp_user'];                // SMTP 用户名  即邮箱的用户名
    $mail->Password = $email_config['smtp_pass'];             // SMTP 密码  部分邮箱是授权码(例如163邮箱)
    $mail->SMTPSecure = $email_config['smtp_crypto'];         // 允许 TLS 或者ssl协议
    $mail->Port = $email_config['smtp_port']; // 服务器端口 25 或者465 具体要看邮箱服务器支持
}

/**
 * 发送人处理
 */
private function receiver($mail, $email_info)
{
    if(isset($email_info['to']) && $email_info['to']){
        for($i=1;$i<=count($email_info['to']);$i++){
            $mail->addAddress($email_info['to']['to'.$i], 'to'.$i);  // 收件人
        }
    }
    if(isset($email_info['cc']) && $email_info['cc']){
        for($i=1;$i<=count($email_info['cc']);$i++){
            $mail->addCC($email_info['cc']['cc'.$i]);  // 抄送人
        }
    }
    if(isset($email_info['bcc']) && $email_info['bcc']){
        for($i=1;$i<=count($email_info['bcc']);$i++){
            $mail->addBCC($email_info['bcc']['bcc'.$i]);  // 密送人
        }
    }
}

/**
 * 邮件内容信息配置
 */
private function mail_info($mail, $email_info, $type, $msg)
{
    $mail->isHTML(true);  // 是否以HTML文档格式发送  发送后客户端可直接显示对应HTML内容
    $mail->Subject = $email_info['subject']['sub'.$type]; // 标题
    $mail->Body    = $email_info['content']['cont'.$type].$msg; // 内容
}

Fourth, the retrieval method, email

/**
 * 测试 - 发送邮件(NEW)
 */
public function test_send_mail_new()
{
    $this->load->service('account_service');
    echo json_encode($this->account_service->SendMail());
}

Written in the last:

About mail, used other plug-ins, but there was PHPMailer most stable, and easiest to use, no problem testing, service, production, deployment, therefore, here to do some recording, convenient later, the junior partner in need , you can also look at!
Published 59 original articles · won praise 2 · Views 5565

Guess you like

Origin blog.csdn.net/LDR1109/article/details/103352471