该问题产生的原因是iOS端默认的编码方式为UTF-8,而CSV文件可能采用了不同的编码方式,导致了乱码问题。解决方法是在发送邮件时,指定CSV文件的编码方式为UTF-8,可通过以下代码实现:
# 导入模块
import boto3
from botocore.exceptions import ClientError
# AWS SES配置
AWS_REGION = "us-west-2"
SENDER = "sender@example.com"
RECIPIENT = "recipient@example.com"
SUBJECT = "Test email with csv attachment"
BODY_TEXT = "Please see the attachment for csv data."
charset = "UTF-8"
# 读取csv文件
with open("file.csv", "rb") as f:
data = f.read()
# 构造SES参数
att = {
'Data': data,
'Charset': charset,
'Filename': 'file.csv'
}
msg = {
'Subject': {
'Data': SUBJECT,
'Charset': charset
},
'Body': {
'Text': {
'Data': BODY_TEXT,
'Charset': charset
}
},
'Source': SENDER,
'Destinations': [RECIPIENT],
'Attachments': [att]
}
# 发送邮件
try:
ses = boto3.client('ses', region_name=AWS_REGION)
response = ses.send_email(
Destination={
'ToAddresses': msg["Destinations"]
},
Message=msg,
Source=msg["Source"]
)
print("Email sent! Message ID:", response['MessageId'])
except ClientError as e:
print(e.response['Error']['Message'])