使用PHPMailer发送邮件

 1、下载PHPMailer。

 2、开启你邮箱的SMTP 服务(一般是默认开启的)。

 3、然后就可以使用PHPMailer发送邮件了

if ($adminEmail) {//如果邮箱存在
    $email_html ='';
    $title = '审核通过';
    $html_email_content =   '您好<br>&nbsp;&nbsp;xxxx的申请已通过,请前往xxxx界面查看审核结果,如有任何疑问,请及时联系审核人员。感谢您的配合<br><br>xx部';
    include DIR_FS_ADMIN . '/includes/email/layouts/common_blade.php';//引用一个公共的html模板
    $htmlMsg['EMAIL_BODY'] = $email_html;//返回一个html页面,作为邮件内容
    $copyToEmail = [];
    $sender = array('user' => 'xxxx', 'email' => 'xxxxx@qq.com', 'password' => 'xxxxx');
    $mailConfig = array('host' => 'smtp.exmail.qq.com');
    $mail = fs_PHPMailer_to_mail($adminEmail, $copyToEmail, $title, $htmlMsg, $sender, $mailConfig);//发送邮件
}



/**
 * 使用PHPMailer类发送邮件,更多请参考PHPMailer的使用说明
 * @param $to_email //收件人邮箱,一位(string),多位(array)
 * @param array $copy_to_email //抄送人邮箱array
 * @param $title //邮件标题
 * @param array $html //邮件html内容 $html['EMAIL_BODY']-主体内容; $html['EMAIL_FILES']-附件内容
 * @param array $sender //发送者设置array('user' => 'xxx', 'email' => 'xxx@xxx.com', 'password' => 'xxx')
 * @param array $mail_config //配置 array('host' => 'smtp.gmail.com')
 * @return PHPMailer
 */
function fs_PHPMailer_to_mail($to_email, $copy_to_email = array(), $title, $html = array(), $sender = array('user' => 'xx', 'email' => 'xxxx@xxx.com', 'password' => 'xxxxx'), $mail_config = array('host' => 'smtp.gmail.com'))
{
    $url = urlencode($_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
    $arr = "url=".$url;
    $html['EMAIL_FOOTER'] = str_replace('Unsubscribemark','https://www.fs.com/email_unsubscribe.html?'.$arr , $html['EMAIL_FOOTER']);
    $html['EMAIL_HEADER'] = str_replace('Unsubscribemark','https://www.fs.com/email_unsubscribe.html?'.$arr , $html['EMAIL_HEADER']);
    $html['EMAIL_BODY'] = str_replace('Unsubscribemark','https://www.fs.com/email_unsubscribe.html?'.$arr , $html['EMAIL_BODY']);
    $mail = new PHPMailer();
    $mail->isSMTP();
    $mail->SMTPAuth = true;
    //配置
    $mail->Host = trim($mail_config['host']);
    $mail->Protocol = 'ssl';
    $mail->Port = 465;
    $mail->CharSet = 'UTF-8';
    $mail->Encoding = "base64";
    //发件人
    $mail->Username = trim($sender['email']);
    $mail->Password = trim($sender['password']);
    $mail->FromName = trim($sender['user']);
    $mail->From = trim($sender['email']);
    //HTML发件内容
    $mail->isHTML(true);
    $mail->Subject = $title;
    $mail->Body = $html['EMAIL_BODY'];

    //附件-最好别带中文路径(可能存在编码问题)
    //$mail->AddAttachment('C:\Users\John\Desktop\123\test.txt', basename('C:\Users\John\Desktop\123\test.txt'));
    if (!empty($html['EMAIL_FILES'])) {
        foreach ($html['EMAIL_FILES'] as $file) {
            //$mail->AddAttachment($file['url'], $file['name']);
//            $mail->AddAttachment(iconv('utf-8', 'gb2312', $file['url']), iconv('utf-8', 'GBK', basename($file['url'])));
            $mail->AddAttachment($file['url'], basename($file['url']));
        }
    }
    //收件人
    if (is_array($to_email)) {
        foreach ($to_email as $email) {
            $mail->addAddress($email, strstr($email, '@', TRUE));
        }
    } else {
        $mail->addAddress($to_email, strstr($to_email, '@', TRUE));
    }
    //抄送人

    if (is_array($copy_to_email)) {
        foreach ($copy_to_email as $email) {
            $mark = get_user_unsubscribe($email, $url);
            if($mark == 1){
                continue;
            }else{
                $mail->AddCC($email, strstr($email, '@', TRUE));
            }
        }
    }
    //发送
    $rs = $mail->send();
    if ($rs) {
        return 1;
    } else {
        return $mail->ErrorInfo;
    }
}

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值