您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

ThinkPHP邮件发送函数

2025/8/9 5:24:18发布23次查看
/**
 * 系统邮件发送函数
 * @param string $to    接收邮件者邮箱
 * @param string $name  接收邮件者名称
 * @param string $subject 邮件主题 
 * @param string $body    邮件内容
 * @param string $attachment 附件列表
 * @return boolean 
 */
function think_send_mail($to, $name, $subject = '', $body = '', $attachment = null){
    $config = c('think_email');
    vendor('phpmailer.class#phpmailer'); //从phpmailer目录导class.phpmailer.php类文件
    $mail             = new phpmailer(); //phpmailer对象
    $mail->charset    = 'utf-8'; //设定邮件编码,默认iso-8859-1,如果发中文此项必须设置,否则乱码
    $mail->issmtp();  // 设定使用smtp服务
    $mail->smtpdebug  = 0;                     // 关闭smtp调试功能
                                               // 1 = errors and messages
                                               // 2 = messages only
    $mail->smtpauth   = true;                  // 启用 smtp 验证功能
    $mail->smtpsecure = 'ssl';                 // 使用安全协议
    $mail->host       = $config['smtp_host'];  // smtp 服务器
    $mail->port       = $config['smtp_port'];  // smtp服务器的端口号
    $mail->username   = $config['smtp_user'];  // smtp服务器用户名
    $mail->password   = $config['smtp_pass'];  // smtp服务器密码
    $mail->setfrom($config['from_email'], $config['from_name']);
    $replyemail       = $config['reply_email']?$config['reply_email']:$config['from_email'];
    $replyname        = $config['reply_name']?$config['reply_name']:$config['from_name'];
    $mail->addreplyto($replyemail, $replyname);
    $mail->subject    = $subject;
    $mail->msghtml($body);
    $mail->addaddress($to, $name);
    if(is_array($attachment)){ // 添加附件
        foreach ($attachment as $file){
            is_file($file) && $mail->addattachment($file);
        }
    }
    return $mail->send() ? true : $mail->errorinfo;
}此函数只能在thinkphp中使用且需要phpmailer扩展的支持;
phpmailer扩展的放置目录为 thinkphp/extend/vendor/phpmailer/class.phpmailer.php
phpmail的下载地址:
https://code.google.com/a/apache-extras.org/p/phpmailer
使用此函数 必须在项目中加入以下配置项//邮件配置
'think_email' => array(
    'smtp_host'   => 'smtp.aaa.com', //smtp服务器
    'smtp_port'   => '465', //smtp服务器端口
    'smtp_user'   => 'mail@aaa.com', //smtp服务器用户名
    'smtp_pass'   => 'password', //smtp服务器密码
    'from_email'  => 'mail@aaa.com', //发件人email
    'from_name'   => 'thinkphp', //发件人名称
    'reply_email' => '', //回复email(留空则为发件人email)
    'reply_name'  => '', //回复名称(留空则为发件人名称)
), ad:真正免费,域名+虚机+企业邮箱=0元
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product