【发布时间】:2015-05-18 13:20:21
【问题描述】:
谁能解释以 UTF-8 格式发送此电子邮件的正确方法是什么?目的地作为人类不可读的代码接收。
Edit1:添加了有关显示 upload_file 变量来自何处的代码的更多详细信息。 Edit2:添加了代码的最后一部分
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
def upload(upload_file):
ftp = ftplib.FTP('ftp.domain.com')
ftp.login("user","pass")
f = open(upload_file,'rb')
ftp_server_response = ftp.storbinary('STOR %s' %upload_file, f)
ftp_server_response_msg = ftp_server_response.split("/", 5)[4]
f.close()
ftp.quit()
os.remove(upload_file)
uploaded_filename = os.path.basename(upload_file)
html = """\
<iframe src="https://example.com/embed/{file_id}/{uploaded_file}" scrolling="no" frameborder="0" width="700" height="430" allowfullscreen="true" webkitallowfullscreen="true" mozallowfullscreen="true"></iframe>
""".format(file_id=ftp_server_response_msg, uploaded_file=uploaded_filename)
From = 'email@domain.com'
Recipient = 'email@domain.com'
# Credentials
username = 'user01@domain.com'
password = 'password'
server = smtplib.SMTP('smtp.domain.com:587')
email_msg = MIMEMultipart('alternative')
email_msg['Subject'] = os.path.basename(upload_file).rsplit(".", 1)[0]
email_msg['From'] = From
email_msg['To'] = Recipient
email_msg_part1 = MIMEText(html, 'html')
email_msg.attach(email_msg_part1)
server.ehlo()
server.starttls()
server.login(username,password)
server.sendmail(From, Recipient, email_msg.as_string())
server.quit()
if __name__ == "__main__":
pool = Pool(9)
tasks = []
for root, dirs, filenames in os.walk("/ext_hdd/download"):
dirs[:] = [d for d in dirs if d not in exclude]
for extension in file_extensions:
for filename in fnmatch.filter(filenames, extension):
match = os.path.join(root, filename)
file_size = os.path.getsize(match)
if file_size > 209715200:
tasks.append(pool.apply_async(upload, args=(match,)))
else:
pass
for task in tasks:
print task
task.get()
pool.close()
pool.join()
【问题讨论】:
-
一切看起来都像 ASCII 所以你应该有编码问题。可以贴一张收到的邮件的图片吗?
-
您好,由于我是通过电子邮件发给博主的,所以我只能向您展示发送部分:pastebin.com/zu2CBzWK
-
我怀疑您的文件名变量不是 UTF-8 编码的。
upload_file变量在哪里创建? -
添加一些更详细的代码,请检查
-
upload_filesource 仍未显示,您已添加 ftp 函数但未在代码中调用。
标签: python python-2.7 utf-8 html-email smtplib