You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Django 1.11.29长邮件主题插入 及逗号后多余空格问题求助

Django 1.11.29长邮件主题格式异常的解决办法

针对Django 1.11.29发送长邮件主题时自动插入制表符、逗号后多余空格的问题,可尝试以下几种方案:

方案一:自定义主题编码函数,绕过默认折叠逻辑

Django默认的邮件主题折叠逻辑在处理带数字逗号的长文本时会出错,可手动对主题进行RFC合规的QP编码,避免自动折叠:

from django.core.mail.message import sanitize_address
import smtplib
from email.mime.text import MIMEText
from email.header import Header

def send_custom_email(subject, message, from_email, recipient_list):
    # 手动编码主题,指定编码格式,禁用自动折叠
    encoded_subject = Header(subject, 'utf-8').encode()
    
    # 构造MIME邮件
    msg = MIMEText(message, 'plain', 'utf-8')
    msg['Subject'] = encoded_subject
    msg['From'] = sanitize_address(from_email, 'utf-8')
    msg['To'] = ', '.join([sanitize_address(addr, 'utf-8') for addr in recipient_list])
    
    # 发送邮件(替换为你的SMTP配置)
    with smtplib.SMTP('your-smtp-server', 587) as server:
        server.starttls()
        server.login('your-username', 'your-password')
        server.send_message(msg)

方案二:直接构造完整MIME对象,精准控制主题格式

完全跳过Django的邮件封装,直接使用Python的email模块构造邮件,手动处理主题的换行和编码:

import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.header import Header

def send_long_subject_email(subject, html_content, from_email, to_email):
    msg = MIMEMultipart('alternative')
    
    # 手动拆分长主题:在合适的空格处拆分,第二行前缀加空格符合RFC 2822规范
    if len(subject) > 70:
        split_pos = subject.rfind(' ', 0, 70)
        if split_pos != -1:
            subject_line1 = subject[:split_pos]
            subject_line2 = ' ' + subject[split_pos+1:]
            encoded_subject = f"{Header(subject_line1, 'utf-8').encode()}\r\n{subject_line2}"
        else:
            # 无合适拆分位置则直接编码
            encoded_subject = Header(subject, 'utf-8').encode()
    else:
        encoded_subject = Header(subject, 'utf-8').encode()
    
    msg['Subject'] = encoded_subject
    msg['From'] = from_email
    msg['To'] = to_email
    
    # 添加邮件内容
    part1 = MIMEText('Plain text content', 'plain', 'utf-8')
    part2 = MIMEText(html_content, 'html', 'utf-8')
    msg.attach(part1)
    msg.attach(part2)
    
    # 发送邮件(替换为你的SMTP配置)
    with smtplib.SMTP('smtp.example.com', 587) as server:
        server.starttls()
        server.login('your-email@example.com', 'your-password')
        server.send_message(msg)

方案三:预处理主题,规避Django错误折叠

在传入主题前,手动对长主题进行合规拆分,避开数字逗号位置,选择合适的空格处拆分,让Django的折叠逻辑不会插入错误字符:

from django.core.mail import send_mail

def preprocess_subject(subject):
    max_length = 70
    if len(subject) <= max_length:
        return subject
    
    # 按空格拆分,逐段拼接,避免超过长度限制
    parts = []
    current_part = ''
    for word in subject.split(' '):
        if len(current_part) + len(word) + 1 > max_length:
            parts.append(current_part.strip())
            current_part = word
        else:
            current_part += ' ' + word
    parts.append(current_part.strip())
    
    # 拼接时每行前加空格,符合RFC规范
    return '\r\n ' .join(parts)

# 使用预处理后的主题发送邮件
long_subject = "Earth - Earth population is 4,652,123 people and digest is d3ea33dbe135a35fdde8984d1d920b8e6149526ed1fb5c9c656e8834d85d3a4e"
processed_subject = preprocess_subject(long_subject)
send_mail(
    processed_subject,
    '邮件内容',
    'me@me.com',
    ['me1@me.com'],
    fail_silently=False,
)

注意:Django 1.11.x已停止官方维护,长期来看建议升级到受支持的版本,从根源上解决这类遗留问题。


内容的提问来源于stack exchange,提问作者evrey matem

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.03 21:30:50