【发布时间】:2021-08-09 18:03:06
【问题描述】:
我有一个名为email_body.txt 的文本文件,其中包含以下数据:
email_body.txt:
Dear {b},
Hope all your queries were resolved in your recent consultation with Dr. XXXXXXXXXXXXX on: {e}
Your prescription is attached herewith. Wishing you a speedy recovery!
Thank You
Regards
XXXXXXXXXXXXX
XXXXXXXXXXXXX
这曾经是f string,并且电子邮件正文和电子邮件主题已修复。但是,我的客户要求电子邮件正文应该是可编辑的,因为他可能会在几个月内更改它。所以现在我被卡住了。
我想创建一个文本文件并让客户在该文件中按照他的意愿修改电子邮件正文,并且我希望正文中的占位符在我使用文件处理将该字符串添加到我的 Python 文件时实际工作。
这里是 main.py:
import smtplib, os
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email import encoders
from typing import final
cwd=os.getcwd()
bodyf=cwd+"\Email_Body_&_Subject\email_body.txt"
print(bodyf)
b="Deven Jain"
e="XYZ"
email_user = "XXXXXXXXXXXXX@gmail.com"
email_password = "XXXXXXXXXXXXX"
email_send = "XXXXXXXXXXXXX@gmail.com"
subject = "Prescription of Consultation"
msg = MIMEMultipart()
msg['From'] = email_user
msg['To'] = email_send
msg['Subject'] = subject
body=open(bodyf,"r")
x=body.read()
body.close()
final=f"{x}"
print(final)
body =final
msg.attach(MIMEText(body,'plain'))
'''
filename=pdfFile
attachment=open(filename,'rb')
part = MIMEBase('application','octet-stream')
part.set_payload((attachment).read())
encoders.encode_base64(part)
debug=filename.split(".")
if debug[-1]=="png":
part.add_header('Content-Disposition',"attachment; filename= "+f"{c}-{b}_({e}).png")
else:
part.add_header('Content-Disposition',"attachment; filename= "+f"{c}-{b}_({e}).pdf")
'''
text = msg.as_string()
server = smtplib.SMTP('smtp.gmail.com',587)
server.starttls()
server.login(email_user,email_password)
server.sendmail(email_user,email_send,text)
server.quit()
接下来我可以尝试什么?
【问题讨论】:
-
我更喜欢使用
.format(b='name', e='something')方法,而不是 F-String。 -
如何将它集成到我的代码中?
-
更新:成功了,谢谢!请将其发布为答案,以便我投票并标记为已批准。
标签: python string email email-attachments f-string