ThinkPHP整合PHPMailer发送邮件笔记!

下载PHPMailer:https://sourceforge.net/projects/phpmailer/
GitHub下载:https://github.com/PHPMailer/PHPMailer

config.php 加入配置:



    // 配置邮件发送服务器
    'MAIL_HOST' =>'smtp.163.com',//smtp服务器的名称
    'MAIL_SMTPAUTH' =>TRUE, //启用smtp认证
    'MAIL_USERNAME' =>'[email protected]',//你的邮箱名
    'MAIL_FROM' =>'[email protected]',//发件人地址
    'MAIL_FROMNAME'=>'DragonersLi',//发件人姓名
    'MAIL_PASSWORD' =>'######',//邮箱密码
    'MAIL_CHARSET' =>'utf-8',//设置邮件编码
    'MAIL_ISHTML' =>TRUE, // 是否HTML格式邮件

function.php加方法如下:


/**
 * 邮件发送函数
 * @param string $to 发送给哪个邮箱
 * @param string $title 发送主题
 * @param string $content 发送内容
 *
 */
function sendEmail($to, $title, $content) {

    Vendor('PHPMailer.PHPMailerAutoload');
    $mail = new PHPMailer(); //实例化
    $mail->IsSMTP(); // 设定使用SMTP服务
    // $mail->SMTPSecure = 'ssl';// SMTP 安全协议
    $mail->Host=C('MAIL_HOST'); //smtp服务器的名称(这里以QQ邮箱为例)
    $mail->SMTPAuth = C('MAIL_SMTPAUTH'); //启用smtp认证
    $mail->Username = C('MAIL_USERNAME'); //你的邮箱名
    $mail->Password = C('MAIL_PASSWORD') ; //邮箱密码
    $mail->From = C('MAIL_FROM'); //发件人地址(也就是你的邮箱地址)
    $mail->FromName = C('MAIL_FROMNAME'); //发件人姓名
    $mail->AddAddress($to,"尊敬的客户");
    $mail->WordWrap = 50; //设置每行字符长度
    $mail->IsHTML(C('MAIL_ISHTML')); // 是否HTML格式邮件
    $mail->CharSet=C('MAIL_CHARSET'); //设置邮件编码
    $mail->Subject =$title; //邮件主题
    $mail->Body = $content; //邮件内容
    $mail->AltBody = "这是一个纯文本的身体在非营利的HTML电子邮件客户端"; //邮件正文不支持HTML的备用显示
    return $mail->Send() ? true : $mail->ErrorInfo;
}
/**
 *
 * @access public
 * @param array $conf
 */
public static function sendMail($conf){
    $mail = new PHPMailer();
    $mail -> IsSMTP();
    $mail -> IsHTML(true);
    $mail -> From = C("FROMMAIL");
    $mail -> FromName = C("FROMNAME");
    $mail -> Subject = $conf["title"];
    $mail -> Host = C("SMTPSERVER");
    $mail -> SMTPAuth = true;
    $mail -> Username = C("MAILUSER");
    $mail -> Password = C("MAILPASS");
    $mail -> Body = $conf["body"];
    $mail -> CharSet = "UTF-8";
    $mail -> Encoding = "base64";
    $mail -> AddAddress($conf["email"], $conf["username"]);
    $mail -> Send();
}

调用代码如下:

        var_dump(sendEmail($to='[email protected]', $title='这是一封测试邮件!', $content='天下武功唯快不破!如来神掌,专克蛤蟆功!'));

你可能感兴趣的:(ThinkPHP整合PHPMailer发送邮件笔记!)