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

使用Python程序发送附件时对方未收到的问题求助

排查Python邮件附件无法送达的问题

Hey there, let's break down why your attachment isn't reaching the recipient—looking at your code, the biggest issue jumps out right away: you haven't actually added any code to attach the file to your email!

核心问题:缺少附件添加逻辑

Your current code sets up a MIMEMultipart message, attaches plain text and HTML parts, but there's no section that reads your target file, encodes it, and adds it to the email. That's why the recipient never sees an attachment.

修复步骤:添加附件处理代码

Here's how to modify your code to include the attachment properly:

  1. Import necessary modules (you might already have these, but double-check):

    from email.mime.base import MIMEBase
    from email import encoders
    import mimetypes
    
  2. Add the attachment logic after attaching your text/HTML parts:

    # Replace with your actual attachment file path
    attachment_path = "path/to/your/attachment.file"
    
    # Guess the file's MIME type automatically
    content_type, encoding = mimetypes.guess_type(attachment_path)
    if content_type is None or encoding is not None:
        # Fallback if we can't guess the type
        content_type = 'application/octet-stream'
    main_type, sub_type = content_type.split('/', 1)
    
    # Read the file and encode it for email
    with open(attachment_path, 'rb') as file:
        attachment = MIMEBase(main_type, sub_type)
        attachment.set_payload(file.read())
    
    encoders.encode_base64(attachment)
    
    # Add headers to mark this as an attachment with a filename
    attachment.add_header(
        'Content-Disposition',
        f'attachment; filename="{attachment_path.split("/")[-1]}"'
    )
    
    # Attach the file to your message
    message.attach(attachment)
    
  3. Double-check your email sending code
    Make sure you're actually sending the constructed message via your SMTP server. You might have omitted this part in your snippet, but it's critical:

    import smtplib
    
    with smtplib.SMTP('your-smtp-host.com', 587) as server:
        server.starttls()
        server.login(remitente, 'your-email-password')
        server.send_message(message)
    

其他排查要点

If adding the attachment logic doesn't fix the issue, check these common pitfalls:

  • File path errors: Ensure the attachment_path is correct and the script has read access to the file. Add a try-except block around the file read to catch errors.
  • SMTP server restrictions: Some email providers limit attachment sizes (e.g., Gmail caps at 25MB) or block certain file types (executables, suspicious scripts).
  • Spam filters: The email might be landing in the recipient's spam folder. Ask them to check there, or adjust your email subject/content to avoid spam triggers.
  • Encoding issues: Make sure the filename uses compatible encoding (UTF-8 is safe for most systems).

内容的提问来源于stack exchange,提问作者Edgar Santos

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.27 04:18:40