sendgrid / sendgrid-python

The Official Twilio SendGrid Python API Library
https://sendgrid.com
MIT License
1.53k stars 711 forks source link

The name of the attachment contains Chinese characters will be garbled #968

Closed shechostr closed 3 years ago

shechostr commented 3 years ago

Add an attachment, the attachment name contains Chinese, the content of the attachment is normal after receiving the email, but the attachment name is garbled

Code Snippet

def attachment_parse(attach_file_info,is_base=False):
    message_attachment = []
    if attach_file_info:
        for attach_file in attach_file_info:
            if attach_file and os.path.exists(attach_file):
                with open(attach_file, 'rb') as f:
                    data = f.read()
                dest_name = '{}'.format(os.path.split(attach_file)[1])
                dest_name = dest_name
                encoded = base64.b64encode(data).decode()
                message_attachment.append(Attachment(FileContent(encoded),
                                                     FileName(u'{}'.format(dest_name)),
                                                     FileType(attach_file_info[attach_file]),
                                                     Disposition('attachment'),
                                                     ContentId(str(time.time()))))
    return message_attachment

Exception/Log

The name of the attachment contains Chinese characters will be garbled

Technical details:

thinkingserious commented 3 years ago

Hello @shechostr,

Thank you for taking the time to report this issue!

I was not able to reproduce this locally. Following is the sample code I used:

import base64
import os
from sendgrid.helpers.mail import (
    Mail, Attachment, FileContent, FileName,
    FileType, Disposition)
from sendgrid import SendGridAPIClient

message = Mail(
    from_email='example+from@example.com',
    to_emails='example+to@example.com',
    subject='Sending with Twilio SendGrid is Fun',
    html_content='<strong>and easy to do anywhere, even with Python</strong>')
file_path = './text.txt'
with open(file_path, 'rb') as f:
    data = f.read()
    f.close()
encoded = base64.b64encode(data).decode()
attachment = Attachment()
attachment.file_content = FileContent(encoded)
attachment.file_type = FileType('plain/text')
attachment.file_name = FileName(u'{}'.format('text金.txt'))
attachment.disposition = Disposition('attachment')
message.attachment = attachment
try:
    sendgrid_client = SendGridAPIClient(os.environ.get('SENDGRID_API_KEY'))
    response = sendgrid_client.send(message)
    print(response.status_code)
    print(response.body)
    print(response.headers)
except Exception as e:
    print(e.message)

Could you please provide a bit more detail to help troubleshoot?

  1. What email client did this issue happen with?
  2. Could you provide the specific title that is causing the error?

With best regards,

Elmer

shechostr commented 3 years ago

Thank you very much for your answer, I tried your Chinese file name and no problem, error email is NetEase Mail and Tencent Mail ( xx@163,com xx@qq.com) ,But google email is no problem Please try this file name "广告排期急迫.txt"

shwetha-manvinkurke commented 3 years ago

@shechostr I did try sending the email (gmail) with the suggested file name attached and I didn't see the issue. Since this is only an issue with certain email clients, this doesn't look like a library issue to me. I suggest you reach out to https://support.sendgrid.com to resolve the issue.