你现在的位置:首页 > PHP网站建设知识库 > Discuz > 正文

Discuz开发 - 发送html格式电子邮件

Discuz!发送的电子邮件默认是text/plain纯文本格式,如果需要发送HTML格式邮件,需要进行discuz开发才行,需要对/include/sendmail.inc.php文件应用如下补丁:

--- sendmail.inc.original.php
+++ sendmail.inc.mime.php
 
 $email_subject = '=?'.$charset.'?B?'.base64_encode(str_replace("/r", '', str_replace("/n", '', '['.$bbname.'] '.$email_subject))).'?=';
 $email_message = chunk_split(base64_encode(str_replace("/r/n.", " /r/n..", str_replace("/n", "/r/n", str_replace("/r", "/n", str_replace("/r/n", "/n", str_replace("/n/r", "/r", $email_message)))))));
 
 $email_from = $email_from == '' ? '=?'.$charset.'?B?'.base64_encode($bbname)."?= <$adminemail>" : (preg_match('/^(.+?) /<(.+?)/>$/',$email_from, $from) ? '=?'.$charset.'?B?'.base64_encode($from[1])."?= <$from[2]>" : $email_from);
 
 foreach(explode(',', $email_to) as $touser) {
        $tousers[] = preg_match('/^(.+?) /<(.+?)/>$/',$touser, $to) ? ($mailusername ? '=?'.$charset.'?B?'.base64_encode($to[1])."?= <$to[2]>" : $to[2]) : $touser;
 }
 $email_to = implode(',', $tousers);
 
-$headers = "From: $email_from{$maildelimiter}X-Priority: 3{$maildelimiter}X-Mailer: Discuz! $version{$maildelimiter}MIME-Version: 1.0{$maildelimiter}Content-type: text/plain; charset=$charset{$maildelimiter}Content-Transfer-Encoding: base64{$maildelimiter}";
+$headers = "From: $email_from{$maildelimiter}X-Priority: 3{$maildelimiter}X-Mailer: Discuz! $version{$maildelimiter}MIME-Version: 1.0{$maildelimiter}Content-type: ".(true === is_string($sendmail_mime) ? $sendmail_mime : 'text/plain')."; charset=$charset{$maildelimiter}Content-Transfer-Encoding: base64{$maildelimiter}";
 
 $mail['port'] = $mail['port'] ? $mail['port'] : 25;
 
 if($mail['mailsend'] == 1 && function_exists('mail')) {
 
        @mail($email_to, $email_subject, $email_message, $headers);
 
 } elseif($mail['mailsend'] == 2) {
 
        if(!$fp = fsockopen($mail['server'], $mail['port'], $errno, $errstr, 30)) {
                $errorlog('SMTP', "($mail[server]:$mail[port]) CONNECT - Unable to connect to the SMTP server", 0);
之后可以通过设定全局变量$sendmail_mime来发送HTML格式电子邮件。